-
Notifications
You must be signed in to change notification settings - Fork 749
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support to parse QueryParameter and PostForm on webhook eve… (
#1978) * feat: add support to parse QueryParameter and PostForm on webhook eventsource Signed-off-by: shirou <[email protected]>
- Loading branch information
Showing
2 changed files
with
177 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package webhook | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/smartystreets/goconvey/convey" | ||
|
||
"github.com/argoproj/argo-events/eventsources/common/webhook" | ||
) | ||
|
||
func TestHandleRoute(t *testing.T) { | ||
convey.Convey("Given a route that receives HTTP access", t, func() { | ||
router := &Router{ | ||
route: webhook.GetFakeRoute(), | ||
} | ||
writer := &webhook.FakeHttpWriter{} | ||
|
||
convey.Convey("Test Get method with query parameters", func() { | ||
url, _ := url.Parse("http://example.com/fake?aaa=b%20b&ccc=d%20d") | ||
out := make(chan []byte) | ||
router.route.Active = true | ||
router.route.Context.Method = http.MethodGet | ||
|
||
go func() { | ||
out <- <-router.route.DataCh | ||
}() | ||
|
||
router.HandleRoute(writer, &http.Request{ | ||
Method: http.MethodGet, | ||
URL: url, | ||
}) | ||
result := <-out | ||
convey.So(writer.HeaderStatus, convey.ShouldEqual, http.StatusOK) | ||
convey.So(string(result), convey.ShouldContainSubstring, `"body":{"aaa":["b b"],"ccc":["d d"]}`) | ||
}) | ||
convey.Convey("Test Get method without query parameter", func() { | ||
url, _ := url.Parse("http://example.com/fake") | ||
out := make(chan []byte) | ||
router.route.Active = true | ||
router.route.Context.Method = http.MethodGet | ||
|
||
go func() { | ||
out <- <-router.route.DataCh | ||
}() | ||
|
||
router.HandleRoute(writer, &http.Request{ | ||
Method: http.MethodGet, | ||
URL: url, | ||
}) | ||
result := <-out | ||
convey.So(writer.HeaderStatus, convey.ShouldEqual, http.StatusOK) | ||
convey.So(string(result), convey.ShouldContainSubstring, `"body":{}`) | ||
}) | ||
convey.Convey("Test POST method with form-urlencoded", func() { | ||
payload := []byte(`aaa=b%20b&ccc=d%20d`) | ||
|
||
out := make(chan []byte) | ||
router.route.Active = true | ||
router.route.Context.Method = http.MethodPost | ||
|
||
go func() { | ||
out <- <-router.route.DataCh | ||
}() | ||
|
||
var buf bytes.Buffer | ||
buf.Write(payload) | ||
|
||
headers := make(map[string][]string) | ||
headers["Content-Type"] = []string{"application/x-www-form-urlencoded"} | ||
|
||
router.HandleRoute(writer, &http.Request{ | ||
Method: http.MethodPost, | ||
Header: headers, | ||
Body: io.NopCloser(strings.NewReader(buf.String())), | ||
}) | ||
result := <-out | ||
convey.So(writer.HeaderStatus, convey.ShouldEqual, http.StatusOK) | ||
convey.So(string(result), convey.ShouldContainSubstring, `"body":{"aaa":["b b"],"ccc":["d d"]}`) | ||
}) | ||
convey.Convey("Test POST method with json", func() { | ||
payload := []byte(`{"aaa":["b b"],"ccc":["d d"]}`) | ||
|
||
out := make(chan []byte) | ||
router.route.Active = true | ||
router.route.Context.Method = http.MethodPost | ||
|
||
go func() { | ||
out <- <-router.route.DataCh | ||
}() | ||
|
||
var buf bytes.Buffer | ||
buf.Write(payload) | ||
|
||
headers := make(map[string][]string) | ||
headers["Content-Type"] = []string{"application/json"} | ||
|
||
router.HandleRoute(writer, &http.Request{ | ||
Method: http.MethodPost, | ||
Header: headers, | ||
Body: io.NopCloser(strings.NewReader(buf.String())), | ||
}) | ||
result := <-out | ||
convey.So(writer.HeaderStatus, convey.ShouldEqual, http.StatusOK) | ||
convey.So(string(result), convey.ShouldContainSubstring, `"body":{"aaa":["b b"],"ccc":["d d"]}`) | ||
}) | ||
}) | ||
} |