Esempio n. 1
0
    def test_error_response_handling(self):
        client = Client(api_key, api_secret)
        # check if appropriate error is raised depending on status code
        # AND if error data is in response, it is used
        error_body = {
            'error': {
                'id': 0,
                'message': 'fake error message',
                'description': 'fake error description'
            }
        }
        for ecode, eclass in six.iteritems(errors._status_code_to_class):
            mock_response = {'body': json.dumps(error_body), 'status': ecode}
            hp.register_uri(hp.GET, re.compile('.*' + str(ecode) + '$'),
                            **mock_response)
            with self.assertRaises(eclass):
                client._handle_response(client._get(str(ecode)))

        # check if appropriate error raised even with no error message in body or if content-type is not text/json
        for ecode, eclass in six.iteritems(errors._status_code_to_class):
            mock_response = {'status': ecode, 'content_type': 'text/plain'}
            hp.register_uri(hp.GET, re.compile('.*' + str(ecode) + '$'),
                            **mock_response)
            with self.assertRaises(eclass):
                client._handle_response(client._get(str(ecode)))

        # check if status code is unrecognized, generic APIError is raised
        mock_response = {'status': 418}
        hp.register_uri(hp.GET, re.compile('.*test$'), **mock_response)
        with self.assertRaises(errors.APIError):
            client._handle_response(client._get('test'))
Esempio n. 2
0
    def test_auth_succeeds_with_string_unicode_and_bytes(self):
        mock_response = {'body': '{}', 'status': 200}
        # String
        api_key = 'fakekey'
        api_secret = 'fakesecret'
        self.assertIsInstance(api_key, six.string_types)
        self.assertIsInstance(api_secret, six.string_types)

        client = Client(api_key, api_secret)
        hp.register_uri(hp.GET, re.compile('.*test$'), **mock_response)
        self.assertEqual(client._get('test').status_code, 200)

        # Unicode
        api_key = u'fakekey'
        api_secret = u'fakesecret'
        self.assertIsInstance(api_key, six.text_type)
        self.assertIsInstance(api_secret, six.text_type)

        client = Client(api_key, api_secret)
        hp.register_uri(hp.GET, re.compile('.*test$'), **mock_response)
        self.assertEqual(client._get('test').status_code, 200)

        # Bytes
        api_key = api_key.encode('utf-8')
        api_secret = api_secret.encode('utf-8')
        self.assertIsInstance(api_key, six.binary_type)
        self.assertIsInstance(api_secret, six.binary_type)

        client = Client(api_key, api_secret)
        hp.register_uri(hp.GET, re.compile('.*test$'), **mock_response)
        self.assertEqual(client._get('test').status_code, 200)
Esempio n. 3
0
 def test_200_response_handling(self):
     # check if 200 response returns a json decoded response
     client = Client(api_key, api_secret)
     mock_response = {'body': json.dumps(mock_items), 'status': 200}
     hp.register_uri(hp.GET, re.compile('.*test$'), **mock_response)
     response = client._get('test')
     self.assertEqual(client._handle_response(response), mock_items)
Esempio n. 4
0
 def test_http_base_api_uri_issues_uri_security_warning(self):
     insecure_url = 'http://api.hitbtc.com/api/1/'
     with self.assertWarns(UserWarning):
         client = Client(api_key, api_secret, insecure_url)
         # check if response is OK even with insecure_url
         mock_response = {'body': '{}', 'status': 200}
         hp.register_uri(hp.GET, insecure_url, **mock_response)
         self.assertEqual(client._get().status_code, 200)
Esempio n. 5
0
    def test_base_api_uri_used_instead_of_default(self):
        # Requests to the default BASE_API_URI will noticeably fail by raising an AssertionError. Requests to the new URL will respond HTTP 200.
        new_base_api_uri = 'https://api.hitbtc.com/api/new/'
        # If any error is raised by the server, the test suite will never exit when using Python 3. This strange technique is used to raise the errors outside of the mocked server environment.
        errors_in_server = []

        def mock_response(request, uri, headers):
            try:
                self.assertEqual(uri, new_base_api_uri)
            except AssertionError as e:
                errors_in_server.append(e)
            return 200, headers, '{}'

        hp.register_uri(hp.GET, Client.BASE_API_URI, mock_response)
        hp.register_uri(hp.GET, new_base_api_uri, mock_response)

        client = Client(api_key, api_secret)  # default BASE_API_URI
        client_new = Client(api_key, api_secret, new_base_api_uri)

        self.assertEqual(client_new._get().status_code, 200)
        with self.assertRaises(AssertionError):
            client._get()
            if errors_in_server:
                raise errors_in_server.pop()