From 057f081060d194cdf520839b7c0bad2020506a7e Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Wed, 27 Nov 2019 00:50:16 +0200 Subject: [PATCH] internal: add more test options --- go.mod | 2 ++ mux_test.go | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index cf89ffd..58d2835 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,3 @@ module github.com/kataras/muxie + +go 1.13 diff --git a/mux_test.go b/mux_test.go index 9b4141b..bfde40c 100644 --- a/mux_test.go +++ b/mux_test.go @@ -6,18 +6,51 @@ import ( "io/ioutil" "net/http" "net/http/httptest" + "net/url" + "strings" "testing" ) -func expect(t *testing.T, method, url string) *testie { +func expect(t *testing.T, method, url string, testieOptions ...func(*http.Request)) *testie { req, err := http.NewRequest(method, url, nil) if err != nil { t.Fatal(err) } + for _, opt := range testieOptions { + opt(req) + } + return testReq(t, req) } +func withHeader(key string, value string) func(*http.Request) { + return func(r *http.Request) { + r.Header.Add(key, value) + } +} + +func withURLParam(key string, value string) func(*http.Request) { + return func(r *http.Request) { + r.URL.Query().Add(key, value) + } +} + +func withFormField(key string, value string) func(*http.Request) { + return func(r *http.Request) { + if r.Form == nil { + r.Form = make(url.Values) + } + r.Form.Add(key, value) + + enc := strings.NewReader(r.Form.Encode()) + r.Body = ioutil.NopCloser(enc) + r.ContentLength = int64(enc.Len()) + + r.Header.Set("Content-Type", "application/x-www-form-urlencoded") + } +} + func expectWithBody(t *testing.T, method, url string, body string, headers http.Header) *testie { req, err := http.NewRequest(method, url, bytes.NewBufferString(body)) if err != nil {