Example #1
0
func TestWorkWithNegroni(t *testing.T) {
	m1 := func(ctx context.Context, w http.ResponseWriter, r *http.Request, next negroni.ContextHandlerFunc) {
		fmt.Fprint(w, "+middleware1")
		next(ctx, w, r)
	}
	m2 := func(ctx context.Context, w http.ResponseWriter, r *http.Request, next negroni.ContextHandlerFunc) {
		fmt.Fprint(w, "+middleware2")
		ctx = context.WithValue(ctx, "m2", "Hahaha")
		next(ctx, w, r)
	}
	m3 := func(ctx context.Context, w http.ResponseWriter, r *http.Request, next negroni.ContextHandlerFunc) {
		fmt.Fprint(w, "+middleware3")
		next(ctx, w, r)
	}
	h1 := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "+handler1:m2=%s", ctx.Value("m2").(string))
	}
	h2 := func(ctx context.Context, w http.ResponseWriter, r *http.Request, ps Params) {
		fmt.Fprintf(w, "+handler2:ps=%s, m2=%s", ps.ByName("name"), ctx.Value("m2").(string))
	}

	endrouter := New()
	endrouter.GET("/admin/handler2/:name", h2)
	endrouter.ContextHandlerFunc("GET", "/admin/handler1", h1)

	m12 := negroni.New(negroni.HandlerFunc(m1), negroni.HandlerFunc(m2), negroni.WrapCH(negroni.ContextHandlerFunc(h1)))
	m32 := negroni.New(negroni.HandlerFunc(m3), negroni.HandlerFunc(m2), negroni.WrapCH(endrouter))

	root := New()
	root.ContextHandler("GET", "/", m12)
	root.ContextHandler("GET", "/admin/*fp", m32)

	w := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "/", nil)
	root.ServeHTTP(w, req)
	if w.Code != http.StatusOK || w.Body.String() != "+middleware1+middleware2+handler1:m2=Hahaha" {
		t.Errorf("GET / failed: %v", w)
	}

	w = httptest.NewRecorder()
	req, _ = http.NewRequest("GET", "/admin/handler2/lumieru", nil)
	root.ServeHTTP(w, req)
	if w.Code != http.StatusOK || w.Body.String() != "+middleware3+middleware2+handler2:ps=lumieru, m2=Hahaha" {
		t.Errorf("GET /admin/handler2/lumieru failed: %v", w)
	}

	w = httptest.NewRecorder()
	req, _ = http.NewRequest("GET", "/admin/handler1", nil)
	root.ServeHTTP(w, req)
	if w.Code != http.StatusOK || w.Body.String() != "+middleware3+middleware2+handler1:m2=Hahaha" {
		t.Errorf("GET /admin/handler1 failed: %v", w)
	}
}
Example #2
0
func TestContextHandler(t *testing.T) {
	router := New()

	router.Handler("GET", "/handler", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "GET /handler")
	}))

	router.HandlerFunc("GET", "/handlerfunc", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "GET /handlerfunc")
	})

	router.ContextHandler("GET", "/contexthandler",
		negroni.ContextHandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
			fmt.Fprintf(w, "GET /contexthandler, value(1)=%s", ctx.Value(1).(string))
		}))

	router.ContextHandlerFunc("GET", "/contexthandlerfunc", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "GET /contexthandlerfunc, value(2)=%s", ctx.Value(2).(string))
	})

	w := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "/handler", nil)
	router.ServeHTTPC(context.TODO(), w, req)
	if w.Code != http.StatusOK || w.Body.String() != "GET /handler" {
		t.Errorf("GET /handler failed")
	}

	w = httptest.NewRecorder()
	req, _ = http.NewRequest("GET", "/handlerfunc", nil)
	router.ServeHTTPC(context.TODO(), w, req)
	if w.Code != http.StatusOK || w.Body.String() != "GET /handlerfunc" {
		t.Errorf("GET /handlerfunc failed")
	}

	w = httptest.NewRecorder()
	req, _ = http.NewRequest("GET", "/contexthandler", nil)
	router.ServeHTTPC(context.WithValue(context.Background(), 1, "Haha!"), w, req)
	if w.Code != http.StatusOK || w.Body.String() != "GET /contexthandler, value(1)=Haha!" {
		t.Errorf("GET /contexthandler failed")
	}

	w = httptest.NewRecorder()
	req, _ = http.NewRequest("GET", "/contexthandlerfunc", nil)
	router.ServeHTTPC(context.WithValue(context.Background(), 2, "We are family!"), w, req)
	if w.Code != http.StatusOK || w.Body.String() != "GET /contexthandlerfunc, value(2)=We are family!" {
		t.Errorf("GET /contexthandlerfunc failed")
	}
}