def test_internal_error(self): client = txbalanced.HTTPClient() with mock.patch('treq.request', return_value=self._create_deferred(code=500)): response = client.get('http://test.com') self.assertFailure(response, txbalanced.errors.InternalError) return response
def test_notauthorized_error(self): client = txbalanced.HTTPClient() with mock.patch('treq.request', return_value=self._create_deferred(code=401)): response = client.get('http://test.com') self.assertFailure(response, txbalanced.errors.NotAuthorizedError) return response
def test_methods(self): client = txbalanced.HTTPClient() for method in ('get', 'put', 'post', 'patch', 'delete'): caller = getattr(client, method) with mock.patch('treq.request', return_value=self._create_deferred()): yield caller('http://text.com') the_method, headers = treq.request.call_args self.assertEqual(the_method[0], method.upper())
def test_headers(self): client = txbalanced.HTTPClient() ret = self._create_deferred(content='') with mock.patch('treq.request', return_value=ret): headers = {'X-Test': 'X-Result'} response = yield client.get('customers/TESTESTESTESTEST', headers=headers) method, data = treq.request.call_args self.assertIn('X-Test', data['headers'])
def test_client_error(self): client = txbalanced.HTTPClient() with mock.patch( 'treq.request', return_value=self._create_deferred( code=409, content='{"description": "You did something wrong"}')): response = client.get('http://test.com') self.assertFailure(response, txbalanced.errors.ClientError) return response
def test_data_munge(self): client = txbalanced.HTTPClient() ret = self._create_deferred(content='') customer = txbalanced.Customer() customer.update(TEST_DATA['customer']) customer.name = 'Different Name' with mock.patch('treq.request', return_value=ret): response = yield client.put('/v1/test', data=customer) ref = json.dumps({'name': 'Different Name'}) self.assertEqual(treq.request.call_args[1]['data'], ref)
def test_url_munge(self): client = txbalanced.HTTPClient() urls = (u'/v1/customers/TESTESTESTESTEST', client.config.uri + '/customers/TESTESTESTESTEST', 'v1/customers/TESTESTESTESTEST', '/customers/TESTESTESTESTEST', 'customers/TESTESTESTESTEST') for url in urls: ret = self._create_deferred(content='') with mock.patch('treq.request', return_value=ret): response = yield client.get(url) method, uri = treq.request.call_args[0] self.assertTrue(uri.startswith(client.config.uri))
def test_deserialize(self): client = txbalanced.HTTPClient() content = '{"testkey": "testvalue"}' with mock.patch('treq.request', return_value=self._create_deferred(content=content)): response = yield client.get('http://test.com') self.assertIsInstance(response.content, dict) self.assertIn('testkey', response.content) self.assertEqual(response.content['testkey'], 'testvalue') with mock.patch('treq.request', return_value=self._create_deferred(content='')): response = yield client.get('http://test.com') self.assertIdentical(response.content, None)