-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify and Merge protocol test request unit tests codegen logic (#447)
* Modify and Merge protocol test request unit tests codegen logic * Modify and Merge protocol test generator syntax * Modify and Merge some unit test syntax * Modify and Merge protocol test codegen code --------- Co-authored-by: Tianyi Wang <[email protected]>
- Loading branch information
1 parent
664582d
commit fdfa00c
Showing
4 changed files
with
179 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package protocol | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/aws/smithy-go/middleware" | ||
smithyhttp "github.com/aws/smithy-go/transport/http" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
const captureRequestID = "CaptureProtocolTestRequest" | ||
|
||
// AddCaptureRequestMiddleware captures serialized http request during protocol test for check | ||
func AddCaptureRequestMiddleware(stack *middleware.Stack, req *http.Request) error { | ||
return stack.Build.Add(&captureRequestMiddleware{ | ||
req: req, | ||
}, middleware.After) | ||
} | ||
|
||
type captureRequestMiddleware struct { | ||
req *http.Request | ||
} | ||
|
||
func (*captureRequestMiddleware) ID() string { | ||
return captureRequestID | ||
} | ||
|
||
func (m *captureRequestMiddleware) HandleBuild(ctx context.Context, input middleware.BuildInput, next middleware.BuildHandler, | ||
) ( | ||
output middleware.BuildOutput, metadata middleware.Metadata, err error, | ||
) { | ||
request, ok := input.Request.(*smithyhttp.Request) | ||
if !ok { | ||
return output, metadata, fmt.Errorf("error while retrieving http request") | ||
} | ||
|
||
*m.req = *request.Build(ctx) | ||
if len(m.req.URL.RawPath) == 0 { | ||
m.req.URL.RawPath = m.req.URL.Path | ||
} | ||
if v := m.req.ContentLength; v != 0 { | ||
m.req.Header.Set("Content-Length", strconv.FormatInt(v, 10)) | ||
} | ||
|
||
return next.HandleBuild(ctx, input) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package protocol | ||
|
||
import ( | ||
"context" | ||
"github.com/aws/smithy-go/middleware" | ||
smithytesting "github.com/aws/smithy-go/testing" | ||
smithyhttp "github.com/aws/smithy-go/transport/http" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
// TestAddCaptureRequestMiddleware tests AddCaptureRequestMiddleware | ||
func TestAddCaptureRequestMiddleware(t *testing.T) { | ||
cases := map[string]struct { | ||
Request *http.Request | ||
ExpectRequest *http.Request | ||
ExpectQuery []smithytesting.QueryItem | ||
Stream io.Reader | ||
}{ | ||
"normal request": { | ||
Request: &http.Request{ | ||
Method: "PUT", | ||
Header: map[string][]string{ | ||
"Foo": {"bar", "too"}, | ||
"Checksum": {"SHA256"}, | ||
}, | ||
URL: &url.URL{ | ||
Path: "test/path", | ||
RawQuery: "language=us®ion=us-west+east", | ||
}, | ||
ContentLength: 100, | ||
}, | ||
ExpectRequest: &http.Request{ | ||
Method: "PUT", | ||
Header: map[string][]string{ | ||
"Foo": {"bar", "too"}, | ||
"Checksum": {"SHA256"}, | ||
"Content-Length": {"100"}, | ||
}, | ||
URL: &url.URL{ | ||
Path: "test/path", | ||
RawPath: "test/path", | ||
}, | ||
Body: ioutil.NopCloser(strings.NewReader("hello world.")), | ||
}, | ||
ExpectQuery: []smithytesting.QueryItem{ | ||
{ | ||
Key: "language", | ||
Value: "us", | ||
}, | ||
{ | ||
Key: "region", | ||
Value: "us-west%20east", | ||
}, | ||
}, | ||
Stream: strings.NewReader("hello world."), | ||
}, | ||
} | ||
|
||
for name, c := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
var err error | ||
req := &smithyhttp.Request{ | ||
Request: c.Request, | ||
} | ||
if c.Stream != nil { | ||
req, err = req.SetStream(c.Stream) | ||
if err != nil { | ||
t.Fatalf("Got error while retrieving case stream: %v", err) | ||
} | ||
} | ||
capturedRequest := &http.Request{} | ||
m := captureRequestMiddleware{ | ||
req: capturedRequest, | ||
} | ||
_, _, err = m.HandleBuild(context.Background(), | ||
middleware.BuildInput{Request: req}, | ||
middleware.BuildHandlerFunc(func(ctx context.Context, input middleware.BuildInput) ( | ||
out middleware.BuildOutput, metadata middleware.Metadata, err error) { | ||
return out, metadata, nil | ||
}), | ||
) | ||
if err != nil { | ||
t.Fatalf("expect no error, got %v", err) | ||
} | ||
|
||
if e, a := c.ExpectRequest.Method, capturedRequest.Method; e != a { | ||
t.Errorf("expect request method %v found, got %v", e, a) | ||
} | ||
if e, a := c.ExpectRequest.URL.Path, capturedRequest.URL.RawPath; e != a { | ||
t.Errorf("expect %v path, got %v", e, a) | ||
} | ||
if c.ExpectRequest.Body != nil { | ||
expect, err := ioutil.ReadAll(c.ExpectRequest.Body) | ||
if capturedRequest.Body == nil { | ||
t.Errorf("Expect request stream %v captured, get nil", string(expect)) | ||
} | ||
actual, err := ioutil.ReadAll(capturedRequest.Body) | ||
if err != nil { | ||
t.Errorf("unable to read captured request body, %v", err) | ||
} | ||
if e, a := string(expect), string(actual); e != a { | ||
t.Errorf("expect request body to be %s, got %s", e, a) | ||
} | ||
} | ||
queryItems := smithytesting.ParseRawQuery(capturedRequest.URL.RawQuery) | ||
smithytesting.AssertHasQuery(t, c.ExpectQuery, queryItems) | ||
smithytesting.AssertHasHeader(t, c.ExpectRequest.Header, capturedRequest.Header) | ||
}) | ||
} | ||
} |