Пример #1
0
func TestWithAccessLog(t *testing.T) {
	var buf bytes.Buffer
	defer buf.Reset()

	ctx := ctxlog.NewContext(context.Background(), log.NewStdLogger(log.SetStdWriter(&buf)))

	finalH := ctxhttp.Chain(
		ctxhttp.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
			w.WriteHeader(http.StatusTeapot)
			_, err := w.Write([]byte{'1', '2', '3'})
			time.Sleep(time.Millisecond)
			return err
		}),
		ctxhttp.WithAccessLog(),
	)

	r, _ := http.NewRequest("GET", "/gopherine", nil)
	r.RemoteAddr = "127.0.0.1"
	r.Header.Set("User-Agent", "Mozilla")
	r.Header.Set("Referer", "http://rustlang.org")

	w := httptest.NewRecorder()
	if err := finalH.ServeHTTPContext(ctx, w, r); err != nil {
		t.Fatal(err)
	}

	assert.Exactly(t, `123`, w.Body.String())
	assert.Exactly(t, http.StatusTeapot, w.Code)

	want1 := `request error: "" method: "GET" uri: "/gopherine" type: "access" status: "error" status_code: 418 duration:`
	want2 := `size: 3 remote_addr: "127.0.0.1" user_agent: "Mozilla" referer: "http://rustlang.org"`
	assert.Contains(t, buf.String(), want1)
	assert.Contains(t, buf.String(), want2)
}
Пример #2
0
func TestWithCompressorDeflateHeader(t *testing.T) {
	finalCH := ctxhttp.Chain(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
		assert.Exactly(t, httputil.CompressDeflate, w.Header().Get(httputil.ContentEncoding))
		assert.Exactly(t, httputil.AcceptEncoding, w.Header().Get(httputil.Vary))
		return nil
	}, ctxmw.WithCompressor())

	w, r := testCompressReqRes()
	r.Header.Set(httputil.AcceptEncoding, "deflate")
	if err := finalCH.ServeHTTPContext(context.TODO(), w, r); err != nil {
		t.Fatal(err)
	}
}
Пример #3
0
func TestWithTimeoutHandler(t *testing.T) {
	ctx := context.WithValue(context.Background(), ctxKey, "gopher life")
	finalCH := ctxhttp.Chain(&handler{}, ctxhttp.WithTimeout(time.Second))

	w := httptest.NewRecorder()
	r, err := http.NewRequest("GET", "http://corestore.io/catalog/product/id/3452", nil)
	if err != nil {
		t.Fatal(err)
	}
	if err := finalCH.ServeHTTPContext(ctx, w, r); err != nil {
		t.Fatal(err)
	}
	assert.Equal(t, "gopher life with deadline", w.Body.String())
}
Пример #4
0
func TestWithCloseHandler(t *testing.T) {
	ctx := context.WithValue(context.Background(), ctxKey, "gopher life")
	finalCH := ctxhttp.Chain(&handler{}, ctxhttp.WithCloseNotify())

	w := &closeNotifyWriter{httptest.NewRecorder()}
	r, err := http.NewRequest("GET", "http://corestore.io/catalog/product/id/3452", nil)
	if err != nil {
		t.Fatal(err)
	}
	if err := finalCH.ServeHTTPContext(ctx, w, r); err != nil {
		t.Fatal(err)
	}
	assert.Equal(t, "gopher life canceled", w.Body.String())
}
Пример #5
0
func (a *app) setupStoreRoutes(rtr *ctxrouter.Router) {

	//	eg1 := e.Group(httputils.APIRoute.String(), a.jwtSrv.WithParseAndValidate())

	path := httputil.APIRoute.String() + store.RouteStores

	rtr.Handler("GET", path,
		ctxhttp.Chain(jsonStores, a.jwtSrv.WithParseAndValidate()),
	)

	//	eg1.Get(store.RouteStores, store.RESTStores(sm))
	//	eg1.POST(store.RouteStores, store.RESTStoreCreate)
	//	eg1.GET(store.RouteStore, store.RESTStore)
	//	eg1.PUT(store.RouteStore, store.RESTStoreSave)
	//	eg1.DELETE(store.RouteStore, store.RESTStoreDelete)
}
Пример #6
0
func testWithRequestID(t *testing.T, gen ctxmw.RequestIDGenerator) {
	finalCH := ctxhttp.Chain(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
		id := w.Header().Get(httputil.RequestIDHeader)
		assert.Exactly(t, "-2", id[len(id)-2:])
		assert.Contains(t, id, "/")
		return nil
	}, ctxmw.WithRequestID(gen))

	w := httptest.NewRecorder()
	r, err := http.NewRequest("GET", "http://corestore.io/catalog/product/id/3452", nil)
	if err != nil {
		t.Fatal(err)
	}
	if err := finalCH.ServeHTTPContext(context.TODO(), w, r); err != nil {
		t.Fatal(err)
	}
}
Пример #7
0
func TestAdapters(t *testing.T) {

	hndlr := ctxhttp.Chain(
		h1{},
		ctxhttp.WithXHTTPMethodOverride(),
		ctxhttp.WithHeader("X-Men", "Y-Women"),
	)

	w := httptest.NewRecorder()
	req, err := http.NewRequest(httputils.MethodGet, "http://example.com/foo", nil)
	req.Header.Set(httputils.MethodOverrideHeader, httputils.MethodPut)
	assert.NoError(t, err)

	a := ctxhttp.NewAdapter(context.Background(), hndlr)
	a.ServeHTTP(w, req)

	assert.Equal(t, httputils.MethodPut, req.Method)
	assert.Equal(t, "h1 called", w.Body.String())
	assert.Equal(t, "Y-Women", w.Header().Get("X-Men"))
}
Пример #8
0
// BenchmarkWithCompressorGZIP_1024B-4	   20000	     81916 ns/op	    1330 B/op	       5 allocs/op
func BenchmarkWithCompressorGZIP_1024B(b *testing.B) {

	rawData := randSeq(1024)

	finalCH := ctxhttp.Chain(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
		return httputil.NewPrinter(w, r).WriteString(http.StatusOK, rawData)
	}, ctxmw.WithCompressor())

	w, r := testCompressReqRes()
	r.Header.Set(httputil.AcceptEncoding, httputil.CompressGZIP)

	ctx := context.TODO()
	b.ResetTimer()
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		if err := finalCH.ServeHTTPContext(ctx, w, r); err != nil {
			b.Fatal(err)
		}
		w.Body.Reset()
	}
}
Пример #9
0
func testWithCompressorConcrete(t *testing.T, header string, uncompressor func(io.Reader) string) {

	rawData := randSeq(1024)

	finalCH := ctxhttp.Chain(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
		return httputil.NewPrinter(w, r).WriteString(http.StatusOK, rawData)
	}, ctxmw.WithCompressor())

	w, r := testCompressReqRes()
	r.Header.Set(httputil.AcceptEncoding, header)
	if err := finalCH.ServeHTTPContext(context.TODO(), w, r); err != nil {
		t.Fatal(err)
	}
	assert.False(t, len(rawData) < len(w.Body.Bytes()))

	uncompressedBody := uncompressor(w.Body)

	assert.Exactly(t, rawData, uncompressedBody)
	assert.Exactly(t, header, w.Header().Get(httputil.ContentEncoding))
	assert.Exactly(t, httputil.AcceptEncoding, w.Header().Get(httputil.Vary))
	assert.Exactly(t, httputil.TextPlain, w.Header().Get(httputil.ContentType))

}
Пример #10
0
// BenchmarkWithRequestID-4	 3000000	       432 ns/op	      64 B/op	       3 allocs/op
func BenchmarkWithRequestID(b *testing.B) {

	finalCH := ctxhttp.Chain(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
		id := w.Header().Get(httputil.RequestIDHeader)
		if id == "" {
			b.Fatal("id is empty")
		}
		return nil
	}, ctxmw.WithRequestID())

	w := httptest.NewRecorder()
	r, err := http.NewRequest("GET", "http://corestore.io/catalog/product/id/3452", nil)
	if err != nil {
		b.Fatal(err)
	}
	ctx := context.TODO()
	b.ResetTimer()
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		if err := finalCH.ServeHTTPContext(ctx, w, r); err != nil {
			b.Fatal(err)
		}
	}
}
Пример #11
0
func TestHttpMethodOverride(t *testing.T) {
	hndlr := ctxhttp.Chain(
		h1{},
		ctxhttp.WithXHTTPMethodOverride())
	w := httptest.NewRecorder()
	req, err := http.NewRequest(httputils.MethodGet, "http://example.com/foo?_method="+httputils.MethodPatch, nil)
	assert.NoError(t, err)
	hndlr.ServeHTTPContext(context.Background(), w, req)
	assert.Equal(t, httputils.MethodPatch, req.Method)
	assert.Equal(t, "h1 called", w.Body.String())

	w = httptest.NewRecorder()
	req, err = http.NewRequest(httputils.MethodGet, "http://example.com/foo?_method=KARATE", nil)
	assert.NoError(t, err)
	hndlr.ServeHTTPContext(context.Background(), w, req)
	assert.Equal(t, httputils.MethodGet, req.Method)

	w = httptest.NewRecorder()
	req, err = http.NewRequest(httputils.MethodGet, "http://example.com/foobar", nil)
	assert.NoError(t, err)
	hndlr.ServeHTTPContext(context.Background(), w, req)
	assert.Equal(t, httputils.MethodGet, req.Method)

}
func ExampleWithInitStoreByToken() {
	initStore()
	ctx := store.NewContextReader(context.Background(), testStoreService)

	jwtService, err := ctxjwt.NewService(ctxjwt.WithPassword([]byte(`GÒph3r`)))

	finalHandler := ctxhttp.Chain(
		ctxhttp.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
			_, haveReqStore, err := store.FromContextReader(ctx)
			if err != nil {
				return err
			}
			// now we know that the current request depends on the store view DE.
			fmt.Fprintf(w, "StoreCode: %s\n", haveReqStore.StoreCode())
			return nil
		}), // 								  executed 3rd
		store.WithInitStoreByToken(),      // executed 2nd
		jwtService.WithParseAndValidate(), // executed 1st
	)

	ts := httptest.NewServer(ctxhttp.NewAdapter(ctx, finalHandler))
	defer ts.Close()

	// Setup GET request
	token, _, err := jwtService.GenerateToken(
		map[string]interface{}{
			// Despite default store for Website ID 1 is AT we are currently
			// in the store context of DE.
			store.ParamName: "de",
		},
	)
	if err != nil {
		log.Fatal("jwtService.GenerateToken", "err", err)
	}

	req, err := http.NewRequest("GET", ts.URL, nil)
	if err != nil {
		log.Fatal("http.Get", "err", err)
	}
	ctxjwt.SetHeaderAuthorization(req, token)

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Fatal("http.DefaultClient.Do", "err", err)
	}

	response, err := ioutil.ReadAll(res.Body)
	if errC := res.Body.Close(); errC != nil {
		log.Fatal("res.Body.Close", "err", errC)
	}

	if err != nil {
		log.Fatal("ioutil.ReadAll", "err", err)
	}

	fmt.Printf("Response: %s\n", response)
	fmt.Printf("Log: %s\n", testDebugLogBuf.String())
	// Output:
	// Response: StoreCode: de
	//
	// Log:
}