Skip to content

Commit

Permalink
internal: add more test options
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Nov 26, 2019
1 parent 6e55f24 commit 057f081
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module github.com/kataras/muxie

go 1.13
35 changes: 34 additions & 1 deletion mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 057f081

Please sign in to comment.