Esempio n. 1
0
class TestClient(unittest.TestCase):

    def setUp(self):
        self.host = 'http://api.test.com'
        self.api_key = "SENDGRID_API_KEY"
        self.request_headers = {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer {}'.format(self.api_key)
        }
        self.client = Client(host=self.host,
                             request_headers=self.request_headers,
                             version=3)

    def test__init__(self):
        default_client = Client(host=self.host)
        self.assertEqual(default_client.host, self.host)
        self.assertEqual(default_client.request_headers, {})
        self.assertIs(default_client.timeout, None)
        methods = ['delete', 'get', 'patch', 'post', 'put']
        self.assertEqual(default_client.methods, methods)
        self.assertIsNone(default_client._version)
        self.assertEqual(default_client._url_path, [])

        request_headers = {'X-Test': 'test', 'X-Test2': 1}
        version = 3
        client = Client(host=self.host,
                        request_headers=request_headers,
                        version=version,
                        timeout=10)
        self.assertEqual(client.host, self.host)
        self.assertEqual(client.request_headers, request_headers)
        methods = ['delete', 'get', 'patch', 'post', 'put']
        self.assertEqual(client.methods, methods)
        self.assertEqual(client._version, 3)
        self.assertEqual(client._url_path, [])
        self.assertEqual(client.timeout, 10)

    def test__build_versioned_url(self):
        url = '/api_keys?hello=1&world=2'
        versioned_url = self.client._build_versioned_url(url)
        url = '{}/v{}{}'.format(self.host, str(self.client._version), url)
        self.assertEqual(versioned_url, url)

    def test__build_url(self):
        self.client._url_path = self.client._url_path + ['here']
        self.client._url_path = self.client._url_path + ['there']
        self.client._url_path = self.client._url_path + [1]
        self.client._version = 3
        url = '{}/v{}{}'.format(
            self.host,
            str(self.client._version),
            '/here/there/1?hello=0&world=1&ztest=0&ztest=1'
        )
        query_params = {'hello': 0, 'world': 1, 'ztest': [0, 1]}
        built_url = self.client._build_url(query_params)
        self.assertEqual(built_url, url)

    def test__update_headers(self):
        request_headers = {'X-Test': 'Test'}
        self.client._update_headers(request_headers)
        self.assertIn('X-Test', self.client.request_headers)
        self.client.request_headers.pop('X-Test', None)

    def test__build_client(self):
        new_client = self.client._build_client('test')
        self.assertTrue(new_client)

    def test__(self):
        self.assertEqual(self.client._url_path, [])
        client = self.client._('hello')
        url_path = ['hello']
        self.assertEqual(client._url_path[0], url_path[0])

    def test__getattr__(self):
        mock_client = MockClient(self.host, 200)
        client = mock_client.__getattr__('hello')
        url_path = ['hello']
        self.assertEqual(client._url_path, url_path)
        self.assertEqual(client.__getattr__('get').__name__, 'http_request')

        # Test Version
        client.version(3)
        self.assertEqual(client._version, 3)

        # Test GET
        mock_client._url_path + ['test']
        r = mock_client.get()
        self.assertEqual(r.status_code, 200)

        # Test POST
        r = mock_client.put()
        self.assertEqual(r.status_code, 200)

        # Test PATCH
        r = mock_client.patch()
        self.assertEqual(r.status_code, 200)

        # Test POST
        mock_client.response_code = 201
        r = mock_client.post()
        self.assertEqual(r.status_code, 201)

        # Test DELETE
        mock_client.response_code = 204
        r = mock_client.delete()
        self.assertEqual(r.status_code, 204)

        mock_client.response_code = 400
        self.assertRaises(BadRequestsError, mock_client.get)

        mock_client.response_code = 404
        self.assertRaises(NotFoundError, mock_client.post)

        mock_client.response_code = 415
        self.assertRaises(UnsupportedMediaTypeError, mock_client.patch)

        mock_client.response_code = 503
        self.assertRaises(ServiceUnavailableError, mock_client.delete)

        mock_client.response_code = 523
        self.assertRaises(HTTPError, mock_client.delete)

    def test_client_pickle_unpickle(self):
        pickled_client = pickle.dumps(self.client)
        unpickled_client = pickle.loads(pickled_client)
        self.assertDictEqual(
            self.client.__dict__,
            unpickled_client.__dict__,
            "original client and unpickled client must have the same state"
        )
