Skip to content

Commit

Permalink
add unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianyi Wang committed May 17, 2023
1 parent 966f546 commit 970e5a0
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions private/protocol/rest/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,54 @@ import (
)

func TestBuildURI(t *testing.T) {
in := struct {
Topic *string `location:"uri" locationName:"topic" type:"string" required:"true"`
cases := map[string]struct {
Topic *string
ExpectPath string
ExpectRawPath string
}{
Topic: aws.String("///devices/123/test"),
"path without prefix slash": {
Topic: aws.String("devices/123/test"),
ExpectPath: "/topics/devices/123/test",
ExpectRawPath: "/topics/devices%2F123%2Ftest",
},
"path containing single prefix slashes": {
Topic: aws.String("/devices/123/test"),
ExpectPath: "/topics//devices/123/test",
ExpectRawPath: "/topics/%2Fdevices%2F123%2Ftest",
},
"path containing prefix multi-slashes": {
Topic: aws.String("///devices/123/test"),
ExpectPath: "/topics////devices/123/test",
ExpectRawPath: "/topics/%2F%2F%2Fdevices%2F123%2Ftest",
},
}

uri := &url.URL{
Path: "/topics/{topic}",
Scheme: "https",
Host: "host",
}
for _, c := range cases {
uri := &url.URL{
Path: "/topics/{topic}",
Scheme: "https",
Host: "host",
}

req := &request.Request{
HTTPRequest: &http.Request{
URL: uri,
},
Params: &in,
}
req := &request.Request{
HTTPRequest: &http.Request{
URL: uri,
},
Params: &struct {
Topic *string `location:"uri" locationName:"topic" type:"string" required:"true"`
}{
c.Topic,
},
}

Build(req)
Build(req)

expectedPath := "/topics////devices/123/test"
expectedRawPath := "/topics/%2F%2F%2Fdevices%2F123%2Ftest"
if a, e := uri.Path, expectedPath; a != e {
t.Errorf("expect %q Path, got %q", e, a)
}
if a, e := uri.RawPath, expectedRawPath; a != e {
t.Errorf("expect %q RawPath, got %q", e, a)
if a, e := uri.Path, c.ExpectPath; a != e {
t.Errorf("expect %q Path, got %q", e, a)
}
if a, e := uri.RawPath, c.ExpectRawPath; a != e {
t.Errorf("expect %q RawPath, got %q", e, a)
}
}
}

Expand Down

0 comments on commit 970e5a0

Please sign in to comment.