1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
package middleware
import (
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"github.com/zenazn/goji/web"
)
func testURLQuery(r *http.Request, f func(*web.C, http.ResponseWriter, *http.Request)) *httptest.ResponseRecorder {
var c web.C
h := func(w http.ResponseWriter, r *http.Request) {
f(&c, w, r)
}
m := URLQuery(&c, http.HandlerFunc(h))
w := httptest.NewRecorder()
m.ServeHTTP(w, r)
return w
}
func TestURLQuery(t *testing.T) {
type testcase struct {
url string
expectedParams url.Values
}
// we're not testing url.Query() here, but rather that the results of the query
// appear in the context
testcases := []testcase{
testcase{"/", url.Values{}},
testcase{"/?a=1&b=2&a=3", url.Values{"a": []string{"1", "3"}, "b": []string{"2"}}},
testcase{"/?x=1&y=2&z=3#freddyishere", url.Values{"x": []string{"1"}, "y": []string{"2"}, "z": []string{"3"}}},
}
for _, tc := range testcases {
r, _ := http.NewRequest("GET", tc.url, nil)
testURLQuery(r,
func(c *web.C, w http.ResponseWriter, r *http.Request) {
params := c.Env[URLQueryKey].(url.Values)
if !reflect.DeepEqual(params, tc.expectedParams) {
t.Errorf("GET %s, URLQuery middleware found %v, should be %v", tc.url, params, tc.expectedParams)
}
w.Write([]byte{'h', 'i'})
},
)
}
}
|