@@ -18,21 +18,23 @@ import (
1818
1919func TestClient_GetCredentials (t * testing.T ) {
2020 cases := map [string ]struct {
21- Token string
22- RelativeURI string
23- ResponseCode int
24- ResponseBody []byte
25- ExpectResult * GetCredentialsOutput
26- ExpectErr bool
27- ValidateRequest func (* testing.T , * http.Request )
28- ValidateError func (* testing.T , error ) bool
21+ Token string
22+ RelativeURI string
23+ ResponseCode int
24+ ResponseBody []byte
25+ ResponseContentType string
26+ ExpectResult * GetCredentialsOutput
27+ ExpectErr bool
28+ ValidateRequest func (* testing.T , * http.Request )
29+ ValidateError func (* testing.T , error ) bool
2930 }{
3031 "success static" : {
3132 ResponseCode : 200 ,
3233 ResponseBody : []byte (` {
3334 "AccessKeyId" : "FooKey",
3435 "SecretAccessKey" : "FooSecret"
3536 }` ),
37+ ResponseContentType : "application/json" ,
3638 ExpectResult : & GetCredentialsOutput {
3739 AccessKeyID : "FooKey" ,
3840 SecretAccessKey : "FooSecret" ,
@@ -45,6 +47,7 @@ func TestClient_GetCredentials(t *testing.T) {
4547 "AccessKeyId" : "FooKey",
4648 "SecretAccessKey" : "FooSecret"
4749 }` ),
50+ ResponseContentType : "application/json" ,
4851 ExpectResult : & GetCredentialsOutput {
4952 AccessKeyID : "FooKey" ,
5053 SecretAccessKey : "FooSecret" ,
@@ -59,6 +62,7 @@ func TestClient_GetCredentials(t *testing.T) {
5962 "Token": "FooToken",
6063 "Expiration": "2016-02-25T06:03:31Z"
6164}` ),
65+ ResponseContentType : "application/json" ,
6266 ExpectResult : & GetCredentialsOutput {
6367 AccessKeyID : "FooKey" ,
6468 SecretAccessKey : "FooSecret" ,
@@ -76,6 +80,7 @@ func TestClient_GetCredentials(t *testing.T) {
7680 "AccessKeyId" : "FooKey",
7781 "SecretAccessKey" : "FooSecret"
7882 }` ),
83+ ResponseContentType : "application/json" ,
7984 ValidateRequest : func (t * testing.T , r * http.Request ) {
8085 t .Helper ()
8186 if e , a := "/path/to/thing" , r .URL .Path ; e != a {
@@ -96,7 +101,8 @@ func TestClient_GetCredentials(t *testing.T) {
96101 "code": "Unauthorized",
97102 "message": "not authorized for endpoint"
98103}` ),
99- ExpectErr : true ,
104+ ResponseContentType : "application/json" ,
105+ ExpectErr : true ,
100106 ValidateError : func (t * testing.T , err error ) (ok bool ) {
101107 t .Helper ()
102108 var apiError smithy.APIError
@@ -126,7 +132,8 @@ func TestClient_GetCredentials(t *testing.T) {
126132 "code": "InternalError",
127133 "message": "an error occurred"
128134}` ),
129- ExpectErr : true ,
135+ ResponseContentType : "application/json" ,
136+ ExpectErr : true ,
130137 ValidateError : func (t * testing.T , err error ) (ok bool ) {
131138 t .Helper ()
132139 var apiError smithy.APIError
@@ -151,13 +158,28 @@ func TestClient_GetCredentials(t *testing.T) {
151158 },
152159 },
153160 "non-json error response" : {
154- ResponseCode : 500 ,
155- ResponseBody : []byte (`<html><body>unexpected message format</body></html>` ),
156- ExpectErr : true ,
161+ ResponseCode : 500 ,
162+ ResponseBody : []byte (`<html><body>unexpected message format</body></html>` ),
163+ ResponseContentType : "text/html" ,
164+ ExpectErr : true ,
157165 ValidateError : func (t * testing.T , err error ) (ok bool ) {
158166 t .Helper ()
159- if e , a := "failed to decode error message" , err .Error (); ! strings .Contains (a , e ) {
160- t .Errorf ("expect %v, got %v" , e , a )
167+ var apiError smithy.APIError
168+ if errors .As (err , & apiError ) {
169+ if e , a := "" , apiError .ErrorCode (); e != a {
170+ t .Errorf ("expect %v, got %v" , e , a )
171+ ok = false
172+ }
173+ if e , a := "<html><body>unexpected message format</body></html>" , apiError .ErrorMessage (); e != a {
174+ t .Errorf ("expect %v, got %v" , e , a )
175+ ok = false
176+ }
177+ if e , a := smithy .FaultServer , apiError .ErrorFault (); e != a {
178+ t .Errorf ("expect %v, got %v" , e , a )
179+ ok = false
180+ }
181+ } else {
182+ t .Errorf ("expect %T error type, got %T: %v" , apiError , err , err )
161183 ok = false
162184 }
163185 return ok
@@ -177,6 +199,7 @@ func TestClient_GetCredentials(t *testing.T) {
177199
178200 actualReq .Body = ioutil .NopCloser (bytes .NewReader (buf .Bytes ()))
179201
202+ w .Header ().Set ("Content-Type" , tt .ResponseContentType )
180203 w .WriteHeader (tt .ResponseCode )
181204 w .Write (tt .ResponseBody )
182205 }))
0 commit comments