Beispiel #1
0
    def test_should_issue_request_with_proxy_authorization(self):
        raise SkipTest('https://github.com/gabrielfalcao/HTTPretty/issues/122')
        Client('key', None,
               'http://*****:*****@localhost:8080').request('GET', '/')

        self.assertEqual(self.request.headers['proxy-authorization'],
                         'Basic dXNlcjpwYXNz')
Beispiel #2
0
 def test_should_issue_request(self, mock):
     mock.side_effect = RaiseException(requests.exceptions.ConnectionError,
                                       num=1)
     mock.return_value = requests.Response()
     mock.return_value.status_code = 201
     self.assertIsInstance(
         Client('key').request('GET', '/', {}), requests.Response)
    def test_should_raise_account_error(self):
        httpretty.register_uri(httpretty.GET, 'https://api.tinify.com/', status=401,
            body='{"error":"Unauthorized","message":"Oops!"}')

        with self.assertRaises(AccountError) as context:
            Client('key').request('GET', '/')
        self.assertEqual('Oops! (HTTP 401/Unauthorized)', str(context.exception))
    def test_should_raise_client_error(self):
        httpretty.register_uri(httpretty.GET, 'https://api.tinify.com/', status=492,
            body='{"error":"BadRequest","message":"Oops!"}')

        with self.assertRaises(ClientError) as context:
            Client('key').request('GET', '/')
        self.assertEqual('Oops! (HTTP 492/BadRequest)', str(context.exception))
    def test_should_raise_server_error(self):
        httpretty.register_uri(httpretty.GET, 'https://api.tinify.com/', status=584,
            body='{"error":"InternalServerError","message":"Oops!"}')

        with self.assertRaises(ServerError) as context:
            Client('key').request('GET', '/')
        self.assertEqual('Oops! (HTTP 584/InternalServerError)', str(context.exception))
    def test_should_raise_server_error(self):
        httpretty.register_uri(httpretty.GET, 'https://api.tinify.com/', status=543,
            body='<!-- this is not json -->')

        with self.assertRaises(ServerError) as context:
            Client('key').request('GET', '/')

        msg = r'Error while parsing response: .* \(HTTP 543/ParseError\)'
        self.assertRegexpMatches(str(context.exception), msg)
Beispiel #7
0
    def test_should_issue_request(self):
        httpretty.register_uri(httpretty.GET,
                               'https://api.tinify.com/',
                               responses=[
                                   httpretty.Response(
                                       body='<!-- this is not json -->',
                                       status=543),
                                   httpretty.Response(body='all good',
                                                      status=201),
                               ])

        response = Client('key').request('GET', '/')
        self.assertEqual('201', str(response.status_code))
Beispiel #8
0
    def test_should_issue_request(self):
        httpretty.register_uri(
            httpretty.GET,
            'https://api.tinify.com/',
            responses=[
                httpretty.Response(
                    body='{"error":"InternalServerError","message":"Oops!"}',
                    status=584),
                httpretty.Response(body='all good', status=201),
            ])

        response = Client('key').request('GET', '/')
        self.assertEqual('201', str(response.status_code))
 def test_should_raise_connection_error(self):
     with self.assertRaises(ConnectionError) as context:
         Client('key').request('GET', '/')
     self.assertEqual('Error while connecting: connection error',
                      str(context.exception))
 def test_should_raise_connection_error_with_cause(self):
     with self.assertRaises(ConnectionError) as context:
         Client('key').request('GET', '/')
     self.assertIsInstance(context.exception.__cause__,
                           requests.exceptions.Timeout)
    def test_should_issue_request_with_user_agent(self):
        Client('key', 'TestApp/0.2').request('GET', '/')

        self.assertEqual(self.request.headers['user-agent'],
                         Client.USER_AGENT + ' TestApp/0.2')
    def test_should_update_compression_count(self):
        Client('key').request('GET', '/')

        self.assertEqual(tinify.compression_count, 12)
    def test_should_issue_request_with_json_body(self):
        Client('key').request('GET', '/', {'hello': 'world'})

        self.assertEqual(self.request.headers['content-type'],
                         'application/json')
        self.assertEqual(self.request.body, b'{"hello": "world"}')
    def test_should_issue_request_without_content_type_when_options_are_empty(
            self):
        Client('key').request('GET', '/', {})

        self.assertIsNone(self.request.headers.get('content-type'))
    def test_should_issue_request_without_body_when_options_are_empty(self):
        Client('key').request('GET', '/', {})

        self.assertEqual(self.request.body, b'')
    def test_should_issue_request(self):
        Client('key').request('GET', '/')

        self.assertEqual(
            self.request.headers['authorization'],
            'Basic {0}'.format(b64encode(b'api:key').decode('ascii')))