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
|
package customizations_test
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"testing"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/internal/awstesting/unit"
"github.com/aws/aws-sdk-go-v2/service/apigateway"
"github.com/aws/smithy-go/middleware"
smithyhttp "github.com/aws/smithy-go/transport/http"
)
type mockClient struct {
}
func (m *mockClient) Do(*http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader([]byte(`{
"_links": {
"curies": {
"href": "https://docs.aws.amazon.com/apigateway/latest/developerguide/account-apigateway-{rel}.html",
"name": "account",
"templated": true
},
"self": {
"href": "/account"
},
"account:update": {
"href": "/account"
}
},
"cloudwatchRoleArn": "arn:aws:iam::123456789012:role/apigAwsProxyRole"
}`))),
}, nil
}
func TestAddAcceptHeader(t *testing.T) {
options := apigateway.Options{
Credentials: unit.StubCredentialsProvider{},
Retryer: aws.NopRetryer{},
HTTPClient: &mockClient{},
Region: "mock-region",
}
svc := apigateway.New(options)
fm := requestRetrieverMiddleware{}
_, err := svc.GetAccount(context.Background(), &apigateway.GetAccountInput{}, func(options *apigateway.Options) {
options.APIOptions = append(options.APIOptions, func(stack *middleware.Stack) error {
stack.Build.Add(&fm, middleware.After)
return nil
})
})
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
req := fm.request
if e, a := "application/json", req.Header.Get("Accept"); e != a {
t.Fatalf("Expected Accept header to be set to %v, got %v", e, a)
}
}
type requestRetrieverMiddleware struct {
request *smithyhttp.Request
}
func (*requestRetrieverMiddleware) ID() string { return "apigateway:requestRetrieverMiddleware" }
func (rm *requestRetrieverMiddleware) HandleBuild(
ctx context.Context, in middleware.BuildInput, next middleware.BuildHandler,
) (
out middleware.BuildOutput, metadata middleware.Metadata, err error,
) {
req, ok := in.Request.(*smithyhttp.Request)
if !ok {
return out, metadata, fmt.Errorf("unknown request type %T", req)
}
rm.request = req
return next.HandleBuild(ctx, in)
}
|