Esempio n. 2
0
class TestClient(unittest.TestCase):
    def setUp(self):
        self.host = 'http://api.test.com'
        self.api_key = "SENDGRID_API_KEY"
        self.request_headers = {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer {}'.format(self.api_key)
        }
        self.client = Client(host=self.host,
                             request_headers=self.request_headers,
                             version=3)

    def test__init__(self):
        default_client = Client(host=self.host)
        self.assertEqual(default_client.host, self.host)
        self.assertEqual(default_client.request_headers, {})
        self.assertIs(default_client.timeout, None)
        methods = {'delete', 'get', 'patch', 'post', 'put'}
        self.assertEqual(default_client.methods, methods)
        self.assertIsNone(default_client._version)
        self.assertEqual(default_client._url_path, [])

        request_headers = {'X-Test': 'test', 'X-Test2': 1}
        version = 3
        client = Client(host=self.host,
                        request_headers=request_headers,
                        version=version,
                        timeout=10)
        self.assertEqual(client.host, self.host)
        self.assertEqual(client.request_headers, request_headers)
        methods = {'delete', 'get', 'patch', 'post', 'put'}
        self.assertEqual(client.methods, methods)
        self.assertEqual(client._version, 3)
        self.assertEqual(client._url_path, [])
        self.assertEqual(client.timeout, 10)

    def test__build_versioned_url(self):
        url = '/api_keys?hello=1&world=2'
        versioned_url = self.client._build_versioned_url(url)
        url = '{}/v{}{}'.format(self.host, str(self.client._version), url)
        self.assertEqual(versioned_url, url)

    def test__build_url(self):
        self.client._url_path = self.client._url_path + ['here']
        self.client._url_path = self.client._url_path + ['there']
        self.client._url_path = self.client._url_path + [1]
        self.client._version = 3
        url = '{}/v{}{}'.format(
            self.host, str(self.client._version),
            '/here/there/1?hello=0&world=1&ztest=0&ztest=1')
        query_params = {'hello': 0, 'world': 1, 'ztest': [0, 1]}
        built_url = self.client._build_url(query_params)
        self.assertEqual(built_url, url)

    @mock.patch('python_http_client.client.Client._make_request')
    def test__urllib_headers(self, maker):
        self.client._update_headers({'X-test': 'Test'})
        self.client.get()
        request = maker.call_args[0][1]
        self.assertIn('X-test', request.headers)

    @mock.patch('python_http_client.client.Client._make_request')
    def test__urllib_method(self, maker):
        self.client.delete()
        request = maker.call_args[0][1]
        self.assertEqual(request.get_method(), 'DELETE')

    def test__update_headers(self):
        request_headers = {'X-Test': 'Test'}
        self.client._update_headers(request_headers)
        self.assertIn('X-Test', self.client.request_headers)
        self.client.request_headers.pop('X-Test', None)

    def test__build_client(self):
        new_client = self.client._build_client('test')
        self.assertTrue(new_client)

    def test__(self):
        self.assertEqual(self.client._url_path, [])
        client = self.client._('hello')
        url_path = ['hello']
        self.assertEqual(client._url_path[0], url_path[0])

    def test__getattr__(self):
        mock_client = MockClient(self.host, 200)
        client = mock_client.__getattr__('hello')
        url_path = ['hello']
        self.assertEqual(client._url_path, url_path)
        self.assertEqual(client.__getattr__('get').__name__, 'http_request')

        # Test Version
        client.version(3)
        self.assertEqual(client._version, 3)

        # Test GET
        mock_client._url_path += ['test']
        r = mock_client.get()
        self.assertEqual(r.status_code, 200)

        # Test POST
        r = mock_client.put()
        self.assertEqual(r.status_code, 200)

        # Test PATCH
        r = mock_client.patch()
        self.assertEqual(r.status_code, 200)

        # Test POST
        mock_client.response_code = 201
        r = mock_client.post()
        self.assertEqual(r.status_code, 201)

        # Test DELETE
        mock_client.response_code = 204
        r = mock_client.delete()
        self.assertEqual(r.status_code, 204)

        mock_client.response_code = 400
        self.assertRaises(BadRequestsError, mock_client.get)

        mock_client.response_code = 404
        self.assertRaises(NotFoundError, mock_client.post)

        mock_client.response_code = 415
        self.assertRaises(UnsupportedMediaTypeError, mock_client.patch)

        mock_client.response_code = 503
        self.assertRaises(ServiceUnavailableError, mock_client.delete)

        mock_client.response_code = 523
        self.assertRaises(HTTPError, mock_client.delete)

    def test_client_pickle_unpickle(self):
        pickled_client = pickle.dumps(self.client)
        unpickled_client = pickle.loads(pickled_client)
        self.assertDictEqual(
            self.client.__dict__, unpickled_client.__dict__,
            "original client and unpickled client must have the same state")
