Пример #1
0
// Handle will call the Gorilla web toolkit's Handle().Method() methods.
func (g *GorillaRouter) Handle(method, path string, h http.Handler) {
	g.mux.Handle(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// copy the route params into a shared location
		// duplicating memory, but allowing Gizmo to be more flexible with
		// router implementations.
		web.SetRouteVars(r, mux.Vars(r))
		h.ServeHTTP(w, r)
	})).Methods(method)
}
Пример #2
0
// HTTPToFastRoute will convert an http.Handler to a httprouter.Handle
// by stuffing any route parameters into a Gorilla request context.
// To access the request parameters within the endpoint,
// use the `web.Vars` function.
func HTTPToFastRoute(fh http.Handler) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
		if len(params) > 0 {
			vars := map[string]string{}
			for _, param := range params {
				vars[param.Key] = param.Value
			}
			web.SetRouteVars(r, vars)
		}
		fh.ServeHTTP(w, r)
	}
}
Пример #3
0
func TestGetInt64Var(t *testing.T) {

	tests := []struct {
		givenURL   string
		givenRoute string

		want int64
	}{
		{
			"/blah/123",
			"/blah/{key}",
			123,
		},
		{
			"/blah/adsf",
			"/blah/{key}",
			0,
		},
		{
			"/blah?key=123",
			"/blah",
			123,
		},
		{
			"/blah?key=abc",
			"/blah",
			0,
		},
	}

	for _, test := range tests {
		route := mux.NewRouter()
		route.HandleFunc(test.givenRoute, func(w http.ResponseWriter, r *http.Request) {
			web.SetRouteVars(r, mux.Vars(r))
			got := web.GetInt64Var(r, "key")
			if got != test.want {
				t.Errorf("got int of %#v, expected %#v", got, test.want)
			}
		})

		r, _ := http.NewRequest("GET", test.givenURL, nil)
		route.ServeHTTP(httptest.NewRecorder(), r)
	}
}