Skip to content

Commit

Permalink
internal/protocol: Add support for order agnostic JSON, QueryString, …
Browse files Browse the repository at this point in the history
…and XML test cases.

The protocol tests no longer will relay on order of the elements within the encoded string. The strings will be parsed in their associated format and their content compared.
  • Loading branch information
jasdel committed Oct 15, 2015
1 parent 927a212 commit 0523f96
Show file tree
Hide file tree
Showing 18 changed files with 638 additions and 267 deletions.
41 changes: 36 additions & 5 deletions internal/fixtures/protocol/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var _ json.Marshaler
var _ time.Time
var _ xmlutil.XMLNode
var _ xml.Attr
var _ utilassert.Imported
var _ = ioutil.Discard
var _ = util.Trim("")
var _ = url.Values{}
Expand Down Expand Up @@ -76,6 +77,7 @@ var extraImports = []string{
"",
"github.com/aws/aws-sdk-go/internal/protocol/xml/xmlutil",
"github.com/aws/aws-sdk-go/internal/util",
"github.com/aws/aws-sdk-go/internal/util/utilassert",
"github.com/stretchr/testify/assert",
}

Expand Down Expand Up @@ -138,16 +140,45 @@ type tplInputTestCaseData struct {
}

func (t tplInputTestCaseData) BodyAssertions() string {
protocol, code := t.TestCase.TestSuite.API.Metadata.Protocol, ""
code := &bytes.Buffer{}
protocol := t.TestCase.TestSuite.API.Metadata.Protocol

// Extract the body bytes
switch protocol {
case "rest-xml":
fmt.Fprintln(code, "body := util.SortXML(r.Body)")
default:
fmt.Fprintln(code, "body, _ := ioutil.ReadAll(r.Body)")
}

// Generate the body verification code
expectedBody := util.Trim(t.TestCase.InputTest.Body)
switch protocol {
case "ec2", "query":
fmt.Fprintf(code, "utilassert.AssertQuery(t, `%s`, util.Trim(string(body)))",
expectedBody)
case "rest-xml":
code += "body := util.SortXML(r.Body)\n"
if strings.HasPrefix(expectedBody, "<") {
fmt.Fprintf(code, "utilassert.AssertXML(t, `%s`, util.Trim(string(body)), %s{})",
expectedBody, t.TestCase.Given.InputRef.ShapeName)
} else {
fmt.Fprintf(code, "assert.Equal(t, `%s`, util.Trim(string(body)))",
expectedBody)
}
case "json", "jsonrpc", "rest-json":
if strings.HasPrefix(expectedBody, "{") {
fmt.Fprintf(code, "utilassert.AssertJSON(t, `%s`, util.Trim(string(body)))",
expectedBody)
} else {
fmt.Fprintf(code, "assert.Equal(t, `%s`, util.Trim(string(body)))",
expectedBody)
}
default:
code += "body, _ := ioutil.ReadAll(r.Body)\n"
fmt.Fprintf(code, "assert.Equal(t, `%s`, util.Trim(string(body)))",
expectedBody)
}

code += "assert.Equal(t, util.Trim(`" + t.TestCase.InputTest.Body + "`), util.Trim(string(body)))"
return code
return code.String()
}