Esempio n. 3
0
class TestClient(unittest.TestCase):
    def setUp(self):
        self.host = 'http://api.test.com'
        self.client = Client(host=self.host)
        self.api_key = "SENDGRID_API_KEY"
        self.request_headers = {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + self.api_key
        }
        self.client = Client(host=self.host,
                             request_headers=self.request_headers,
                             version=3)

    def test__init__(self):
        default_client = Client(host=self.host)
        self.assertEqual(default_client.host, self.host)
        self.assertEqual(default_client.request_headers, {})
        methods = ['delete', 'get', 'patch', 'post', 'put']
        self.assertEqual(default_client.methods, methods)
        self.assertEqual(default_client._version, None)
        self.assertEqual(default_client._url_path, [])

        request_headers = {'X-Test': 'test', 'X-Test2': 1}
        version = 3
        client = Client(host=self.host,
                        request_headers=request_headers,
                        version=version)
        self.assertEqual(client.host, self.host)
        self.assertEqual(client.request_headers, request_headers)
        methods = ['delete', 'get', 'patch', 'post', 'put']
        self.assertEqual(client.methods, methods)
        self.assertEqual(client._version, 3)
        self.assertEqual(client._url_path, [])

    def test__build_versioned_url(self):
        url = '/api_keys?hello=1&world=2'
        versioned_url = self.client._build_versioned_url(url)
        url = '{0}/v{1}{2}'.format(self.host, str(self.client._version), url)
        self.assertEqual(versioned_url, url)

    def test__build_url(self):
        self.client._url_path = self.client._url_path + ['here']
        self.client._url_path = self.client._url_path + ['there']
        self.client._url_path = self.client._url_path + [1]
        self.client._version = 3
        url = '{0}/v{1}{2}'.format(
            self.host, str(self.client._version),
            '/here/there/1?hello=0&world=1&ztest=0&ztest=1')
        query_params = {'hello': 0, 'world': 1, 'ztest': [0, 1]}
        built_url = self.client._build_url(query_params)
        self.assertEqual(built_url, url)

    def test__update_headers(self):
        request_headers = {'X-Test': 'Test'}
        self.client._update_headers(request_headers)
        self.assertTrue('X-Test' in self.client.request_headers)
        self.client.request_headers.pop('X-Test', None)

    def test__build_client(self):
        new_client = self.client._build_client('test')
        self.assertTrue(new_client)

    def test__(self):
        self.assertEqual(self.client._url_path, [])
        client = self.client._('hello')
        url_path = ['hello']
        self.assertEqual(client._url_path[0], url_path[0])

    def test__getattr__(self):
        mock_client = MockClient(self.host, 200)
        client = mock_client.__getattr__('hello')
        url_path = ['hello']
        self.assertEqual(client._url_path, url_path)
        self.assertEqual(client.__getattr__('get').__name__, 'http_request')

        # Test Version
        client.version(3)
        self.assertEqual(client._version, 3)

        # Test GET
        mock_client._url_path + ['test']
        r = mock_client.get()
        self.assertEqual(r.status_code, 200)

        # Test POST
        r = mock_client.put()
        self.assertEqual(r.status_code, 200)

        # Test PATCH
        r = mock_client.patch()
        self.assertEqual(r.status_code, 200)

        # Test POST
        mock_client.response_code = 201
        r = mock_client.post()
        self.assertEqual(r.status_code, 201)

        # Test DELETE
        mock_client.response_code = 204
        r = mock_client.delete()
        self.assertEqual(r.status_code, 204)
