def test_query_string_format(self, mock_request, mock_response): TestCases._setup_mocks(mock_request, mock_response, '{}') Http.request('http://www.example.org', args={'a': 1, 'b': 'a b c'}) parts = mock_request.call_args[0][0].replace('?', '&').split('&') self.assertEqual(parts[0], 'http://www.example.org') self.assertTrue('a=1' in parts[1:]) self.assertTrue('b=a+b+c' in parts[1:])
def test_raises_http_error(self, mock_request, mock_response): TestCases._setup_mocks(mock_request, mock_response, 'Not Found', 404) with self.assertRaises(Exception) as error: Http.request('http://www.example.org') e = error.exception self.assertEqual(e.status, 404) self.assertEqual(e.content, 'Not Found')
def test_supports_custom_content(self, mock_request, mock_response): TestCases._setup_mocks(mock_request, mock_response, '{}') headers = {'Content-Type': 'text/plain'} data = 'custom text' Http.request('http://www.example.org', data=data, headers=headers) self.assertEqual(mock_request.call_args[1]['body'], 'custom text') self.assertEqual(mock_request.call_args[1]['headers']['Content-Type'], 'text/plain')
def test_formats_json_request(self, mock_request, mock_response): TestCases._setup_mocks(mock_request, mock_response, '{}') data = {'abc': 123} Http.request('http://www.example.org', data=data) self.assertEqual(mock_request.call_args[1]['body'], '{"abc": 123}') self.assertEqual(mock_request.call_args[1]['headers']['Content-Type'], 'application/json')
def test_parses_json_response(self, mock_request, mock_response): TestCases._setup_mocks(mock_request, mock_response, '{"abc":123}') data = Http.request('http://www.example.org') self.assertEqual(data['abc'], 123)
def test_explicit_post_request_is_invoked(self, mock_request, mock_response): TestCases._setup_mocks(mock_request, mock_response, '{}') Http.request('http://www.example.org', method='POST') self.assertEqual(mock_request.call_args[1]['method'], 'POST')
def test_get_request_is_invoked(self, mock_request, mock_response): TestCases._setup_mocks(mock_request, mock_response, '{}') Http.request('http://www.example.org') self.assertEqual(mock_request.call_count, 1) self.assertEqual(mock_request.call_args[1]['method'], 'GET')
def test_query_string_format(self, mock_request, mock_response): TestCases._setup_mocks(mock_request, mock_response, '{}') Http.request('http://www.example.org', args={'a': 1, 'b': 'a b c'}) self.assertEqual(mock_request.call_args[0][0], 'http://www.example.org?a=1&b=a+b+c')