def test_request_parsing(self): qs = ('Action=PutMetricAlarm&Version=2010-08-01&ComparisonOperator=GreaterThanOrEqualToThreshold&' + 'EvaluationPeriods=1&AlarmActions.member.1=test123&AlarmDescription=Upper+threshold+scaling+alarm&' + 'Metrics.member.1.Expression=e1%2F%28%281000%2A30%2A60%29%2F100%29&Metrics.member.1.Id=expr_1&' + 'Metrics.member.2.Expression=FILL%28m1%2C0%29&Metrics.member.2.Id=e1&' + 'Metrics.member.2.ReturnData=false&Metrics.member.3.Id=m1&' + 'Metrics.member.3.MetricStat.Metric.Dimensions.member.1.Name=StreamName&' + 'Metrics.member.3.MetricStat.Metric.Dimensions.member.1.Value=arn%3Aaws%3Akinesis%3A123&' + 'Metrics.member.3.MetricStat.Metric.MetricName=PutRecords.TotalRecords&' + 'Metrics.member.3.MetricStat.Metric.Namespace=AWS%2FKinesis&Metrics.member.3.MetricStat.Period=60&' + 'Metrics.member.3.MetricStat.Stat=Sum&Metrics.member.3.ReturnData=false&Threshold=80&' + 'AlarmName=mctesterson-application-tests-kds-fastpipe-stack-dataops-None-e8f05d1a') expected = [ {'Expression': 'e1/((1000*30*60)/100)', 'Id': 'expr_1'}, {'Expression': 'FILL(m1,0)', 'Id': 'e1', 'ReturnData': 'false'}, {'Id': 'm1', 'MetricStat': {'Metric': {'Dimensions.member': [{'Name': 'StreamName', 'Value': 'arn:aws:kinesis:123'}], 'MetricName': 'PutRecords.TotalRecords', 'Namespace': 'AWS/Kinesis'}, 'Period': '60', 'Stat': 'Sum'}, 'ReturnData': 'false'} ] response = BaseResponse() response.querystring = parse_qs(qs) result = response._get_multi_param('Metrics.member', skip_result_conversion=True) self.assertEqual(result, expected)
def parse_urlencoded_data(qs_data: Union[Dict, str, bytes], top_level_attribute: str, second_level_attribute: str = ""): # TODO: potentially find a better way than calling moto here... from moto.core.responses import BaseResponse if qs_data and isinstance(qs_data, dict): # make sure we're using the array form of query string dict here qs_data = { k: v if isinstance(v, list) else [v] for k, v in qs_data.items() } if isinstance(qs_data, (str, bytes)): qs_data = parse_qs(qs_data) response = BaseResponse() response.querystring = qs_data result = response._get_multi_param(top_level_attribute, skip_result_conversion=True) if second_level_attribute: for r in range(len(result)): second_level_result = response._get_multi_param( f"{top_level_attribute}.{r+1}.{second_level_attribute}", skip_result_conversion=True) inner_key = second_level_attribute.split(".")[ 0] # "MessageAttributes.entry".split('.')[0] if second_level_result: result[r][inner_key] = second_level_result return result
def test_request_parsing(self): qs = ( "Action=PutMetricAlarm&Version=2010-08-01&ComparisonOperator=GreaterThanOrEqualToThreshold&" + "EvaluationPeriods=1&AlarmActions.member.1=test123&AlarmDescription=Upper+threshold+scaling+alarm&" + "Metrics.member.1.Expression=e1%2F%28%281000%2A30%2A60%29%2F100%29&Metrics.member.1.Id=expr_1&" + "Metrics.member.2.Expression=FILL%28m1%2C0%29&Metrics.member.2.Id=e1&" + "Metrics.member.2.ReturnData=false&Metrics.member.3.Id=m1&" + "Metrics.member.3.MetricStat.Metric.Dimensions.member.1.Name=StreamName&" + "Metrics.member.3.MetricStat.Metric.Dimensions.member.1.Value=arn%3Aaws%3Akinesis%3A123&" + "Metrics.member.3.MetricStat.Metric.MetricName=PutRecords.TotalRecords&" + "Metrics.member.3.MetricStat.Metric.Namespace=AWS%2FKinesis&Metrics.member.3.MetricStat.Period=60&" + "Metrics.member.3.MetricStat.Stat=Sum&Metrics.member.3.ReturnData=false&Threshold=80&" + "AlarmName=mctesterson-application-tests-kds-fastpipe-stack-dataops-None-e8f05d1a" ) expected = [ { "Expression": "e1/((1000*30*60)/100)", "Id": "expr_1" }, { "Expression": "FILL(m1,0)", "Id": "e1", "ReturnData": "false" }, { "Id": "m1", "MetricStat": { "Metric": { "Dimensions.member": [{ "Name": "StreamName", "Value": "arn:aws:kinesis:123" }], "MetricName": "PutRecords.TotalRecords", "Namespace": "AWS/Kinesis", }, "Period": "60", "Stat": "Sum", }, "ReturnData": "false", }, ] response = BaseResponse() response.querystring = parse_qs(qs) result = response._get_multi_param("Metrics.member", skip_result_conversion=True) self.assertEqual(result, expected)
def parse_urlencoded_data(qs_data, top_level_attribute): # TODO: potentially find a better way than calling moto here... from moto.core.responses import BaseResponse if qs_data and isinstance(qs_data, dict): # make sure we're using the array form of query string dict here qs_data = {k: v if isinstance(v, list) else [v] for k, v in qs_data.items()} if isinstance(qs_data, (str, bytes)): qs_data = parse_qs(qs_data) response = BaseResponse() response.querystring = qs_data result = response._get_multi_param(top_level_attribute, skip_result_conversion=True) return result
def test_get_params(): subject = BaseResponse() subject.querystring = OrderedDict( [ ("Action", ["CreateRule"]), ("Version", ["2015-12-01"]), ( "ListenerArn", [ "arn:aws:elasticloadbalancing:us-east-1:1:listener/my-lb/50dc6c495c0c9188/80139731473870416" ], ), ("Priority", ["100"]), ("Conditions.member.1.Field", ["http-header"]), ("Conditions.member.1.HttpHeaderConfig.HttpHeaderName", ["User-Agent"]), ("Conditions.member.1.HttpHeaderConfig.Values.member.2", ["curl"]), ("Conditions.member.1.HttpHeaderConfig.Values.member.1", ["Mozilla"]), ("Actions.member.1.FixedResponseConfig.StatusCode", ["200"]), ("Actions.member.1.FixedResponseConfig.ContentType", ["text/plain"]), ("Actions.member.1.Type", ["fixed-response"]), ] ) result = subject._get_params() result.should.equal( { "Action": "CreateRule", "Version": "2015-12-01", "ListenerArn": "arn:aws:elasticloadbalancing:us-east-1:1:listener/my-lb/50dc6c495c0c9188/80139731473870416", "Priority": "100", "Conditions": [ { "Field": "http-header", "HttpHeaderConfig": { "HttpHeaderName": "User-Agent", "Values": ["Mozilla", "curl"], }, } ], "Actions": [ { "Type": "fixed-response", "FixedResponseConfig": { "StatusCode": "200", "ContentType": "text/plain", }, } ], } )
def test_get_dict_list_params(): subject = BaseResponse() subject.querystring = OrderedDict([ ("Action", ["CreateDBCluster"]), ("Version", ["2014-10-31"]), ("VpcSecurityGroupIds.VpcSecurityGroupId.1", ["sg-123"]), ("VpcSecurityGroupIds.VpcSecurityGroupId.2", ["sg-456"]), ("VpcSecurityGroupIds.VpcSecurityGroupId.3", ["sg-789"]), ]) # TODO: extend test and logic such that we can call subject._get_params() directly here result = subject._get_multi_param_dict("VpcSecurityGroupIds") result.should.equal({"VpcSecurityGroupId": ["sg-123", "sg-456", "sg-789"]})
def test_parse_qs_unicode_decode_error(): body = b'{"key": "%D0"}, "C": "#0 = :0"}' request = AWSPreparedRequest("GET", "http://request", {"foo": "bar"}, body, False) BaseResponse().setup_class(request, request.url, request.headers)
def test_parse_qs_unicode_decode_error(): body = b'{"key": "%D0"}, "C": "#0 = :0"}' request = AWSPreparedRequest('GET', 'http://request', {'foo': 'bar'}, body, False) BaseResponse().setup_class(request, request.url, request.headers)