예제 #1
0
 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)
예제 #2
0
 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)
     )
예제 #3
0
 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)
예제 #4
0
 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)
예제 #5
0
 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)
예제 #6
0
 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)
예제 #7
0
 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)
예제 #9
0
 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)
예제 #10
0
 def test_dict(self):
     # Dict should convert to json-encoded string
     req = {'jsonrpc': '1.0', 'method': 'foo', 'id': 1}
     self.assertIsInstance(PreparedRequest(req), basestring)
예제 #11
0
 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)