Esempio n. 4
0
class TestClient(unittest.TestCase):
    def setUp(self):
        self.host = 'http://api.test.com'
        self.client = Client(host=self.host)
        if os.environ.get('TRAVIS'):
            Config(os.path.abspath(os.path.dirname(__file__)))
        else:
            local_path = '{0}/..'\
                .format(os.path.abspath(os.path.dirname(__file__)))
            Config(local_path)
        self.api_key = os.environ.get('SENDGRID_API_KEY')
        self.host = os.environ.get('MOCK_HOST')
        self.request_headers = {
                                 'Content-Type': 'application/json',
                                 'Authorization': 'Bearer ' + self.api_key
                                }
        self.client = Client(host=self.host,
                             request_headers=self.request_headers,
                             version=3)

    def test__init__(self):
        default_client = Client(host=self.host)
        self.assertEqual(default_client.host, self.host)
        self.assertEqual(default_client.request_headers, {})
        methods = ['delete', 'get', 'patch', 'post', 'put']
        self.assertEqual(default_client.methods, methods)
        self.assertEqual(default_client._version, None)
        self.assertEqual(default_client._url_path, [])

        request_headers = {'X-Test': 'test', 'X-Test2': 1}
        version = 3
        client = Client(host=self.host,
                        request_headers=request_headers,
                        version=version)
        self.assertEqual(client.host, self.host)
        self.assertEqual(client.request_headers, request_headers)
        methods = ['delete', 'get', 'patch', 'post', 'put']
        self.assertEqual(client.methods, methods)
        self.assertEqual(client._version, 3)
        self.assertEqual(client._url_path, [])

    def test__build_versioned_url(self):
        url = '/api_keys?hello=1&world=2'
        versioned_url = self.client._build_versioned_url(url)
        url = '{0}/v{1}{2}'.format(self.host, str(self.client._version), url)
        self.assertEqual(versioned_url, url)

    def test__build_url(self):
        self.client._url_path = self.client._url_path + ['here']
        self.client._url_path = self.client._url_path + ['there']
        self.client._url_path = self.client._url_path + [1]
        self.client._version = 3
        url = '{0}/v{1}{2}'.format(self.host,
                                   str(self.client._version),
                                   '/here/there/1?hello=0&world=1')
        query_params = {'hello': 0, 'world': 1}
        built_url = self.client._build_url(query_params)
        self.assertEqual(built_url, url)

    def test__update_headers(self):
        request_headers = {'X-Test': 'Test'}
        self.client._update_headers(request_headers)
        self.assertTrue('X-Test' in self.client.request_headers)
        self.client.request_headers.pop('X-Test', None)

    def test__build_client(self):
        new_client = self.client._build_client('test')
        self.assertTrue(new_client)

    def test__(self):
        self.assertEqual(self.client._url_path, [])
        client = self.client._('hello')
        url_path = ['hello']
        self.assertEqual(client._url_path[0], url_path[0])

    def test__getattr__(self):
        mock_client = MockClient(self.host, 200)
        client = mock_client.__getattr__('hello')
        url_path = ['hello']
        self.assertEqual(client._url_path, url_path)
        self.assertEqual(client.__getattr__('get').__name__, 'http_request')

        # Test Version
        client.version(3)
        self.assertEqual(client._version, 3)

        # Test GET
        mock_client._url_path+['test']
        r = mock_client.get()
        self.assertEqual(r.status_code, 200)

        # Test POST
        r = mock_client.put()
        self.assertEqual(r.status_code, 200)

        # Test PATCH
        r = mock_client.patch()
        self.assertEqual(r.status_code, 200)

        # Test POST
        mock_client.response_code = 201
        r = mock_client.post()
        self.assertEqual(r.status_code, 201)

        # Test DELETE
        mock_client.response_code = 204
        r = mock_client.delete()
        self.assertEqual(r.status_code, 204)
