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