def test_add_query_string_from_original_request(self, mock_get): with application.test_request_context("/v2/apps?a=b&c=d", method="GET"): replay_request(flask.request) self.assertTrue(mock_get.called) called_headers = mock_get.call_args[1]['params'] self.assertEqual(dict(called_headers), {"a": "b", "c": "d"})
def test_add_original_payload_to_upstream_request(self, mock_get): with application.test_request_context("/v2/apps?a=b&c=d", method="GET", data="Request Data"): replay_request(flask.request) self.assertTrue(mock_get.called) called_headers = mock_get.call_args[1]['data'] self.assertEqual(called_headers, b"Request Data")
def test_remove_conent_length_header(self, mock_get): with application.test_request_context("/v2/apps", method="GET", headers={"Content-Length": 42}): replay_request(flask.request) self.assertTrue(mock_get.called) called_headers = mock_get.call_args[1]['headers'] self.assertTrue('Content-Length' not in called_headers)
def test_remove_conent_length_header_mixed_case(self, mock_get): with application.test_request_context("/v2/apps", method="GET", headers={"COntenT-LenGtH": 42}): replay_request(flask.request) self.assertTrue(mock_get.called) called_headers = mock_get.call_args[1]['headers'] header_names = [k.lower() for k in called_headers.keys()] self.assertTrue('content-length' not in called_headers)
def test_original_headers_to_upstream_request(self, mock_get): with application.test_request_context("/v2/apps", method="GET", headers={ "X-Header-A": 42, "X-Header-B": 10 }): replay_request(flask.request) self.assertTrue(mock_get.called) called_headers = mock_get.call_args[1]['headers'] self.assertEqual(called_headers['X-Header-A'], "42") self.assertEqual(called_headers['X-Header-B'], "10")
def test_no_not_attempt_to_parse_a_non_json_body_put(self, mock_put): """ We must remove these keys: * version * fetch When GETting an app, Marathon returns a JSON with these two keys, bus refuses to accept a PUT/POST on this same app if these keys are present. """ with application.test_request_context("/v2/apps//foo/bar/restart", method="PUT", data=''): replay_request(flask.request) self.assertTrue(mock_put.called)
def upstream_request(request: HollowmanRequest) -> Response: resp = upstream.replay_request(request) return Response( response=resp.content, status=resp.status_code, headers=dict(resp.headers), )
def test_remove_some_key_before_replay_put_request_data_is_a_list( self, mock_put): """ A API aceita uma lista se o request for um PUT em /v2/apps. O pop() da lista se comporta diferente do pop do dict. Temos que tratar isso. """ with application.test_request_context( "/v2/apps", method="PUT", data='[{"id": "/abc", "version": "0", "fetch": ["a", "b"]}]', headers={'Content-Type': 'application/json'}): replay_request(flask.request) self.assertTrue(mock_put.called) called_data_json = json.loads(mock_put.call_args[1]['data']) self.assertFalse('version' in called_data_json[0]) self.assertFalse('fetch' in called_data_json[0])
def test_remove_some_key_before_replay_put_request(self, mock_put): """ We must remove these keys: * version * fetch When GETting an app, Marathon returns a JSON with these two keys, bus refuses to accept a PUT/POST on this same app if these keys are present. """ with application.test_request_context( "/v2/apps", method="PUT", data='{"id": "/abc", "version": "0", "fetch": ["a", "b"]}', headers={'Content-Type': 'application/json'}): replay_request(flask.request) self.assertTrue(mock_put.called) called_data_json = json.loads(mock_put.call_args[1]['data']) self.assertFalse('version' in called_data_json) self.assertFalse('fetch' in called_data_json)
def test_remove_some_key_before_replay_post_request(self, mock_post): """ We must remove these keys: * version * fetch When GETting an app, Marathon returns a JSON with these two keys, bus refuses to accept a PUT/POST on this same app if these keys are present. """ with application.test_request_context( "/v2/apps", method="POST", data= '{"id": "/abc", "version": "0", "fetch": ["a", "b"], "secrets": {}}', headers={"Content-Type": "application/json"}, ): # flask.request.is_json = True replay_request(flask.request) self.assertTrue(mock_post.called) called_data_json = json.loads(mock_post.call_args[1]["data"]) self.assertFalse("version" in called_data_json) self.assertFalse("fetch" in called_data_json) self.assertFalse("secrets" in called_data_json)
def test_replay_request_removes_specific_headers_from_upstream_response( self, mock_get): HEADER_NAME_CONTENT_ENCODING = "Content-Encoding" HEADER_NAME_TRANSFER_ENCODING = "Transfer-Encoding" mock_get.return_value = RequestStub( headers={ HEADER_NAME_CONTENT_ENCODING: "gzip", HEADER_NAME_TRANSFER_ENCODING: "chunked" }) with application.test_request_context("/v2/apps", method="GET"): response = replay_request(flask.request) self.assertFalse( HEADER_NAME_CONTENT_ENCODING in response.headers.keys()) self.assertFalse( HEADER_NAME_TRANSFER_ENCODING in response.headers.keys())
def test_add_authorization_header(self, mock_get): with application.test_request_context("/v2/apps", method="GET"): replay_request(flask.request) self.assertTrue(mock_get.called) called_headers = mock_get.call_args[1]['headers']['Authorization'] self.assertEqual(called_headers, "bla")
def test_no_not_attempt_to_parse_a_non_json_body_put(self, mock_put): with application.test_request_context("/v2/apps//foo/bar/restart", method="PUT", data=''): replay_request(flask.request) self.assertTrue(mock_put.called)
def raw_proxy(): r = upstream.replay_request(request) return Response(response=r.content, status=r.status_code, headers=dict(r.headers))
def test_remove_transfer_encoding_header(self): with application.test_request_context("/v2/apps", method="GET") as ctx: response = replay_request(ctx.request) self.assertTrue("Content-Encoding" not in response.headers) self.assertEqual(200, response.status_code)