func TestMuxContextMakerError(t *testing.T) { givenErr := errors.New("") var actualErr error mux, err := ctxmux.New( ctxmux.MuxContextMaker(func(r *http.Request) (context.Context, error) { return nil, givenErr }), ctxmux.MuxErrorHandler( func(ctx context.Context, w http.ResponseWriter, r *http.Request, err error) { ensure.DeepEqual(t, ctx, context.Background()) actualErr = err }), ) ensure.Nil(t, err) hw := httptest.NewRecorder() hr := &http.Request{ Method: "GET", URL: &url.URL{ Path: "/", }, } mux.GET(hr.URL.Path, func(context.Context, http.ResponseWriter, *http.Request) error { panic("not reached") }) mux.ServeHTTP(hw, hr) ensure.DeepEqual(t, actualErr, givenErr) }
func TestNewError(t *testing.T) { givenErr := errors.New("") mux, err := ctxmux.New( func(*ctxmux.Mux) error { return givenErr }, ) ensure.True(t, mux == nil) ensure.DeepEqual(t, err, givenErr) }
// Serve HTTP requests for the main port. func (a *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { a.once.Do(func() { const public = "/public/" fileserver := http.FileServer(a.PublicFS) mux, err := ctxmux.New( ctxmux.MuxErrorHandler(a.handleError), ctxmux.MuxNotFoundHandler(a.ExamplesHandler.Example), ctxmux.MuxRedirectTrailingSlash(), ctxmux.MuxContextMaker(a.contextMaker), ) if err != nil { panic(err) } mux.GET(a.Static.Path+"*rest", ctxmux.HTTPHandler(a.Static)) mux.GET("/favicon.ico", ctxmux.HTTPHandler(fileserver)) mux.GET("/f8.jpg", ctxmux.HTTPHandler(fileserver)) mux.GET("/robots.txt", ctxmux.HTTPHandler(fileserver)) mux.GET(public+"*rest", ctxmux.HTTPHandler(http.StripPrefix(public, fileserver))) mux.GET("/info/*rest", a.ContextHandler.Info) mux.POST("/info/*rest", a.ContextHandler.Info) mux.GET("/examples/", a.ExamplesHandler.List) mux.GET("/saved/:hash", a.ExamplesHandler.GetSaved) mux.POST("/saved/", a.ExamplesHandler.PostSaved) mux.GET("/og/*rest", a.OgHandler.Values) mux.GET("/rog/*rest", a.OgHandler.Base64) mux.GET("/rog-redirect/*rest", a.OgHandler.Redirect) mux.GET(oauth.Path+"*rest", a.OauthHandler.Handler) if a.AdminHandler.Path != "" { adminPath := path.Join("/", a.AdminHandler.Path) + "/*rest" mux.GET(adminPath, ctxmux.HTTPHandler(a.AdminHandler)) } var handler http.Handler handler = &appdata.Handler{ Handler: mux, Secret: a.App.SecretByte(), MaxAge: a.SignedRequestMaxAge, } a.mux = handler a.ctx = context.Background() a.ctx = ctxerr.WithConfig(a.ctx, ctxerr.Config{ StackMode: ctxerr.StackModeMultiStack, StringMode: ctxerr.StringModeNone, }) }) a.mux.ServeHTTP(w, r) }
func TestRedirectTrailingSlash(t *testing.T) { mux, err := ctxmux.New(ctxmux.MuxRedirectTrailingSlash()) ensure.Nil(t, err) hw := httptest.NewRecorder() hr := &http.Request{ Method: "GET", URL: &url.URL{ Path: "/foo", }, } mux.GET(hr.URL.Path+"/", func(context.Context, http.ResponseWriter, *http.Request) error { return nil }) mux.ServeHTTP(hw, hr) ensure.DeepEqual(t, hw.Header().Get("Location"), hr.URL.Path) ensure.DeepEqual(t, hw.Code, http.StatusMovedPermanently) }
func TestHandleCustomMethod(t *testing.T) { mux, err := ctxmux.New() ensure.Nil(t, err) const method = "FOO" body := []byte("body") hw := httptest.NewRecorder() hr := &http.Request{ Method: method, URL: &url.URL{ Path: "/", }, } mux.Handler(method, hr.URL.Path, func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { w.Write(body) return nil }) mux.ServeHTTP(hw, hr) ensure.DeepEqual(t, hw.Body.Bytes(), body) }
func TestHandlerNotFound(t *testing.T) { var called bool mux, err := ctxmux.New( ctxmux.MuxNotFoundHandler( func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { called = true return nil }), ) ensure.Nil(t, err) hw := httptest.NewRecorder() hr := &http.Request{ Method: "GET", URL: &url.URL{ Path: "/", }, } mux.ServeHTTP(hw, hr) ensure.True(t, called) }
func TestHandlerNoPanic(t *testing.T) { mux, err := ctxmux.New( ctxmux.MuxPanicHandler( func(ctx context.Context, w http.ResponseWriter, r *http.Request, v interface{}) { panic("not reached") }), ) ensure.Nil(t, err) hw := httptest.NewRecorder() hr := &http.Request{ Method: "GET", URL: &url.URL{ Path: "/", }, } mux.GET(hr.URL.Path, func(context.Context, http.ResponseWriter, *http.Request) error { return nil }) mux.ServeHTTP(hw, hr) }
func TestWrapMethods(t *testing.T) { cases := []struct { Method string Register func(*ctxmux.Mux, string, ctxmux.Handler) }{ {Method: "HEAD", Register: (*ctxmux.Mux).HEAD}, {Method: "GET", Register: (*ctxmux.Mux).GET}, {Method: "POST", Register: (*ctxmux.Mux).POST}, {Method: "PUT", Register: (*ctxmux.Mux).PUT}, {Method: "DELETE", Register: (*ctxmux.Mux).DELETE}, {Method: "PATCH", Register: (*ctxmux.Mux).PATCH}, } const key = int(1) const val = int(2) body := []byte("body") for _, c := range cases { mux, err := ctxmux.New( ctxmux.MuxContextMaker(func(r *http.Request) (context.Context, error) { ctx := context.Background() return context.WithValue(ctx, key, val), nil }), ) ensure.Nil(t, err) hw := httptest.NewRecorder() hr := &http.Request{ Method: c.Method, URL: &url.URL{ Path: "/", }, } c.Register(mux, hr.URL.Path, func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { ensure.DeepEqual(t, ctx.Value(key), val) w.Write(body) return nil }) mux.ServeHTTP(hw, hr) ensure.DeepEqual(t, hw.Body.Bytes(), body) } }
func TestHandlerPanic(t *testing.T) { var actualPanic interface{} mux, err := ctxmux.New( ctxmux.MuxPanicHandler( func(ctx context.Context, w http.ResponseWriter, r *http.Request, v interface{}) { actualPanic = v }), ) ensure.Nil(t, err) hw := httptest.NewRecorder() hr := &http.Request{ Method: "GET", URL: &url.URL{ Path: "/", }, } givenPanic := int(42) mux.GET(hr.URL.Path, func(context.Context, http.ResponseWriter, *http.Request) error { panic(givenPanic) }) mux.ServeHTTP(hw, hr) ensure.DeepEqual(t, actualPanic, givenPanic) }
func TestHandlerReturnErr(t *testing.T) { givenErr := errors.New("") var actualErr error mux, err := ctxmux.New( ctxmux.MuxErrorHandler( func(ctx context.Context, w http.ResponseWriter, r *http.Request, err error) { actualErr = err }), ) ensure.Nil(t, err) hw := httptest.NewRecorder() hr := &http.Request{ Method: "GET", URL: &url.URL{ Path: "/", }, } mux.GET(hr.URL.Path, func(context.Context, http.ResponseWriter, *http.Request) error { return givenErr }) mux.ServeHTTP(hw, hr) ensure.DeepEqual(t, actualErr, givenErr) }