var tplOutputTestCase = template.Must(template.New("outputcase").Parse(`
Expand Down
2 changes: 1 addition & 1 deletion internal/fixtures/protocol/input/json.json
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@
"X-Amz-Target": "com.amazonaws.foo.OperationName",
"Content-Type": "application/x-amz-json-1.1"
},
"body": "{\"RecursiveStruct\": {\"RecursiveMap\": {\"bar\": {\"NoRecurse\": \"bar\"}, \"foo\": {\"NoRecurse\": \"foo\"}}}}"
"body": "{\"RecursiveStruct\": {\"RecursiveMap\": {\"foo\": {\"NoRecurse\": \"foo\"}, \"bar\": {\"NoRecurse\": \"bar\"}}}}"
}
}
]
Expand Down
53 changes: 52 additions & 1 deletion internal/fixtures/protocol/input/query.json
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,57 @@
}
]
},
{
"description": "Serialize map type with locationName",
"metadata": {
"protocol": "query",
"apiVersion": "2014-01-01"
},
"shapes": {
"InputShape": {
"type": "structure",
"members": {
"MapArg": {
"shape": "StringMap"
}
}
},
"StringMap": {
"type": "map",
"key": {
"shape": "StringType",
"locationName": "TheKey"
},
"value": {
"shape": "StringType",
"locationName": "TheValue"
}
},
"StringType": {
"type": "string"
}
},
"cases": [
{
"given": {
"input": {
"shape": "InputShape"
},
"name": "OperationName"
},
"params": {
"MapArg": {
"key1": "val1",
"key2": "val2"
}
},
"serialized": {
"uri": "/",
"body": "Action=OperationName&Version=2014-01-01&MapArg.entry.1.TheKey=key1&MapArg.entry.1.TheValue=val1&MapArg.entry.2.TheKey=key2&MapArg.entry.2.TheValue=val2"
}
}
]
},
{
"description": "Base64 encoded Blobs",
"metadata": {
Expand Down Expand Up @@ -544,7 +595,7 @@
},
"serialized": {
"uri": "/",
"body": "Action=OperationName&Version=2014-01-01&RecursiveStruct.RecursiveMap.entry.1.key=bar&RecursiveStruct.RecursiveMap.entry.1.value.NoRecurse=bar&RecursiveStruct.RecursiveMap.entry.2.key=foo&RecursiveStruct.RecursiveMap.entry.2.value.NoRecurse=foo"
"body": "Action=OperationName&Version=2014-01-01&RecursiveStruct.RecursiveMap.entry.1.key=foo&RecursiveStruct.RecursiveMap.entry.1.value.NoRecurse=foo&RecursiveStruct.RecursiveMap.entry.2.key=bar&RecursiveStruct.RecursiveMap.entry.2.value.NoRecurse=bar"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion internal/fixtures/protocol/input/rest-json.json
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@
"serialized": {
"uri": "/path",
"headers": {},
"body": "{\"RecursiveStruct\": {\"RecursiveMap\": {\"bar\": {\"NoRecurse\": \"bar\"}, \"foo\": {\"NoRecurse\": \"foo\"}}}}"
"body": "{\"RecursiveStruct\": {\"RecursiveMap\": {\"foo\": {\"NoRecurse\": \"foo\"}, \"bar\": {\"NoRecurse\": \"bar\"}}}}"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion internal/fixtures/protocol/input/rest-xml.json
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,7 @@
},
"serialized": {
"uri": "/path",
"body": "<OperationRequest xmlns=\"https://foo/\"><RecursiveStruct><RecursiveMap><entry><key>bar</key><value><NoRecurse>bar</NoRecurse></value></entry><entry><key>foo</key><value><NoRecurse>foo</NoRecurse></value></entry></RecursiveMap></RecursiveStruct></OperationRequest>"
"body": "<OperationRequest xmlns=\"https://foo/\"><RecursiveStruct><RecursiveMap><entry><key>foo</key><value><NoRecurse>foo</NoRecurse></value></entry><entry><key>bar</key><value><NoRecurse>bar</NoRecurse></value></entry></RecursiveMap></RecursiveStruct></OperationRequest>"
}
}
]
Expand Down
18 changes: 10 additions & 8 deletions internal/protocol/ec2query/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/aws/aws-sdk-go/internal/protocol/xml/xmlutil"
"github.com/aws/aws-sdk-go/internal/signer/v4"
"github.com/aws/aws-sdk-go/internal/util"
"github.com/aws/aws-sdk-go/internal/util/utilassert"
"github.com/stretchr/testify/assert"
)

Expand All @@ -29,6 +30,7 @@ var _ json.Marshaler
var _ time.Time
var _ xmlutil.XMLNode
var _ xml.Attr
var _ utilassert.Imported
var _ = ioutil.Discard
var _ = util.Trim("")
var _ = url.Values{}
Expand Down Expand Up @@ -672,7 +674,7 @@ func TestInputService1ProtocolTestScalarMembersCase1(t *testing.T) {
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`Action=OperationName&Bar=val2&Foo=val1&Version=2014-01-01`), util.Trim(string(body)))
utilassert.AssertQuery(t, `Action=OperationName&Bar=val2&Foo=val1&Version=2014-01-01`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand Down Expand Up @@ -700,7 +702,7 @@ func TestInputService2ProtocolTestStructureWithLocationNameAndQueryNameAppliedTo
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`Action=OperationName&BarLocationName=val2&Foo=val1&Version=2014-01-01&yuckQueryName=val3`), util.Trim(string(body)))
utilassert.AssertQuery(t, `Action=OperationName&BarLocationName=val2&Foo=val1&Version=2014-01-01&yuckQueryName=val3`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand Down Expand Up @@ -728,7 +730,7 @@ func TestInputService3ProtocolTestNestedStructureMembersCase1(t *testing.T) {
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`Action=OperationName&Struct.Scalar=foo&Version=2014-01-01`), util.Trim(string(body)))
utilassert.AssertQuery(t, `Action=OperationName&Struct.Scalar=foo&Version=2014-01-01`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand Down Expand Up @@ -758,7 +760,7 @@ func TestInputService4ProtocolTestListTypesCase1(t *testing.T) {
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`Action=OperationName&ListArg.1=foo&ListArg.2=bar&ListArg.3=baz&Version=2014-01-01`), util.Trim(string(body)))
utilassert.AssertQuery(t, `Action=OperationName&ListArg.1=foo&ListArg.2=bar&ListArg.3=baz&Version=2014-01-01`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand Down Expand Up @@ -788,7 +790,7 @@ func TestInputService5ProtocolTestListWithLocationNameAppliedToMemberCase1(t *te
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`Action=OperationName&ListMemberName.1=a&ListMemberName.2=b&ListMemberName.3=c&Version=2014-01-01`), util.Trim(string(body)))
utilassert.AssertQuery(t, `Action=OperationName&ListMemberName.1=a&ListMemberName.2=b&ListMemberName.3=c&Version=2014-01-01`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand Down Expand Up @@ -818,7 +820,7 @@ func TestInputService6ProtocolTestListWithLocationNameAndQueryNameCase1(t *testi
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`Action=OperationName&ListQueryName.1=a&ListQueryName.2=b&ListQueryName.3=c&Version=2014-01-01`), util.Trim(string(body)))
utilassert.AssertQuery(t, `Action=OperationName&ListQueryName.1=a&ListQueryName.2=b&ListQueryName.3=c&Version=2014-01-01`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand All @@ -844,7 +846,7 @@ func TestInputService7ProtocolTestBase64EncodedBlobsCase1(t *testing.T) {
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`Action=OperationName&BlobArg=Zm9v&Version=2014-01-01`), util.Trim(string(body)))
utilassert.AssertQuery(t, `Action=OperationName&BlobArg=Zm9v&Version=2014-01-01`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand All @@ -870,7 +872,7 @@ func TestInputService8ProtocolTestTimestampValuesCase1(t *testing.T) {
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`Action=OperationName&TimeArg=2015-01-25T08%3A00%3A00Z&Version=2014-01-01`), util.Trim(string(body)))
utilassert.AssertQuery(t, `Action=OperationName&TimeArg=2015-01-25T08%3A00%3A00Z&Version=2014-01-01`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand Down
2 changes: 2 additions & 0 deletions internal/protocol/ec2query/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/aws/aws-sdk-go/internal/protocol/xml/xmlutil"
"github.com/aws/aws-sdk-go/internal/signer/v4"
"github.com/aws/aws-sdk-go/internal/util"
"github.com/aws/aws-sdk-go/internal/util/utilassert"
"github.com/stretchr/testify/assert"
)

Expand All @@ -29,6 +30,7 @@ var _ json.Marshaler
var _ time.Time
var _ xmlutil.XMLNode
var _ xml.Attr
var _ utilassert.Imported
var _ = ioutil.Discard
var _ = util.Trim("")
var _ = url.Values{}
Expand Down
26 changes: 14 additions & 12 deletions internal/protocol/jsonrpc/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/aws/aws-sdk-go/internal/protocol/xml/xmlutil"
"github.com/aws/aws-sdk-go/internal/signer/v4"
"github.com/aws/aws-sdk-go/internal/util"
"github.com/aws/aws-sdk-go/internal/util/utilassert"
"github.com/stretchr/testify/assert"
)

Expand All @@ -29,6 +30,7 @@ var _ json.Marshaler
var _ time.Time
var _ xmlutil.XMLNode
var _ xml.Attr
var _ utilassert.Imported
var _ = ioutil.Discard
var _ = util.Trim("")
var _ = url.Values{}
Expand Down Expand Up @@ -730,7 +732,7 @@ func TestInputService1ProtocolTestScalarMembersCase1(t *testing.T) {
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`{"Name":"myname"}`), util.Trim(string(body)))
utilassert.AssertJSON(t, `{"Name":"myname"}`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand Down Expand Up @@ -758,7 +760,7 @@ func TestInputService2ProtocolTestTimestampValuesCase1(t *testing.T) {
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`{"TimeArg":1422172800}`), util.Trim(string(body)))
utilassert.AssertJSON(t, `{"TimeArg":1422172800}`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand Down Expand Up @@ -786,7 +788,7 @@ func TestInputService3ProtocolTestBase64EncodedBlobsCase1(t *testing.T) {
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`{"BlobArg":"Zm9v"}`), util.Trim(string(body)))
utilassert.AssertJSON(t, `{"BlobArg":"Zm9v"}`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand Down Expand Up @@ -817,7 +819,7 @@ func TestInputService3ProtocolTestBase64EncodedBlobsCase2(t *testing.T) {
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`{"BlobMap":{"key1":"Zm9v","key2":"YmFy"}}`), util.Trim(string(body)))
utilassert.AssertJSON(t, `{"BlobMap":{"key1":"Zm9v","key2":"YmFy"}}`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand Down Expand Up @@ -848,7 +850,7 @@ func TestInputService4ProtocolTestNestedBlobsCase1(t *testing.T) {
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`{"ListParam":["Zm9v","YmFy"]}`), util.Trim(string(body)))
utilassert.AssertJSON(t, `{"ListParam":["Zm9v","YmFy"]}`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand Down Expand Up @@ -878,7 +880,7 @@ func TestInputService5ProtocolTestRecursiveShapesCase1(t *testing.T) {
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`{"RecursiveStruct":{"NoRecurse":"foo"}}`), util.Trim(string(body)))
utilassert.AssertJSON(t, `{"RecursiveStruct":{"NoRecurse":"foo"}}`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand Down Expand Up @@ -910,7 +912,7 @@ func TestInputService5ProtocolTestRecursiveShapesCase2(t *testing.T) {
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`{"RecursiveStruct":{"RecursiveStruct":{"NoRecurse":"foo"}}}`), util.Trim(string(body)))
utilassert.AssertJSON(t, `{"RecursiveStruct":{"RecursiveStruct":{"NoRecurse":"foo"}}}`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand Down Expand Up @@ -946,7 +948,7 @@ func TestInputService5ProtocolTestRecursiveShapesCase3(t *testing.T) {
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`{"RecursiveStruct":{"RecursiveStruct":{"RecursiveStruct":{"RecursiveStruct":{"NoRecurse":"foo"}}}}}`), util.Trim(string(body)))
utilassert.AssertJSON(t, `{"RecursiveStruct":{"RecursiveStruct":{"RecursiveStruct":{"RecursiveStruct":{"NoRecurse":"foo"}}}}}`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand Down Expand Up @@ -983,7 +985,7 @@ func TestInputService5ProtocolTestRecursiveShapesCase4(t *testing.T) {
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`{"RecursiveStruct":{"RecursiveList":[{"NoRecurse":"foo"},{"NoRecurse":"bar"}]}}`), util.Trim(string(body)))
utilassert.AssertJSON(t, `{"RecursiveStruct":{"RecursiveList":[{"NoRecurse":"foo"},{"NoRecurse":"bar"}]}}`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand Down Expand Up @@ -1022,7 +1024,7 @@ func TestInputService5ProtocolTestRecursiveShapesCase5(t *testing.T) {
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`{"RecursiveStruct":{"RecursiveList":[{"NoRecurse":"foo"},{"RecursiveStruct":{"NoRecurse":"bar"}}]}}`), util.Trim(string(body)))
utilassert.AssertJSON(t, `{"RecursiveStruct":{"RecursiveList":[{"NoRecurse":"foo"},{"RecursiveStruct":{"NoRecurse":"bar"}}]}}`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand Down Expand Up @@ -1059,7 +1061,7 @@ func TestInputService5ProtocolTestRecursiveShapesCase6(t *testing.T) {
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`{"RecursiveStruct":{"RecursiveMap":{"bar":{"NoRecurse":"bar"},"foo":{"NoRecurse":"foo"}}}}`), util.Trim(string(body)))
utilassert.AssertJSON(t, `{"RecursiveStruct":{"RecursiveMap":{"foo":{"NoRecurse":"foo"},"bar":{"NoRecurse":"bar"}}}}`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand Down Expand Up @@ -1087,7 +1089,7 @@ func TestInputService6ProtocolTestEmptyMapsCase1(t *testing.T) {
// assert body
assert.NotNil(t, r.Body)
body, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, util.Trim(`{"Map":{}}`), util.Trim(string(body)))
utilassert.AssertJSON(t, `{"Map":{}}`, util.Trim(string(body)))

// assert URL
assert.Equal(t, "https://test/", r.URL.String())
Expand Down
Loading

0 comments on commit 0523f96

Please sign in to comment.