Beispiel #1
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)
	}
}
Beispiel #2
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)
		}
	}
}