def test_body(self): client = HTTPClient('http://test/') request = PreparedRequest(Request('go')) client.prepare_request(request) with self.assertRaises(requests.exceptions.RequestException): client.send_message(request) self.assertEqual(request, request.prepped.body)
def test_string(self): # String should remain unchanged req = '{"jsonrpc": "1.0", "method": "foo", "id": 1}' self.assertEqual( '{"jsonrpc": "1.0", "method": "foo", "id": 1}', PreparedRequest(req) )
def test_list_of_strings(self): # List of strings should convert to one json-encoded string req = ['{"jsonrpc": "2.0", "method": "foo", "id": 1}', '{"jsonrpc": "2.0", "method": "foo", "id": 2}'] exp = '[{"jsonrpc": "2.0", "method": "foo", "id": 1}, {"jsonrpc": "2.0", "method": "foo", "id": 2}]' prepped = PreparedRequest(req) self.assertIsInstance(prepped, basestring) self.assertEqual(prepped, exp)
def test_ssl_verification(self): client = HTTPClient('https://test/') client.session.cert = '/path/to/cert' client.session.verify = 'ca-cert' request = PreparedRequest(Request('go')) client.prepare_request(request) with self.assertRaises(requests.exceptions.RequestException): client.send_message(request)
def test_ssl_verification(self): client = HTTPClient('https://test/') client.session.cert = '/path/to/cert' client.session.verify = 'ca-cert' request = PreparedRequest(Request('go')) client.prepare_request(request) with self.assertRaises(OSError): # Invalid certificate client.send_message(request)
def test_success_200(): client = HTTPClient('http://test/') request = PreparedRequest(Request('go')) client.prepare_request(request) responses.add(responses.POST, 'http://test/', status=200, body='{"jsonrpc": "2.0", "result": 5, "id": 1}') client.send_message(request)
def test_invalid_request(self): client = HTTPClient('http://test/') request = PreparedRequest(Request('go')) client.prepare_request(request) # Impossible to pass an invalid dict, so just assume the exception was raised responses.add(responses.POST, 'http://test/', status=400, body=requests.exceptions.InvalidSchema()) with self.assertRaises(requests.exceptions.InvalidSchema): client.send_message(request)
def test_list(self): # List should convert to json-encoded string req = [{ 'jsonrpc': '2.0', 'method': 'foo', 'id': 1 }, { 'jsonrpc': '2.0', 'method': 'foo', 'id': 2 }] self.assertIsInstance(PreparedRequest(req), basestring)
def test_send_custom_headers(self): client = HTTPClient('http://test/') client.session.headers['Content-Type'] = 'application/json-rpc' request = PreparedRequest(Request('go')) client.prepare_request(request) with self.assertRaises(requests.exceptions.RequestException): client.send_message(request) # Header set by argument self.assertEqual('application/json-rpc', request.prepped.headers['Content-Type']) # Header set by DEFAULT_HEADERS self.assertEqual('application/json', request.prepped.headers['Accept']) # Header set by Requests default_headers self.assertIn('Content-Length', request.prepped.headers)
def test_dict(self): # Dict should convert to json-encoded string req = {'jsonrpc': '1.0', 'method': 'foo', 'id': 1} self.assertIsInstance(PreparedRequest(req), basestring)
def test_connection_error(self): client = HTTPClient('http://test/') request = PreparedRequest(Request('go')) client.prepare_request(request) with self.assertRaises(requests.exceptions.RequestException): client.send_message(request)