コード例 #1
0
func TestMiddlewarePreempt(t *testing.T) {
	mux := NewMux()
	mux.Router.GET("/", httprouter.ContextHandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "router")
	}))
	mux.SetPreRouteMiddleware(middleware.WrapFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request, next httprouter.ContextHandler) {
		fmt.Fprint(w, "premiddleware")
		return
	}))
	resp := httptest.NewRecorder()
	req, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		t.Fatal(err)
	}
	mux.ServeHTTPContext(context.Background(), resp, req)

	if resp.Code != http.StatusOK {
		t.Error("Exected 200 response")
	}

	responseBody, err := resp.Body.ReadString(byte(0))
	if responseBody != "premiddleware" {
		t.Errorf("Middleware was not preemtive: %s", responseBody)
	}

}
コード例 #2
0
func TestFull(t *testing.T) {
	mux := NewMux()
	mux.Router.GET("/", httprouter.ContextHandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "router")
	}))
	mux.SetPreRouteMiddleware(middleware.Chain(middleware.WrapFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request, next httprouter.ContextHandler) {
		fmt.Fprint(w, "premw1")
		next.ServeHTTPContext(ctx, w, r)
		fmt.Fprint(w, "postmw1")
	}),
		middleware.WrapFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request, next httprouter.ContextHandler) {
			fmt.Fprint(w, "premw2")
			next.ServeHTTPContext(ctx, w, r)
			fmt.Fprint(w, "postmw2")
		})))

	resp := httptest.NewRecorder()
	req, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		t.Fatal(err)
	}
	mux.ServeHTTPContext(context.Background(), resp, req)

	if resp.Code != http.StatusOK {
		t.Error("Exected 200 response")
	}

	responseBody, err := resp.Body.ReadString(byte(0))
	if responseBody != "premw1premw2routerpostmw2postmw1" {
		t.Errorf("Wrong order of executing: %s", responseBody)
	}

}
コード例 #3
0
func TestTwoChains(t *testing.T) {
	var handled1, handled2 int

	var handler1Func = func(context.Context, http.ResponseWriter, *http.Request) {
		handled1++
	}

	var handler2Func = func(context.Context, http.ResponseWriter, *http.Request) {
		handled2++
	}

	var middleware1 = WrapFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request, next httprouter.ContextHandler) {
		next.ServeHTTPContext(ctx, w, r)
	})

	var middleware2 = WrapFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request, next httprouter.ContextHandler) {
		next.ServeHTTPContext(ctx, w, r)
	})

	var chain1 = Chain(NilMiddleware, middleware1, middleware2)
	handler1 := Curry(chain1, httprouter.ContextHandlerFunc(handler1Func))
	handler1.ServeHTTPContext(nil, nil, nil)

	var chain2 = Chain(NilMiddleware, middleware2)
	handler2 := Curry(chain2, httprouter.ContextHandlerFunc(handler2Func))
	handler2.ServeHTTPContext(nil, nil, nil)

	if handled1 > 1 {
		t.Error("Handler1 was executed twice")
	}

	if handled1 < 1 {
		t.Error("Handler1 was not executed")
	}

	if handled2 > 1 {
		t.Error("Handler2 was executed twice")
	}

	if handled2 < 1 {
		t.Error("Handler2 was not executed")
	}
}
コード例 #4
0
func TestChain(t *testing.T) {
	var chainBeforeNext []int
	var chainAfterNext []int
	var handled bool

	var handlerFunc = func(context.Context, http.ResponseWriter, *http.Request) {
		handled = true
	}

	var middleware1 = WrapFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request, next httprouter.ContextHandler) {
		chainBeforeNext = append(chainBeforeNext, 1)
		next.ServeHTTPContext(ctx, w, r)
		chainAfterNext = append(chainAfterNext, 1)
	})

	var middleware2 = WrapFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request, next httprouter.ContextHandler) {
		chainBeforeNext = append(chainBeforeNext, 2)
		next.ServeHTTPContext(ctx, w, r)
		chainAfterNext = append(chainAfterNext, 2)
	})

	var middleware3 = WrapFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request, next httprouter.ContextHandler) {
		chainBeforeNext = append(chainBeforeNext, 3)
		next.ServeHTTPContext(ctx, w, r)
		chainAfterNext = append(chainAfterNext, 3)
	})

	var chainedMiddleware = Chain(middleware1, middleware2, middleware3)

	chainedMiddleware.SetNext(httprouter.ContextHandlerFunc(handlerFunc))
	chainedMiddleware.ServeHTTPContext(nil, nil, nil)

	if !reflect.DeepEqual(chainBeforeNext, []int{1, 2, 3}) {
		t.Error("Middleware is not chained in right order. ChainBeforeNext is wrong")
	}

	if !reflect.DeepEqual(chainAfterNext, []int{3, 2, 1}) {
		t.Error("Middleware is not chained in right order. ChainAfterNext is wrong")
	}

	if !handled {
		t.Error("Final handler was not called")
	}
}