def _botocore_parser_integration_test(service: str, action: str, method: str = None, request_uri: str = None, headers: dict = None, expected: dict = None, **kwargs): # Load the appropriate service service = load_service(service) # Use the serializer from botocore to serialize the request params serializer = create_serializer(service.protocol) serialized_request = serializer.serialize_to_request( kwargs, service.operation_model(action)) serialized_request["path"] = request_uri serialized_request["method"] = method serialized_request["headers"] = headers if service.protocol in ["query", "ec2"]: # Serialize the body as query parameter serialized_request["body"] = urlencode(serialized_request["body"]) # Use our parser to parse the serialized body parser = create_parser(service) operation_model, parsed_request = parser.parse(serialized_request) # Check if the result is equal to the given "expected" dict or the kwargs (if "expected" has not been set) assert parsed_request == (expected or kwargs)
def test_s3_virtual_host_addressing(): """Test the parsing of a map with the location trait 'headers'.""" request = HttpRequest( method="PUT", headers={"host": s3_utils.get_bucket_hostname("test-bucket")} ) parser = create_parser(load_service("s3")) parsed_operation_model, parsed_request = parser.parse(request) assert parsed_operation_model.name == "CreateBucket" assert "Bucket" in parsed_request assert parsed_request["Bucket"] == "test-bucket"
def __init__(self, service: ServiceModel, implementation: Union[Any, DispatchTable]): self.service = service self.parser = create_parser(service) self.serializer = create_serializer(service) if isinstance(implementation, dict): self.dispatch_table = implementation else: self.dispatch_table = create_dispatch_table(implementation)
def _botocore_parser_integration_test(service: str, action: str, headers: dict = None, expected: dict = None, **kwargs): # Load the appropriate service service = load_service(service) # Use the serializer from botocore to serialize the request params serializer = create_serializer(service.protocol) operation_model = service.operation_model(action) serialized_request = serializer.serialize_to_request( kwargs, operation_model) prepare_request_dict(serialized_request, "") split_url = urlsplit(serialized_request.get("url")) path = split_url.path query_string = split_url.query body = serialized_request["body"] # use custom headers (if provided), or headers from serialized request as default headers = serialized_request.get("headers") if headers is None else headers if service.protocol in ["query", "ec2"]: # Serialize the body as query parameter body = urlencode(serialized_request["body"]) # Use our parser to parse the serialized body parser = create_parser(service) parsed_operation_model, parsed_request = parser.parse( HttpRequest( method=serialized_request.get("method") or "GET", path=unquote(path), query_string=to_str(query_string), headers=headers, body=body, raw_path=path, )) # Check if the determined operation_model is correct assert parsed_operation_model == operation_model # Check if the result is equal to the given "expected" dict or the kwargs (if "expected" has not been set) expected = expected or kwargs # The parser adds None for none-existing members on purpose. Remove those for the assert expected = { key: value for key, value in expected.items() if value is not None } parsed_request = { key: value for key, value in parsed_request.items() if value is not None } assert parsed_request == expected
def test_restjson_operation_detection_with_subpath(): """ Tests if the operation lookup correctly fails for a subpath of an operation. For example: The detection of a URL which is routed through API Gateway. """ service = load_service("apigateway") parser = create_parser(service) with pytest.raises(OperationNotFoundParserError): parser.parse( HttpRequest( method="GET", path="/restapis/cmqinv79uh/local/_user_request_/", raw_path="/restapis/cmqinv79uh/local/_user_request_/", ))
def test_missing_required_field_restjson(self): parser = create_parser(load_service("opensearch")) op, params = parser.parse( HttpRequest( "POST", "/2021-01-01/tags", body='{"ARN":"somearn"}', )) with pytest.raises(MissingRequiredField) as e: validate_request(op, params).raise_first() assert e.value.error.reason == "missing required field" assert e.value.required_name == "TagList"
def test_missing_required_field_restxml(self): parser = create_parser(load_service("route53")) op, params = parser.parse( HttpRequest( "POST", "/2013-04-01/hostedzone", body= "<CreateHostedZoneRequest><Name>foobar.com</Name></CreateHostedZoneRequest>", )) with pytest.raises(MissingRequiredField) as e: validate_request(op, params).raise_first() assert e.value.error.reason == "missing required field" assert e.value.required_name == "CallerReference"
def test_invalid_length_query(self): parser = create_parser(load_service("sts")) op, params = parser.parse( HttpRequest( "POST", "/", body=urlencode( query={ "Action": "AssumeRole", "RoleArn": "arn:aws", # min=8 "RoleSessionName": "foobared", }), headers={"Content-Type": "application/x-www-form-urlencoded"}, )) with pytest.raises(InvalidLength) as e: validate_request(op, params).raise_first() e.match("RoleArn")
def test_invalid_range_query(self): parser = create_parser(load_service("sts")) op, params = parser.parse( HttpRequest( "POST", "/", body=urlencode( query={ "Action": "AssumeRole", "RoleArn": "arn:aws:iam::000000000000:role/foobared", "RoleSessionName": "foobared", "DurationSeconds": "100", }), headers={"Content-Type": "application/x-www-form-urlencoded"}, )) with pytest.raises(InvalidRange) as e: validate_request(op, params).raise_first() e.match("DurationSeconds")
def _botocore_parser_integration_test(service: str, action: str, headers: dict = None, expected: dict = None, **kwargs): # Load the appropriate service service = load_service(service) # Use the serializer from botocore to serialize the request params serializer = create_serializer(service.protocol) operation_model = service.operation_model(action) serialized_request = serializer.serialize_to_request( kwargs, operation_model) body = serialized_request["body"] query_string = urlencode(serialized_request.get("query_string") or "", doseq=False) # use custom headers (if provided), or headers from serialized request as default headers = serialized_request.get("headers") if headers is None else headers if service.protocol in ["query", "ec2"]: # Serialize the body as query parameter body = urlencode(serialized_request["body"]) # Use our parser to parse the serialized body parser = create_parser(service) parsed_operation_model, parsed_request = parser.parse( HttpRequest( method=serialized_request.get("method") or "GET", path=serialized_request.get("url_path") or "", query_string=query_string, headers=headers, body=body, )) # Check if the determined operation_model is correct assert parsed_operation_model == operation_model # Check if the result is equal to the given "expected" dict or the kwargs (if "expected" has not been set) assert parsed_request == (expected or kwargs)
def test_missing_required_field_query(self): parser = create_parser(load_service("sqs")) op, params = parser.parse( HttpRequest( "POST", "/", body= ("Action=SendMessage&Version=2012-11-05&" "QueueUrl=http%3A%2F%2Flocalhost%3A4566%2F000000000000%2Ftf-acc-test-queue&" ), headers={"Content-Type": "application/x-www-form-urlencoded"}, )) validator = ParamValidator() errors = validator.validate(params, op.input_shape) assert errors.has_errors() with pytest.raises(MissingRequiredField) as e: errors.raise_first() assert e.match("MessageBody") assert e.value.error.reason == "missing required field" assert e.value.required_name == "MessageBody"
from localstack import config from localstack.aws.api import CommonServiceException from localstack.aws.protocol.parser import OperationNotFoundParserError, create_parser from localstack.aws.protocol.serializer import create_serializer from localstack.aws.protocol.validate import MissingRequiredField, validate_request from localstack.aws.spec import load_service from localstack.http import Request, Response, Router, route from localstack.http.dispatcher import Handler from localstack.services.sqs.provider import MissingParameter from localstack.utils.aws import aws_stack from localstack.utils.aws.request_context import extract_region_from_headers LOG = logging.getLogger(__name__) service = load_service("sqs") parser = create_parser(service) serializer = create_serializer(service) @route( '/queue/<regex("[a-z0-9-]+"):region>/<regex("[0-9]{12}"):account_id>/<regex("[a-zA-Z0-9_-]+(.fifo)?"):queue_name>', methods=["POST", "GET"], ) def path_strategy_handler(request: Request, region, account_id: str, queue_name: str): return handle_request(request, region) @route( '/<regex("[0-9]{12}"):account_id>/<regex("[a-zA-Z0-9_-]+(.fifo)?"):queue_name>', host='<regex("([a-z0-9-]+\\.)?"):region>queue.localhost.localstack.cloud<regex("(:[0-9]{2,5})?"):port>', methods=["POST", "GET"],