Skip to content

Commit

Permalink
Rework query string value constraint test
Browse files Browse the repository at this point in the history
Query strings specified without an associated value map to empty string, not
null. This means that they satisfy required constraints, and length constraints
must be used to enforce that a value is specified when they have dynamic
values.
  • Loading branch information
adamthom-amzn committed Oct 11, 2021
1 parent 2bd986c commit d8fdf44
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ service RestJsonValidation {
MalformedEnum,
MalformedLength,
MalformedLengthOverride,
MalformedLengthQueryString,
MalformedPattern,
MalformedPatternOverride,
MalformedRange,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ operation MalformedLengthOverride {
errors: [ValidationException]
}

@http(uri: "/MalformedLengthQueryString", method: "POST")
operation MalformedLengthQueryString {
input: MalformedLengthQueryStringInput,
errors: [ValidationException]
}

apply MalformedLength @httpMalformedRequestTests([
{
id: "RestJsonMalformedLengthBlob",
Expand Down Expand Up @@ -542,6 +548,43 @@ apply MalformedLengthOverride @httpMalformedRequestTests([
}
])

// query strings that have unspecified value are treated as being an empty string
// which means length validation is as important as required validation for ensuring a specified value
apply MalformedLengthQueryString @httpMalformedRequestTests([
{
id: "RestJsonMalformedLengthQueryStringNoValue",
documentation: """
When a required member has no value in the query string,
the response should be a 400 ValidationException.""",
protocol: restJson1,
request: {
method: "POST",
uri: "/MalformedLengthQueryString",
body: "{}",
queryParams: [
"string"
],
headers: {
"content-type": "application/json"
},
},
response: {
code: 400,
headers: {
"x-amzn-errortype": "ValidationException"
},
body: {
mediaType: "application/json",
assertion: {
contents: """
{ "message" : "1 validation error detected. Value with length 0 at '/string' failed to satisfy constraint: Member must have length between 2 and 8, inclusive",
"fieldList" : [{"message": "Value with length 0 at '/string' failed to satisfy constraint: Member must have length between 2 and 8, inclusive", "path": "/string"}]}"""
}
}
}
},
])

structure MalformedLengthInput {
blob: LengthBlob,
string: LengthString,
Expand Down Expand Up @@ -571,6 +614,11 @@ structure MalformedLengthOverrideInput {
map: LengthMap
}

structure MalformedLengthQueryStringInput {
@httpQuery("string")
string: LengthString
}

@length(min:2, max:8)
blob LengthBlob

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,40 +82,6 @@ apply MalformedRequired @httpMalformedRequestTests([
}
}
},
{
id: "RestJsonMalformedRequiredQueryNoValue",
documentation: """
When a required member has no value in the query string,
the response should be a 400 ValidationException.""",
protocol: restJson1,
request: {
method: "POST",
uri: "/MalformedRequired",
body: """
{ "string": "abc" }""",
queryParams: [
"stringInQuery"
],
headers: {
"content-type": "application/json",
"string-in-headers": "abc"
},
},
response: {
code: 400,
headers: {
"x-amzn-errortype": "ValidationException"
},
body: {
mediaType: "application/json",
assertion: {
contents: """
{ "message" : "1 validation error detected. Value null at '/string' failed to satisfy constraint: Member must not be null",
"fieldList" : [{"message": "Value null at '/string' failed to satisfy constraint: Member must not be null", "path": "/string"}]}"""
}
}
}
},
{
id: "RestJsonMalformedRequiredHeaderUnset",
documentation: """
Expand Down

0 comments on commit d8fdf44

Please sign in to comment.