// Benchmark_WithValidateBaseUrl-4 3000 489089 ns/op 188333 B/op 272 allocs/op => with debug enabled // Benchmark_WithValidateBaseUrl-4 200000 8925 ns/op 2924 B/op 49 allocs/op => no debug func Benchmark_WithValidateBaseUrl(b *testing.B) { // todo: there is room for optimization with disabled debugging. too many allocs store.PkgLog.SetLevel(log.StdLevelInfo) req, err := http.NewRequest(httputils.MethodGet, "https://corestore.io/customer/comments/view?id=1916#tab=ratings", nil) if err != nil { b.Fatal(err) } finalHandler := store.WithValidateBaseURL(middlewareConfigReader)(ctxhttp.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { return errors.New("This handler should not be called!") })) want := "https://www.corestore.io/customer/comments/view?id=1916#tab=ratings" rec := httptest.NewRecorder() b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { if err := finalHandler.ServeHTTPContext(middlewareCtxStoreService, rec, req); err != nil { b.Error(err) } if rec.HeaderMap.Get("Location") != want { b.Errorf("Have: %s\nWant: %s", rec.HeaderMap.Get("Location"), want) } rec.HeaderMap = nil } }
func TestWithValidateBaseUrl_ActivatedAndShouldNotRedirectWithPOSTRequest(t *testing.T) { mockReader := config.NewMockReader( config.WithMockValues(config.MockPV{ config.MockPathScopeDefault(store.PathRedirectToBase): 301, }), ) w := httptest.NewRecorder() req, err := http.NewRequest(httputils.MethodGet, "http://corestore.io/catalog/product/view", nil) assert.NoError(t, err) mw := store.WithValidateBaseURL(mockReader)(finalHandlerWithValidateBaseURL(t)) err = mw.ServeHTTPContext(context.Background(), w, req) assert.EqualError(t, err, store.ErrContextServiceNotFound.Error()) w = httptest.NewRecorder() req, err = http.NewRequest(httputils.MethodPost, "http://corestore.io/catalog/product/view", strings.NewReader(`{ "k1": "v1", "k2": { "k3": ["va1"] }}`)) assert.NoError(t, err) err = mw.ServeHTTPContext(context.Background(), w, req) assert.NoError(t, err) }
func TestWithValidateBaseUrl_ActivatedAndShouldRedirectWithGETRequest(t *testing.T) { var newReq = func(urlStr string) *http.Request { req, err := http.NewRequest(httputils.MethodGet, urlStr, nil) if err != nil { t.Fatal(err) } return req } tests := []struct { rec *httptest.ResponseRecorder req *http.Request wantRedirectURL string }{ { httptest.NewRecorder(), newReq("http://corestore.io/catalog/product/view/"), "http://www.corestore.io/catalog/product/view/", }, { httptest.NewRecorder(), newReq("http://corestore.io/catalog/product/view"), "http://www.corestore.io/catalog/product/view", }, { httptest.NewRecorder(), newReq("http://corestore.io"), "http://www.corestore.io", }, { httptest.NewRecorder(), newReq("https://corestore.io/catalog/category/view?catid=1916"), "https://www.corestore.io/catalog/category/view?catid=1916", }, { httptest.NewRecorder(), newReq("https://corestore.io/customer/comments/view?id=1916#tab=ratings"), "https://www.corestore.io/customer/comments/view?id=1916#tab=ratings", }, } for i, test := range tests { mw := store.WithValidateBaseURL(middlewareConfigReader)(ctxhttp.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { return fmt.Errorf("This handler should not be called! Iindex %d", i) })) assert.NoError(t, mw.ServeHTTPContext(middlewareCtxStoreService, test.rec, test.req), "Index %d", i) assert.Exactly(t, test.wantRedirectURL, test.rec.HeaderMap.Get("Location"), "Index %d", i) } }
func TestWithValidateBaseUrl_DeactivatedAndShouldNotRedirectWithGETRequest(t *testing.T) { mockReader := config.NewMockReader( config.WithMockValues(config.MockPV{ config.MockPathScopeDefault(store.PathRedirectToBase): 0, }), ) // no post request but check deactivated w := httptest.NewRecorder() req, err := http.NewRequest(httputils.MethodGet, "http://corestore.io/catalog/product/view", nil) assert.NoError(t, err) err = store.WithValidateBaseURL(mockReader)(finalHandlerWithValidateBaseURL(t)).ServeHTTPContext(context.Background(), w, req) assert.NoError(t, err) }