func TestInputService5ProtocolTestSerializeMapTypeCase2(t *testing.T) { svc := NewInputService5ProtocolTest(nil) svc.Endpoint = "https://test" input := &InputService5TestShapeInputShape{ MapArg: &map[string]*string{}, } req, _ := svc.InputService5TestCaseOperation2Request(input) r := req.HTTPRequest // build request query.Build(req) assert.NoError(t, req.Error) // assert body assert.NotNil(t, r.Body) body, _ := ioutil.ReadAll(r.Body) assert.Equal(t, util.Trim(`Action=OperationName&MapArg=&Version=2014-01-01`), util.Trim(string(body))) // assert URL assert.Equal(t, "https://test/", r.URL.String()) // assert headers }
func TestInputService9ProtocolTestRecursiveShapesCase6(t *testing.T) { svc := NewInputService9ProtocolTest(nil) svc.Endpoint = "https://test" input := &InputService9TestShapeInputShape{ RecursiveStruct: &InputService9TestShapeRecursiveStructType{ RecursiveMap: map[string]*InputService9TestShapeRecursiveStructType{ "bar": { NoRecurse: aws.String("bar"), }, "foo": { NoRecurse: aws.String("foo"), }, }, }, } req, _ := svc.InputService9TestCaseOperation6Request(input) r := req.HTTPRequest // build request query.Build(req) assert.NoError(t, req.Error) // assert body assert.NotNil(t, r.Body) body, _ := ioutil.ReadAll(r.Body) assert.Equal(t, util.Trim(`Action=OperationName&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&Version=2014-01-01`), util.Trim(string(body))) // assert URL assert.Equal(t, "https://test/", r.URL.String()) // assert headers }
func TestInputService1ProtocolTestScalarMembersCase1(t *testing.T) { svc := NewInputService1ProtocolTest(nil) svc.Endpoint = "https://test" input := &InputService1TestShapeInputShape{ Bar: aws.String("val2"), Foo: aws.String("val1"), } req, _ := svc.InputService1TestCaseOperation1Request(input) r := req.HTTPRequest // build request query.Build(req) assert.NoError(t, req.Error) // 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))) // assert URL assert.Equal(t, "https://test/", r.URL.String()) // assert headers }
func TestInputService7ProtocolTestBase64EncodedBlobsCase1(t *testing.T) { svc := NewInputService7ProtocolTest(nil) svc.Endpoint = "https://test" input := &InputService7TestShapeInputService7TestCaseOperation1Input{ BlobArg: []byte("foo"), } req, _ := svc.InputService7TestCaseOperation1Request(input) r := req.HTTPRequest // build request ec2query.Build(req) assert.NoError(t, req.Error) // 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))) // assert URL assert.Equal(t, "https://test/", r.URL.String()) // assert headers }
func TestInputService8ProtocolTestTimestampValuesCase1(t *testing.T) { svc := NewInputService8ProtocolTest(nil) svc.Endpoint = "https://test" input := &InputService8TestShapeInputService8TestCaseOperation1Input{ TimeArg: aws.Time(time.Unix(1422172800, 0)), } req, _ := svc.InputService8TestCaseOperation1Request(input) r := req.HTTPRequest // build request ec2query.Build(req) assert.NoError(t, req.Error) // 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))) // assert URL assert.Equal(t, "https://test/", r.URL.String()) // assert headers }
func TestInputService3ProtocolTestNestedStructureMembersCase1(t *testing.T) { svc := NewInputService3ProtocolTest(nil) svc.Endpoint = "https://test" input := &InputService3TestShapeInputService3TestCaseOperation1Input{ StructArg: &InputService3TestShapeStructType{ ScalarArg: aws.String("foo"), }, } req, _ := svc.InputService3TestCaseOperation1Request(input) r := req.HTTPRequest // build request ec2query.Build(req) assert.NoError(t, req.Error) // 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))) // assert URL assert.Equal(t, "https://test/", r.URL.String()) // assert headers }
func TestInputService6ProtocolTestListWithLocationNameAndQueryNameCase1(t *testing.T) { svc := NewInputService6ProtocolTest(nil) svc.Endpoint = "https://test" input := &InputService6TestShapeInputService6TestCaseOperation1Input{ ListArg: []*string{ aws.String("a"), aws.String("b"), aws.String("c"), }, } req, _ := svc.InputService6TestCaseOperation1Request(input) r := req.HTTPRequest // build request ec2query.Build(req) assert.NoError(t, req.Error) // 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))) // assert URL assert.Equal(t, "https://test/", r.URL.String()) // assert headers }
func TestInputService4ProtocolTestListTypesCase1(t *testing.T) { svc := NewInputService4ProtocolTest(nil) svc.Endpoint = "https://test" input := &InputService4TestShapeInputShape{ ListArg: []*string{ aws.String("foo"), aws.String("bar"), aws.String("baz"), }, } req, _ := svc.InputService4TestCaseOperation1Request(input) r := req.HTTPRequest // build request ec2query.Build(req) assert.NoError(t, req.Error) // 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))) // assert URL assert.Equal(t, "https://test/", r.URL.String()) // assert headers }
func TestInputService2ProtocolTestStructureWithLocationNameAndQueryNameAppliedToMembersCase1(t *testing.T) { svc := NewInputService2ProtocolTest(nil) svc.Endpoint = "https://test" input := &InputService2TestShapeInputService2TestCaseOperation1Input{ Bar: aws.String("val2"), Foo: aws.String("val1"), Yuck: aws.String("val3"), } req, _ := svc.InputService2TestCaseOperation1Request(input) r := req.HTTPRequest // build request ec2query.Build(req) assert.NoError(t, req.Error) // 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))) // assert URL assert.Equal(t, "https://test/", r.URL.String()) // assert headers }
func TestInputService6ProtocolTestSerializeMapTypeCase1(t *testing.T) { svc := NewInputService6ProtocolTest(nil) svc.Endpoint = "https://test" input := &InputService6TestShapeInputService6TestCaseOperation1Input{ MapArg: map[string]*string{ "key1": aws.String("val1"), "key2": aws.String("val2"), }, } req, _ := svc.InputService6TestCaseOperation1Request(input) r := req.HTTPRequest // build request query.Build(req) assert.NoError(t, req.Error) // assert body assert.NotNil(t, r.Body) body, _ := ioutil.ReadAll(r.Body) assert.Equal(t, util.Trim(`Action=OperationName&MapArg.entry.1.key=key1&MapArg.entry.1.value=val1&MapArg.entry.2.key=key2&MapArg.entry.2.value=val2&Version=2014-01-01`), util.Trim(string(body))) // assert URL assert.Equal(t, "https://test/", r.URL.String()) // assert headers }
func TestInputService4ProtocolTestFlattenedListCase2(t *testing.T) { svc := NewInputService4ProtocolTest(nil) svc.Endpoint = "https://test" input := &InputService4TestShapeInputShape{ ListArg: []*string{}, ScalarArg: aws.String("foo"), } req, _ := svc.InputService4TestCaseOperation2Request(input) r := req.HTTPRequest // build request query.Build(req) assert.NoError(t, req.Error) // assert body assert.NotNil(t, r.Body) body, _ := ioutil.ReadAll(r.Body) assert.Equal(t, util.Trim(`Action=OperationName&ListArg=&ScalarArg=foo&Version=2014-01-01`), util.Trim(string(body))) // assert URL assert.Equal(t, "https://test/", r.URL.String()) // assert headers }
func TestOutputService1ProtocolTestScalarMembersCase1(t *testing.T) { svc := NewOutputService1ProtocolTest(nil) buf := bytes.NewReader([]byte("<OperationNameResponse><Str>myname</Str><FooNum>123</FooNum><FalseBool>false</FalseBool><TrueBool>true</TrueBool><Float>1.2</Float><Double>1.3</Double><Long>200</Long><Char>a</Char><RequestId>request-id</RequestId></OperationNameResponse>")) req, out := svc.OutputService1TestCaseOperation1Request(nil) req.HTTPResponse = &http.Response{StatusCode: 200, Body: ioutil.NopCloser(buf), Header: http.Header{}} // set headers // unmarshal response ec2query.UnmarshalMeta(req) ec2query.Unmarshal(req) assert.NoError(t, req.Error) // assert response assert.NotNil(t, out) // ensure out variable is used assert.Equal(t, "a", *out.Char) assert.Equal(t, 1.3, *out.Double) assert.Equal(t, false, *out.FalseBool) assert.Equal(t, 1.2, *out.Float) assert.Equal(t, int64(200), *out.Long) assert.Equal(t, int64(123), *out.Num) assert.Equal(t, "myname", *out.Str) assert.Equal(t, true, *out.TrueBool) }
func TestSendHandlerError(t *testing.T) { svc := service.New(&aws.Config{ HTTPClient: &http.Client{ Transport: &testSendHandlerTransport{}, }, }) svc.Handlers.Clear() svc.Handlers.Send.PushBackNamed(corehandlers.SendHandler) r := svc.NewRequest(&request.Operation{Name: "Operation"}, nil, nil) r.Send() assert.Error(t, r.Error) assert.NotNil(t, r.HTTPResponse) }
func TestOutputService2ProtocolTestBlobCase1(t *testing.T) { svc := NewOutputService2ProtocolTest(nil) buf := bytes.NewReader([]byte("<OperationNameResponse><Blob>dmFsdWU=</Blob><RequestId>requestid</RequestId></OperationNameResponse>")) req, out := svc.OutputService2TestCaseOperation1Request(nil) req.HTTPResponse = &http.Response{StatusCode: 200, Body: ioutil.NopCloser(buf), Header: http.Header{}} // set headers // unmarshal response ec2query.UnmarshalMeta(req) ec2query.Unmarshal(req) assert.NoError(t, req.Error) // assert response assert.NotNil(t, out) // ensure out variable is used assert.Equal(t, "value", string(out.Blob)) }
func TestRequestExhaustRetries(t *testing.T) { delays := []time.Duration{} sleepDelay := func(delay time.Duration) { delays = append(delays, delay) } reqNum := 0 reqs := []http.Response{ {StatusCode: 500, Body: body(`{"__type":"UnknownError","message":"An error occurred."}`)}, {StatusCode: 500, Body: body(`{"__type":"UnknownError","message":"An error occurred."}`)}, {StatusCode: 500, Body: body(`{"__type":"UnknownError","message":"An error occurred."}`)}, {StatusCode: 500, Body: body(`{"__type":"UnknownError","message":"An error occurred."}`)}, } s := service.New(aws.NewConfig().WithMaxRetries(aws.DefaultRetries).WithSleepDelay(sleepDelay)) s.Handlers.Validate.Clear() s.Handlers.Unmarshal.PushBack(unmarshal) s.Handlers.UnmarshalError.PushBack(unmarshalError) s.Handlers.Send.Clear() // mock sending s.Handlers.Send.PushBack(func(r *request.Request) { r.HTTPResponse = &reqs[reqNum] reqNum++ }) r := s.NewRequest(&request.Operation{Name: "Operation"}, nil, nil) err := r.Send() assert.NotNil(t, err) if e, ok := err.(awserr.RequestFailure); ok { assert.Equal(t, 500, e.StatusCode()) } else { assert.Fail(t, "Expected error to be a service failure") } assert.Equal(t, "UnknownError", err.(awserr.Error).Code()) assert.Equal(t, "An error occurred.", err.(awserr.Error).Message()) assert.Equal(t, 3, int(r.RetryCount)) expectDelays := []struct{ min, max time.Duration }{{30, 59}, {60, 118}, {120, 236}} for i, v := range delays { min := expectDelays[i].min * time.Millisecond max := expectDelays[i].max * time.Millisecond assert.True(t, min <= v && v <= max, "Expect delay to be within range, i:%d, v:%s, min:%s, max:%s", i, v, min, max) } }
func TestOutputService8ProtocolTestNamedMapCase1(t *testing.T) { svc := NewOutputService8ProtocolTest(nil) buf := bytes.NewReader([]byte("<OperationNameResponse><Map><foo>qux</foo><bar>bar</bar></Map><Map><foo>baz</foo><bar>bam</bar></Map><RequestId>requestid</RequestId></OperationNameResponse>")) req, out := svc.OutputService8TestCaseOperation1Request(nil) req.HTTPResponse = &http.Response{StatusCode: 200, Body: ioutil.NopCloser(buf), Header: http.Header{}} // set headers // unmarshal response ec2query.UnmarshalMeta(req) ec2query.Unmarshal(req) assert.NoError(t, req.Error) // assert response assert.NotNil(t, out) // ensure out variable is used assert.Equal(t, "bam", *out.Map["baz"]) assert.Equal(t, "bar", *out.Map["qux"]) }
func TestOutputService5ProtocolTestFlattenedListCase1(t *testing.T) { svc := NewOutputService5ProtocolTest(nil) buf := bytes.NewReader([]byte("<OperationNameResponse><ListMember>abc</ListMember><ListMember>123</ListMember><RequestId>requestid</RequestId></OperationNameResponse>")) req, out := svc.OutputService5TestCaseOperation1Request(nil) req.HTTPResponse = &http.Response{StatusCode: 200, Body: ioutil.NopCloser(buf), Header: http.Header{}} // set headers // unmarshal response ec2query.UnmarshalMeta(req) ec2query.Unmarshal(req) assert.NoError(t, req.Error) // assert response assert.NotNil(t, out) // ensure out variable is used assert.Equal(t, "abc", *out.ListMember[0]) assert.Equal(t, "123", *out.ListMember[1]) }
// test that retries don't occur for 4xx status codes with a response type that can't be retried func TestRequest4xxUnretryable(t *testing.T) { s := NewService(&Config{MaxRetries: 10}) s.Handlers.Validate.Clear() s.Handlers.Unmarshal.PushBack(unmarshal) s.Handlers.UnmarshalError.PushBack(unmarshalError) s.Handlers.Send.Clear() // mock sending s.Handlers.Send.PushBack(func(r *Request) { r.HTTPResponse = &http.Response{StatusCode: 401, Body: body(`{"__type":"SignatureDoesNotMatch","message":"Signature does not match."}`)} }) out := &testData{} r := NewRequest(s, &Operation{Name: "Operation"}, nil, out) err := r.Send() assert.NotNil(t, err) if e, ok := err.(awserr.RequestFailure); ok { assert.Equal(t, 401, e.StatusCode()) } else { assert.Fail(t, "Expected error to be a service failure") } assert.Equal(t, "SignatureDoesNotMatch", err.(awserr.Error).Code()) assert.Equal(t, "Signature does not match.", err.(awserr.Error).Message()) assert.Equal(t, 0, int(r.RetryCount)) }
func TestRequestExhaustRetries(t *testing.T) { delays := []time.Duration{} sleepDelay = func(delay time.Duration) { delays = append(delays, delay) } reqNum := 0 reqs := []http.Response{ http.Response{StatusCode: 500, Body: body(`{"__type":"UnknownError","message":"An error occurred."}`)}, http.Response{StatusCode: 500, Body: body(`{"__type":"UnknownError","message":"An error occurred."}`)}, http.Response{StatusCode: 500, Body: body(`{"__type":"UnknownError","message":"An error occurred."}`)}, http.Response{StatusCode: 500, Body: body(`{"__type":"UnknownError","message":"An error occurred."}`)}, } s := NewService(&Config{MaxRetries: -1}) s.Handlers.Validate.Clear() s.Handlers.Unmarshal.PushBack(unmarshal) s.Handlers.UnmarshalError.PushBack(unmarshalError) s.Handlers.Send.Clear() // mock sending s.Handlers.Send.PushBack(func(r *Request) { r.HTTPResponse = &reqs[reqNum] reqNum++ }) r := NewRequest(s, &Operation{Name: "Operation"}, nil, nil) err := r.Send() assert.NotNil(t, err) if e, ok := err.(awserr.RequestFailure); ok { assert.Equal(t, 500, e.StatusCode()) } else { assert.Fail(t, "Expected error to be a service failure") } assert.Equal(t, "UnknownError", err.(awserr.Error).Code()) assert.Equal(t, "An error occurred.", err.(awserr.Error).Message()) assert.Equal(t, 3, int(r.RetryCount)) assert.True(t, reflect.DeepEqual([]time.Duration{30 * time.Millisecond, 60 * time.Millisecond, 120 * time.Millisecond}, delays)) }