예제 #1
0
파일: ctxmux.go 프로젝트: chrisp-fb/rell
func (m *Mux) makeContext(r *http.Request) (context.Context, error) {
	if m.contextMaker != nil {
		ctx, err := m.contextMaker(r)
		if err != nil {
			return context.Background(), err
		}
		return ctx, nil
	}
	return context.Background(), nil
}
예제 #2
0
func fromValues(t *testing.T, values url.Values) (*rellenv.Env, context.Context) {
	req, err := http.NewRequest(
		"GET",
		"http://www.fbrell.com/?"+values.Encode(),
		nil)
	if err != nil {
		t.Fatalf("Failed to create request: %s", err)
	}
	env, err := defaultParser().FromRequest(context.Background(), req)
	if err != nil {
		t.Fatalf("Failed to create env: %s", err)
	}
	return env, rellenv.WithEnv(context.Background(), env)
}
예제 #3
0
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)
}
예제 #4
0
func TestWrapExistingMultiStackError(t *testing.T) {
	err := &multiStackError{
		multiStack: new(stack.Multi),
	}
	we := Wrap(context.Background(), err)
	ensure.DeepEqual(t, we, err)
	ensure.DeepEqual(t, len(err.multiStack.Stacks()), 1)
}
예제 #5
0
func TestWrapNewSingleFrame(t *testing.T) {
	err := errors.New("")
	config := Config{StackMode: StackModeSingleFrame}
	ctx := WithConfig(context.Background(), config)
	we := Wrap(ctx, err).(*singleFrameError)
	ensure.DeepEqual(t, we.config, config)
	ensure.DeepEqual(t, we.underlying, err)
	ensure.DeepEqual(t, we.frame.Name, "TestWrapNewSingleFrame")
}
예제 #6
0
func TestWrapNewMultiStackError(t *testing.T) {
	err := errors.New("")
	config := Config{StackMode: StackModeMultiStack}
	ctx := WithConfig(context.Background(), config)
	we := Wrap(ctx, err).(*multiStackError)
	ensure.DeepEqual(t, we.config, config)
	ensure.DeepEqual(t, we.underlying, err)
	ensure.DeepEqual(t, we.multiStack.Stacks()[0][0].Name, "TestWrapNewMultiStackError")
}
예제 #7
0
파일: web.go 프로젝트: chrisp-fb/rell
// 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)
}
예제 #8
0
func ExampleWithTimeout() {
	// Pass a context with a timeout to tell a blocking function that it
	// should abandon its work after the timeout elapses.
	ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond)
	select {
	case <-time.After(200 * time.Millisecond):
		fmt.Println("overslept")
	case <-ctx.Done():
		fmt.Println(ctx.Err()) // prints "context deadline exceeded"
	}
	// Output:
	// context deadline exceeded
}
예제 #9
0
파일: og_test.go 프로젝트: chrisp-fb/rell
func TestParseBase64(t *testing.T) {
	t.Parallel()
	const song1 = "W1sib2c6dGl0bGUiLCJzb25nMSJdLFsib2c6dHlwZSIsInNvbmciXV0"
	expected := &Object{Pairs: []Pair{
		{"og:title", "song1"},
		{"og:type", "song"},
		{"og:url", "http://www.fbrell.com/rog/" + song1},
		{"og:image", "http://www.fbrell.com/static/W1siL2ltYWdlcy90YXhpX3JvdGlhXzI4MDYzMzkxMjUuanBnIiwiMTdkMTlmNDUiXV0.jpg"},
		{"og:description", stockDescriptions[0]},
	}}

	object, err := defaultParser().FromBase64(context.Background(), defaultContext, song1)
	if err != nil {
		t.Fatal(err)
	}
	assertSubset(t, expected, object)
}
예제 #10
0
파일: og_test.go 프로젝트: chrisp-fb/rell
func TestParseValues(t *testing.T) {
	t.Parallel()
	const ogType = "article"
	const ogTitle = "foo"
	values := url.Values{}
	values.Set("og:type", ogType)
	values.Set("og:title", ogTitle)
	expected := &Object{Pairs: []Pair{
		{"og:type", ogType},
		{"og:title", ogTitle},
		{"og:url", "http://www.fbrell.com/og/" + ogType + "/" + ogTitle},
		{"og:image", "http://www.fbrell.com/static/W1siL2ltYWdlcy90YXhpX3JvdGlhXzI4MDYzMzkxMjUuanBnIiwiMTdkMTlmNDUiXV0.jpg"},
		{"og:description", stockDescriptions[6]},
	}}

	object, err := defaultParser().FromValues(context.Background(), defaultContext, values)
	if err != nil {
		t.Fatal(err)
	}
	assertSubset(t, expected, object)
}
예제 #11
0
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)
	}
}
예제 #12
0
func TestWithConfig(t *testing.T) {
	ac := Config{StackMode: StackModeMultiStack}
	ctx := WithConfig(context.Background(), ac)
	ensure.DeepEqual(t, ContextConfig(ctx), ac)
}
예제 #13
0
func TestContextWithNoParams(t *testing.T) {
	var nilParams httprouter.Params
	ensure.DeepEqual(t, ctxmux.ContextParams(context.Background()), nilParams)
}
예제 #14
0
func TestWrapDefaultPassThru(t *testing.T) {
	err := errors.New("")
	ensure.DeepEqual(t, Wrap(context.Background(), err), err)
}
예제 #15
0
func TestContextWithFromParams(t *testing.T) {
	p := httprouter.Params{}
	ctx := ctxmux.WithParams(context.Background(), p)
	actual := ctxmux.ContextParams(ctx)
	ensure.DeepEqual(t, actual, p)
}
예제 #16
0
func TestContextConfig(t *testing.T) {
	ensure.DeepEqual(t, ContextConfig(context.Background()), Config{})
}
예제 #17
0
func TestWrapSingleStackErro(t *testing.T) {
	err := &singleStackError{}
	ensure.DeepEqual(t, Wrap(context.Background(), err), err)
}
예제 #18
0
func TestWrapNil(t *testing.T) {
	ensure.Nil(t, Wrap(context.Background(), nil))
}