def test_restclient_has_delete_shortcut(self): client = ByteRESTClient() client.request = mock.MagicMock(return_value=42) ret = client.delete("/delete/") client.request.assert_called_once_with('delete', "/delete/") self.assertEqual(ret, 42) # returned client.request return value
def test_restclient_has_patch_shortcut(self): client = ByteRESTClient() client.request = mock.MagicMock(return_value=42) ret = client.patch("/patch/") client.request.assert_called_once_with('patch', "/patch/") self.assertEqual(ret, 42) # returned client.request return value
def test_restclient_has_post_shortcut(self): client = ByteRESTClient() client.request = mock.MagicMock(return_value=42) ret = client.post("/post/", data={"a": 1, "b": 2}) client.request.assert_called_once_with('post', "/post/", data={"a": 1, "b": 2}) self.assertEqual(ret, 42) # returned client.request return value
def test_restclient_request_makes_correct_call_using_requests(self): client = ByteRESTClient() client.request('get', '/', data={"a": "b"}) self.mock_get.assert_called_once_with( REST_CLIENT_ENDPOINT + "/", data=json.dumps({"a": "b"}), headers=client.headers, allow_redirects=False )
def test_restclient_appends_path_to_url(self): client = ByteRESTClient() client.request('get', '/varnish/v2/config/henkslaaf.nl') self.mock_get.assert_called_once_with( REST_CLIENT_ENDPOINT + "/varnish/v2/config/henkslaaf.nl", data='{}', headers=client.headers, allow_redirects=False )
def test_restclient_request_honours_given_method_name(self): client = ByteRESTClient() client.request('post', '/', data={"a": "b"}) self.assertEqual(self.mock_get.call_count, 0) # get is not called, because post is requested self.mock_post.assert_called_once_with( REST_CLIENT_ENDPOINT + "/", data=json.dumps({"a": "b"}), headers=client.headers, allow_redirects=False )
def test_restclient_corrects_missing_slashes_in_urls(self): client = ByteRESTClient(endpoint="http://henk.nl/api") client.headers = {} client.get("hypernode/") self.mock_get.assert_called_once_with( "http://henk.nl/api/hypernode/", data='{}', headers={}, allow_redirects=False )
def test_restclient_passes_extra_parameters_to_requests(self): client = ByteRESTClient(endpoint='http://henk.nl/api') client.headers = {} client.get('hypernode', params={"q": "mynode"}) self.mock_get.assert_called_once_with( "http://henk.nl/api/hypernode", headers={}, params={"q": "mynode"}, data='{}', allow_redirects=False )
def test_that_format_absolute_url_works_correctly_if_path_has_query_params_and_fragments(self): client = ByteRESTClient(endpoint="http://*****:*****@aap.nl:8080/mies/") absolute_url = client.format_absolute_url("/henk;zus?aap=noot#noot") self.assertEqual(absolute_url, "http://*****:*****@aap.nl:8080/mies/henk;zus?aap=noot#noot")
def test_that_format_absolute_url_accepts_empty_paths(self): client = ByteRESTClient(endpoint="http://henk.nl/api/") absolute_url = client.format_absolute_url("") self.assertEqual(absolute_url, "http://henk.nl/api/")
def test_that_format_absolute_url_corrects_double_slashes_in_path(self): client = ByteRESTClient(endpoint="http://henk.nl/api/") absolute_url = client.format_absolute_url("/web1.c79//ips") self.assertEqual(absolute_url, "http://henk.nl/api/web1.c79/ips")
def test_that_format_absolute_url_removes_double_slashes_between_endpoint_and_path(self): client = ByteRESTClient(endpoint="http://henk.nl/api/") absolute_url = client.format_absolute_url("/foo/bar") self.assertEqual(absolute_url, "http://henk.nl/api/foo/bar")
def test_that_format_absolute_url_adds_a_slash_between_endpoint_and_path(self): client = ByteRESTClient(endpoint="http://henk.nl/api") absolute_url = client.format_absolute_url("foo/bar") self.assertEqual(absolute_url, "http://henk.nl/api/foo/bar")
def test_restclient_raises_HTTPError_when_request_is_redirect(self): self.mock_response.status_code = 302 client = ByteRESTClient() with self.assertRaises(HTTPError): client.request('get', '/get/', data={"a": "b"})
def test_restclient_returns_none_when_status_is_204(self): self.mock_response.status_code = 204 client = ByteRESTClient() ret = client.request('get', '/get/', data={"a": "b"}) self.assertIsNone(ret)
def test_that_format_absolute_url_concatenates_a_path_to_the_endpoint(self): client = ByteRESTClient(endpoint="http://henk.nl/api/") absolute_url = client.format_absolute_url("foo/bar") self.assertEqual(absolute_url, "http://henk.nl/api/foo/bar")
def test_restclient_request_returns_decoded_json_response(self): client = ByteRESTClient() ret = client.request('get', '/get/', data={"a": "b"}) self.assertEqual(ret, {"b": "a"})