Esempio n. 5
0
class TestClient(unittest.TestCase):
    def setUp(self):
        self.host = 'http://api.test.com'
        self.client = Client(host=self.host)
        if os.environ.get('TRAVIS'):
            Config(os.path.abspath(os.path.dirname(__file__)))
        else:
            local_path = '{0}/..'\
                .format(os.path.abspath(os.path.dirname(__file__)))
            Config(local_path)
        self.api_key = os.environ.get('SENDGRID_API_KEY')
        self.host = os.environ.get('MOCK_HOST')
        self.request_headers = {
                                 'Content-Type': 'application/json',
                                 'Authorization': 'Bearer ' + self.api_key
                                }
        self.client = Client(host=self.host,
                             request_headers=self.request_headers,
                             version=3)

    def test__init__(self):
        default_client = Client(host=self.host)
        self.assertEqual(default_client.host, self.host)
        self.assertEqual(default_client.request_headers, None)
        methods = ['delete', 'get', 'patch', 'post', 'put']
        self.assertEqual(default_client.methods, methods)
        self.assertEqual(default_client._version, None)
        self.assertEqual(default_client._count, 0)
        self.assertEqual(default_client._url_path, {})
        self.assertEqual(default_client._status_code, None)
        self.assertEqual(default_client._response_body, None)
        self.assertEqual(default_client._response_headers, None)
        self.assertEqual(default_client._response, None)

        request_headers = {'X-Test': 'test', 'X-Test2': 1}
        version = 3
        client = Client(host=self.host,
                        request_headers=request_headers,
                        version=version)
        self.assertEqual(client.host, self.host)
        self.assertEqual(client.request_headers, request_headers)
        methods = ['delete', 'get', 'patch', 'post', 'put']
        self.assertEqual(client.methods, methods)
        self.assertEqual(client._version, 3)
        self.assertEqual(client._count, 0)
        self.assertEqual(client._url_path, {})
        self.assertEqual(client._status_code, None)
        self.assertEqual(client._response_body, None)
        self.assertEqual(client._response_headers, None)
        self.assertEqual(client._response, None)

    def test__reset(self):
        self.client._count = 1
        self.client._url_path = {0: 'test', 1: 'path'}
        self.client._response = 'fake_response_object'
        self.client._reset()
        self.assertEqual(self.client._count, 0)
        self.assertEqual(self.client._url_path, {})
        self.assertEqual(self.client._response, None)

    def test__add_to_url_path(self):
        self.client._add_to_url_path("here")
        self.client._add_to_url_path("there")
        self.client._add_to_url_path(str(1))
        self.assertEqual(self.client._count, 3)
        url_path = {0: 'here', 1: 'there', 2: '1'}
        self.assertEqual(self.client._url_path, url_path)
        self.client._reset()

    def test__build_versioned_url(self):
        url = '/api_keys?hello=1&world=2'
        versioned_url = self.client._build_versioned_url(url)
        url = '{0}/v{1}{2}'.format(self.host, str(self.client._version), url)
        self.assertEqual(versioned_url, url)

    def test__build_url(self):
        self.client._add_to_url_path('here')
        self.client._add_to_url_path('there')
        self.client._add_to_url_path(1)
        self.client._version = 3
        url = '{0}/v{1}{2}'.format(self.host,
                                   str(self.client._version),
                                   '/here/there/1?hello=0&world=1')
        query_params = {'hello': 0, 'world': 1}
        built_url = self.client._build_url(query_params)
        self.assertEqual(built_url, url)
        self.client._reset()

    def test__set_response(self):
        response = MockResponse(200)
        self.client._set_response(response)
        self.assertEqual(self.client._status_code, 200)
        self.assertEqual(self.client._response_body, 'RESPONSE BODY')
        self.assertEqual(self.client._response_headers, 'HEADERS')

    def test__set_headers(self):
        request_headers = {'X-Test': 'Test'}
        self.client._set_headers(request_headers)
        self.assertTrue('X-Test' in self.client.request_headers)
        self.client.request_headers.pop('X-Test', None)

    def test__(self):
        self.assertEqual(self.client._url_path, {})
        self.client._("hello")
        url_path = {0: 'hello'}
        self.assertEqual(self.client._url_path, url_path)
        self.client._reset()

    def test__getattr__(self):
        client = MockClient(self.host, 200)
        client.__getattr__('hello')
        url_path = {0: 'hello'}
        self.assertEqual(client._url_path, url_path)
        self.assertEqual(client.__getattr__('get').__name__,
                         'http_request')
        client._reset()

        # Test Version
        client.version(3)
        self.assertEqual(client._version, 3)

        # Test GET
        client._add_to_url_path('test')
        client.get()
        self.assertEqual(client.status_code, 200)

        # Test POST
        client.put()
        self.assertEqual(client.status_code, 200)

        # Test PATCH
        client.patch()
        self.assertEqual(client.status_code, 200)

        # Test POST
        client.response_code = 201
        client.post()
        self.assertEqual(client.status_code, 201)

        # Test DELETE
        client.response_code = 204
        client.delete()
        self.assertEqual(client.status_code, 204)