github.com/aavshr/aws-sdk-go@v1.41.3/private/protocol/unmarshal_test.go (about) 1 package protocol_test 2 3 import ( 4 "io/ioutil" 5 "net/http" 6 "strings" 7 "testing" 8 9 "github.com/aavshr/aws-sdk-go/aws/awserr" 10 "github.com/aavshr/aws-sdk-go/aws/request" 11 "github.com/aavshr/aws-sdk-go/private/protocol" 12 "github.com/aavshr/aws-sdk-go/private/protocol/ec2query" 13 "github.com/aavshr/aws-sdk-go/private/protocol/jsonrpc" 14 "github.com/aavshr/aws-sdk-go/private/protocol/query" 15 "github.com/aavshr/aws-sdk-go/private/protocol/restjson" 16 "github.com/aavshr/aws-sdk-go/private/protocol/restxml" 17 ) 18 19 type mockCloser struct { 20 *strings.Reader 21 Closed bool 22 } 23 24 func (m *mockCloser) Close() error { 25 m.Closed = true 26 return nil 27 } 28 29 func TestUnmarshalDrainBody(t *testing.T) { 30 b := &mockCloser{Reader: strings.NewReader("example body")} 31 r := &request.Request{HTTPResponse: &http.Response{ 32 Body: b, 33 }} 34 35 protocol.UnmarshalDiscardBody(r) 36 if err := r.Error; err != nil { 37 t.Errorf("expect nil, %v", err) 38 } 39 if e, a := 0, b.Len(); e != a { 40 t.Errorf("expect %v, got %v", e, a) 41 } 42 if !b.Closed { 43 t.Errorf("expect true") 44 } 45 } 46 47 func TestUnmarshalDrainBodyNoBody(t *testing.T) { 48 r := &request.Request{HTTPResponse: &http.Response{}} 49 50 protocol.UnmarshalDiscardBody(r) 51 if err := r.Error; err != nil { 52 t.Errorf("expect nil, %v", err) 53 } 54 } 55 56 func TestUnmarshalSeriaizationError(t *testing.T) { 57 58 type testOutput struct { 59 _ struct{} 60 } 61 62 cases := []struct { 63 name string 64 r request.Request 65 unmarshalFn func(*request.Request) 66 expectedError awserr.RequestFailure 67 }{ 68 { 69 name: "jsonrpc", 70 r: request.Request{ 71 Data: &testOutput{}, 72 HTTPResponse: &http.Response{ 73 StatusCode: 502, 74 Body: ioutil.NopCloser(strings.NewReader("invalid json")), 75 }, 76 }, 77 unmarshalFn: jsonrpc.Unmarshal, 78 expectedError: awserr.NewRequestFailure( 79 awserr.New(request.ErrCodeSerialization, "", nil), 80 502, 81 "", 82 ), 83 }, 84 { 85 name: "ec2query", 86 r: request.Request{ 87 Data: &testOutput{}, 88 HTTPResponse: &http.Response{ 89 StatusCode: 111, 90 Body: ioutil.NopCloser(strings.NewReader("<<>>>>>>")), 91 }, 92 }, 93 unmarshalFn: ec2query.Unmarshal, 94 expectedError: awserr.NewRequestFailure( 95 awserr.New(request.ErrCodeSerialization, "", nil), 96 111, 97 "", 98 ), 99 }, 100 { 101 name: "query", 102 r: request.Request{ 103 Operation: &request.Operation{ 104 Name: "Foo", 105 }, 106 Data: &testOutput{}, 107 HTTPResponse: &http.Response{ 108 StatusCode: 1, 109 Body: ioutil.NopCloser(strings.NewReader("<<>>>>>>")), 110 }, 111 }, 112 unmarshalFn: query.Unmarshal, 113 expectedError: awserr.NewRequestFailure( 114 awserr.New(request.ErrCodeSerialization, "", nil), 115 1, 116 "", 117 ), 118 }, 119 { 120 name: "restjson", 121 r: request.Request{ 122 Data: &testOutput{}, 123 HTTPResponse: &http.Response{ 124 StatusCode: 123, 125 Body: ioutil.NopCloser(strings.NewReader("invalid json")), 126 }, 127 }, 128 unmarshalFn: restjson.Unmarshal, 129 expectedError: awserr.NewRequestFailure( 130 awserr.New(request.ErrCodeSerialization, "", nil), 131 123, 132 "", 133 ), 134 }, 135 { 136 name: "restxml", 137 r: request.Request{ 138 Data: &testOutput{}, 139 HTTPResponse: &http.Response{ 140 StatusCode: 456, 141 Body: ioutil.NopCloser(strings.NewReader("<<>>>>>>")), 142 }, 143 }, 144 unmarshalFn: restxml.Unmarshal, 145 expectedError: awserr.NewRequestFailure( 146 awserr.New(request.ErrCodeSerialization, "", nil), 147 456, 148 "", 149 ), 150 }, 151 } 152 153 for _, c := range cases { 154 c.unmarshalFn(&c.r) 155 156 rfErr, ok := c.r.Error.(awserr.RequestFailure) 157 if !ok { 158 t.Errorf("%s: expected awserr.RequestFailure, but received %T", c.name, c.r.Error) 159 } 160 161 if e, a := c.expectedError.StatusCode(), rfErr.StatusCode(); e != a { 162 t.Errorf("%s: expected %v, but received %v", c.name, e, a) 163 } 164 } 165 }