Beispiel #1
0
    def test_add_basic_auth(self):
        """Testing HostingServiceHTTPRequest.add_basic_auth"""
        request = HostingServiceHTTPRequest('http://example.com')
        request.add_basic_auth(b'username', b'password')

        self.assertEqual(request.headers, {
            'Authorization': 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=',
        })
Beispiel #2
0
    def test_add_basic_auth(self):
        """Testing HostingServiceHTTPRequest.add_basic_auth"""
        request = HostingServiceHTTPRequest('http://example.com')
        request.add_basic_auth(b'username', b'password')

        self.assertEqual(
            request.headers,
            {
                'Authorization': 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=',
            })
Beispiel #3
0
    def _make_raw_request(self, url, body=None, method='GET',
                          content_type=''):
        """Make an HTTP request.

        Args:
            url (unicode):
                The URL to make the request against.

            body (unicode or bytes, optional):
                The content of the request.

            method (unicode, optional):
                The request method. If not provided, it defaults to a ``GET``
                request.

            content_type (unicode, optional):
                The type of the content being POSTed.

        Returns:
            bytes:
            The contents of the HTTP response body.

        Raises:
            urllib2.URLError:
                The HTTP request failed.
        """
        logger.debug('Making request to Jenkins CI %s', url)

        headers = {}

        if self.crumb:
            headers[self.crumb_request_field] = self.crumb

        if content_type:
            headers['Content-Type'] = content_type

        request = HostingServiceHTTPRequest(
            url,
            body=body,
            method=method,
            headers=headers)

        request.add_basic_auth(self.username, self.password)

        response = request.open()
        return response.data
    def test_json_with_non_json_response(self):
        """Testing HostingServiceHTTPResponse.json with non-JSON response"""
        request = HostingServiceHTTPRequest('http://example.com')
        response = HostingServiceHTTPResponse(request=request,
                                              url='http://example.com',
                                              data=b'{[(',
                                              headers={},
                                              status_code=200)

        with self.assertRaises(ValueError):
            response.json
Beispiel #5
0
 def test_json(self):
     """Testing HostingServiceHTTPResponse.json"""
     request = HostingServiceHTTPRequest('http://example.com')
     response = HostingServiceHTTPResponse(request=request,
                                           url='http://example.com',
                                           data=b'{"a": 1, "b": 2}',
                                           headers={},
                                           status_code=200)
     self.assertEqual(response.json, {
         'a': 1,
         'b': 2,
     })
    def test_init_with_query(self):
        """Testing HostingServiceHTTPRequest construction with query="""
        request = HostingServiceHTTPRequest(
            url='http://example.com?z=1&z=2&baz=true',
            query={
                'foo': 'bar',
                'a': 10,
                'list': ['a', 'b', 'c'],
            })

        self.assertEqual(
            request.url,
            'http://example.com?a=10&baz=true&foo=bar&list=a&list=b&list=c'
            '&z=1&z=2')
Beispiel #7
0
    def test_init_with_body_not_bytes(self):
        """Testing HostingServiceHTTPRequest construction with non-bytes body
        """
        account = HostingServiceAccount()
        service = HostingService(account)

        expected_message = (
            'Received non-bytes body for the HTTP request for %r. This is '
            'likely an implementation problem. Please make sure only byte '
            'strings are sent for the request body.' % HostingService)

        with self.assertRaisesMessage(TypeError, expected_message):
            HostingServiceHTTPRequest(
                url='http://example.com?z=1&z=2&baz=true',
                method='POST',
                body=123,
                hosting_service=service)
Beispiel #8
0
    def test_get_header(self):
        """Testing HostingServiceHTTPRequest.get_header"""
        request = HostingServiceHTTPRequest('http://example.com')
        response = HostingServiceHTTPResponse(
            request=request,
            url=request.url,
            status_code=200,
            data=b'',
            headers={
                str('Authorization'): str('Basic abc123'),
                str('Content-Length'): str('123'),
            })

        self.assertEqual(response.get_header('Authorization'), 'Basic abc123')
        self.assertEqual(response.get_header('AUTHORIZATION'), 'Basic abc123')
        self.assertEqual(response.get_header('authorization'), 'Basic abc123')

        self.assertEqual(response.get_header('Content-Length'), '123')
        self.assertEqual(response.get_header('CONTENT-LENGTH'), '123')
        self.assertEqual(response.get_header('content-length'), '123')
Beispiel #9
0
    def test_init_with_header_value_not_unicode(self):
        """Testing HostingServiceHTTPRequest construction with non-Unicode
        header value
        """
        account = HostingServiceAccount()
        service = HostingService(account)

        expected_message = (
            'Received non-Unicode header %r (value=%r) for the HTTP request '
            'for %r. This is likely an implementation problem. Please make '
            'sure only Unicode strings are sent in request headers.' %
            ('My-Header', b'abc', HostingService))

        with self.assertRaisesMessage(TypeError, expected_message):
            HostingServiceHTTPRequest(
                url='http://example.com?z=1&z=2&baz=true',
                method='POST',
                headers={
                    'My-Header': b'abc',
                },
                hosting_service=service)
Beispiel #10
0
    def test_get_header(self):
        """Testing HostingServiceHTTPRequest.get_header"""
        request = HostingServiceHTTPRequest('http://example.com',
                                            headers={
                                                'Authorization':
                                                'Basic abc123',
                                                'Content-Length': '123',
                                            })

        self.assertEqual(request.get_header('Authorization'), 'Basic abc123')
        self.assertEqual(request.get_header('AUTHORIZATION'), 'Basic abc123')
        self.assertEqual(request.get_header('authorization'), 'Basic abc123')

        self.assertEqual(request.get_header('Content-Length'), '123')
        self.assertEqual(request.get_header('CONTENT-LENGTH'), '123')
        self.assertEqual(request.get_header('content-length'), '123')