Best Python code snippet using localstack_python
test_api_gateway.py
Source:test_api_gateway.py
...277 content = b"hello, please base64 encode me"278 def _mutate_data(data) -> None:279 data["return_is_base_64_encoded"] = True280 data["return_raw_body"] = base64.b64encode(content).decode("utf8")281 test_result = self._test_api_gateway_lambda_proxy_integration_no_asserts(282 self.TEST_LAMBDA_PROXY_BACKEND_WITH_IS_BASE64,283 self.API_PATH_LAMBDA_PROXY_BACKEND_WITH_IS_BASE64,284 data_mutator_fn=_mutate_data,285 )286 # Ensure that `invoke_rest_api_integration_backend` correctly decodes the base64 content287 self.assertEqual(test_result.result.status_code, 203)288 self.assertEqual(test_result.result.content, content)289 def _test_api_gateway_lambda_proxy_integration_no_asserts(290 self,291 fn_name: str,292 path: str,293 data_mutator_fn: Optional[Callable] = None,294 ) -> ApiGatewayLambdaProxyIntegrationTestResult:295 """296 Perform the setup needed to do a POST against a Lambda Proxy Integration;297 then execute the POST.298 :param data_mutator_fn: a Callable[[Dict], None] that lets us mutate the299 data dictionary before sending it off to the lambda.300 """301 self.create_lambda_function(fn_name)302 # create API Gateway and connect it to the Lambda proxy backend303 lambda_uri = aws_stack.lambda_function_arn(fn_name)304 invocation_uri = "arn:aws:apigateway:%s:lambda:path/2015-03-31/functions/%s/invocations"305 target_uri = invocation_uri % (aws_stack.get_region(), lambda_uri)306 result = testutil.connect_api_gateway_to_http_with_lambda_proxy(307 "test_gateway2", target_uri, path=path, stage_name=self.TEST_STAGE_NAME308 )309 api_id = result["id"]310 path_map = get_rest_api_paths(api_id)311 _, resource = get_resource_for_path(path, path_map)312 # make test request to gateway and check response313 path_with_replace = path.replace("{test_param1}", "foo1")314 path_with_params = path_with_replace + "?foo=foo&bar=bar&bar=baz"315 url = gateway_request_url(316 api_id=api_id, stage_name=self.TEST_STAGE_NAME, path=path_with_params317 )318 # These values get read in `lambda_integration.py`319 data = {"return_status_code": 203, "return_headers": {"foo": "bar123"}}320 if data_mutator_fn:321 assert callable(data_mutator_fn)322 data_mutator_fn(data)323 result = requests.post(324 url,325 data=json.dumps(data),326 headers={"User-Agent": "python-requests/testing"},327 )328 return ApiGatewayLambdaProxyIntegrationTestResult(329 data=data,330 resource=resource,331 result=result,332 url=url,333 path_with_replace=path_with_replace,334 )335 def _test_api_gateway_lambda_proxy_integration(336 self,337 fn_name: str,338 path: str,339 ) -> None:340 test_result = self._test_api_gateway_lambda_proxy_integration_no_asserts(fn_name, path)341 data, resource, result, url, path_with_replace = test_result342 self.assertEqual(result.status_code, 203)343 self.assertEqual(result.headers.get("foo"), "bar123")344 self.assertIn("set-cookie", result.headers)345 try:346 parsed_body = json.loads(to_str(result.content))347 except json.decoder.JSONDecodeError as e:348 raise Exception(349 "Couldn't json-decode content: {}".format(to_str(result.content))350 ) from e351 self.assertEqual(parsed_body.get("return_status_code"), 203)352 self.assertDictEqual(parsed_body.get("return_headers"), {"foo": "bar123"})353 self.assertDictEqual(354 parsed_body.get("queryStringParameters"),...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!