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
|
package query
import (
"bytes"
"context"
"testing"
"github.com/aws/smithy-go/middleware"
smithyhttp "github.com/aws/smithy-go/transport/http"
)
func TestAsGetRequestMiddleware(t *testing.T) {
cases := map[string]struct {
Request *smithyhttp.Request
Expect *smithyhttp.Request
}{
"post": {
Request: func() *smithyhttp.Request {
req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
req.Method = "POST"
req, _ = req.SetStream(bytes.NewReader([]byte("some=field")))
return req
}(),
Expect: func() *smithyhttp.Request {
req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
req.Method = "GET"
req.URL.RawQuery = "some=field"
return req
}(),
},
"get": {
Request: func() *smithyhttp.Request {
req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
req.Method = "GET"
req.URL.RawQuery = "existing=query"
return req
}(),
Expect: func() *smithyhttp.Request {
req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
req.Method = "GET"
req.URL.RawQuery = "existing=query"
return req
}(),
},
"get with stream": {
Request: func() *smithyhttp.Request {
req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
req.Method = "GET"
req.URL.RawQuery = "existing=query"
req, _ = req.SetStream(bytes.NewReader([]byte("some=field")))
return req
}(),
Expect: func() *smithyhttp.Request {
req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
req.Method = "GET"
req.URL.RawQuery = "existing=query&some=field"
return req
}(),
},
"with query": {
Request: func() *smithyhttp.Request {
req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
req.Method = "POST"
req.URL.RawQuery = "existing=query"
req, _ = req.SetStream(bytes.NewReader([]byte("some=field")))
return req
}(),
Expect: func() *smithyhttp.Request {
req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
req.Method = "GET"
req.URL.RawQuery = "existing=query&some=field"
return req
}(),
},
"no body": {
Request: func() *smithyhttp.Request {
req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
req.Method = "POST"
return req
}(),
Expect: func() *smithyhttp.Request {
req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
req.Method = "GET"
return req
}(),
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
m := &asGetRequest{}
_, _, err := m.HandleSerialize(context.Background(), middleware.SerializeInput{
Parameters: struct{}{},
Request: c.Request,
}, middleware.SerializeHandlerFunc(func(
ctx context.Context, input middleware.SerializeInput,
) (
out middleware.SerializeOutput, metadata middleware.Metadata, err error,
) {
req, ok := input.Request.(*smithyhttp.Request)
if !ok {
t.Fatalf("expect smithy HTTP request, got %T", input.Request)
}
if e, a := c.Expect.URL.RawQuery, req.URL.RawQuery; e != a {
t.Errorf("expect %s query, got %s", e, a)
}
if v := req.GetStream(); v != nil {
t.Errorf("expect no request body, got %v", v)
}
return out, metadata, err
}))
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
})
}
}
|