Esempio n. 1
0
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)

}
Esempio n. 2
0
func TestHTTPRateLimitConfig(t *testing.T) {
	cr := config.NewMockReader(
		config.WithMockValues(config.MockPV{
			config.MockPathScopeDefault(ctxthrottled.PathRateLimitBurst):    0,
			config.MockPathScopeDefault(ctxthrottled.PathRateLimitRequests): 1,
			config.MockPathScopeDefault(ctxthrottled.PathRateLimitDuration): "i",
		}),
	)

	limiter := ctxthrottled.HTTPRateLimit{
		Config: cr,
		VaryBy: &pathGetter{},
	}

	handler := limiter.WithRateLimit(nil, ctxhttp.HandlerFunc(func(_ context.Context, w http.ResponseWriter, _ *http.Request) error {
		w.WriteHeader(200)
		return nil
	}))

	runHTTPTestCases(t, handler, []httpTestCase{
		{"xx", 200, map[string]string{"X-Ratelimit-Limit": "1", "X-Ratelimit-Remaining": "0", "X-Ratelimit-Reset": "60"}},
		{"xx", 429, map[string]string{"X-Ratelimit-Limit": "1", "X-Ratelimit-Remaining": "0", "X-Ratelimit-Reset": "60", "Retry-After": "60"}},
	})
}
Esempio n. 3
0
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)
}
Esempio n. 4
0
func TestPasswordFromConfig(t *testing.T) {

	cfg := config.NewMockReader(
		config.WithMockValues(config.MockPV{
			config.MockPathScopeDefault(ctxjwt.PathJWTPassword): `Rump3lst!lzch3n`,
		}),
	)

	jm, err := ctxjwt.NewService(
		ctxjwt.WithPasswordFromConfig(cfg),
	)
	assert.NoError(t, err)

	theToken, _, err := jm.GenerateToken(nil)
	assert.NoError(t, err)
	assert.NotEmpty(t, theToken)

}
Esempio n. 5
0
func init() {
	middlewareConfigReader = config.NewMockReader(
		config.WithMockValues(config.MockPV{
			config.MockPathScopeDefault(store.PathRedirectToBase):    1,
			config.MockPathScopeStore(1, store.PathSecureInFrontend): true,
			config.MockPathScopeStore(1, store.PathUnsecureBaseURL):  "http://www.corestore.io/",
			config.MockPathScopeStore(1, store.PathSecureBaseURL):    "https://www.corestore.io/",
		}),
	)

	middlewareCtxStoreService = storemock.NewContextService(
		scope.Option{},
		func(ms *storemock.Storage) {
			ms.MockStore = func() (*store.Store, error) {
				return store.NewStore(
					&store.TableStore{StoreID: 1, Code: dbr.NewNullString("de"), WebsiteID: 1, GroupID: 1, Name: "Germany", SortOrder: 10, IsActive: true},
					&store.TableWebsite{WebsiteID: 1, Code: dbr.NewNullString("euro"), Name: dbr.NewNullString("Europe"), SortOrder: 0, DefaultGroupID: 1, IsDefault: dbr.NewNullBool(true)},
					&store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2},
					store.SetStoreConfig(middlewareConfigReader),
				)
			}
		},
	)
}
Esempio n. 6
0
func TestCtxIsSecure(t *testing.T) {
	tests := []struct {
		ctx          context.Context
		req          *http.Request
		wantIsSecure bool
	}{
		{
			context.Background(),
			func() *http.Request {
				r, err := http.NewRequest("GET", "/", nil)
				if err != nil {
					t.Fatal(err)
				}
				r.TLS = new(tls.ConnectionState)
				return r
			}(),
			true,
		},
		{
			config.WithContextMockGetter(context.Background(), config.WithMockValues(config.MockPV{
				config.MockPathScopeDefault(httputil.PathOffloaderHeader): "X_FORWARDED_PROTO",
			})),
			func() *http.Request {
				r, err := http.NewRequest("GET", "/", nil)
				if err != nil {
					t.Fatal(err)
				}
				r.Header.Set("HTTP_X_FORWARDED_PROTO", "https")
				return r
			}(),
			true,
		},
		{
			config.WithContextMockGetter(context.Background(), config.WithMockValues(config.MockPV{
				config.MockPathScopeDefault(httputil.PathOffloaderHeader): "X_FORWARDED_PROTO",
			})),
			func() *http.Request {
				r, err := http.NewRequest("GET", "/", nil)
				if err != nil {
					t.Fatal(err)
				}
				r.Header.Set("HTTP_X_FORWARDED_PROTO", "tls")
				return r
			}(),
			false,
		},
		{
			config.WithContextMockGetter(context.Background(), config.WithMockValues(config.MockPV{})),
			func() *http.Request {
				r, err := http.NewRequest("GET", "/", nil)
				if err != nil {
					t.Fatal(err)
				}
				r.Header.Set("HTTP_X_FORWARDED_PROTO", "does not matter")
				return r
			}(),
			false,
		},
	}
	for _, test := range tests {
		assert.Exactly(t, test.wantIsSecure, httputil.CtxIsSecure(test.ctx, test.req))
	}
}
Esempio n. 7
0
func TestStoreBaseURLandPath(t *testing.T) {

	s, err := store.NewStore(
		&store.TableStore{StoreID: 1, Code: dbr.NewNullString("de"), WebsiteID: 1, GroupID: 1, Name: "Germany", SortOrder: 10, IsActive: true},
		&store.TableWebsite{WebsiteID: 1, Code: dbr.NewNullString("admin"), Name: dbr.NewNullString("Admin"), SortOrder: 0, DefaultGroupID: 0, IsDefault: dbr.NewNullBool(false)},
		&store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "Default", RootCategoryID: 0, DefaultStoreID: 1},
	)
	assert.NoError(t, err)
	if s == nil {
		t.Fail()
	}

	tests := []struct {
		haveR        config.Reader
		haveUT       config.URLType
		haveIsSecure bool
		wantBaseUrl  string
		wantPath     string
	}{
		{
			config.NewMockReader(config.WithMockString(
				func(path string) (string, error) {
					switch path {
					case config.MockPathScopeDefault(store.PathSecureBaseURL):
						return "https://corestore.io", nil
					case config.MockPathScopeDefault(store.PathUnsecureBaseURL):
						return "http://corestore.io", nil
					}
					return "", config.ErrKeyNotFound
				},
			)),
			config.URLTypeWeb, true, "https://corestore.io/", "/",
		},
		{
			config.NewMockReader(config.WithMockString(
				func(path string) (string, error) {
					switch path {
					case config.MockPathScopeDefault(store.PathSecureBaseURL):
						return "https://myplatform.io/customer1", nil
					case config.MockPathScopeDefault(store.PathUnsecureBaseURL):
						return "http://myplatform.io/customer1", nil
					}
					return "", config.ErrKeyNotFound
				},
			)),
			config.URLTypeWeb, false, "http://myplatform.io/customer1/", "/customer1/",
		},
		{
			config.NewMockReader(config.WithMockString(
				func(path string) (string, error) {
					switch path {
					case config.MockPathScopeDefault(store.PathSecureBaseURL):
						return store.PlaceholderBaseURL, nil
					case config.MockPathScopeDefault(store.PathUnsecureBaseURL):
						return store.PlaceholderBaseURL, nil
					case config.MockPathScopeDefault(config.PathCSBaseURL):
						return config.CSBaseURL, nil
					}
					return "", config.ErrKeyNotFound
				},
			)),
			config.URLTypeWeb, false, config.CSBaseURL, "/",
		},
	}

	for i, test := range tests {
		s.ApplyOptions(store.SetStoreConfig(test.haveR))
		assert.NotNil(t, s.Config, "Index %d", i)
		baseURL, err := s.BaseURL(test.haveUT, test.haveIsSecure)
		assert.NoError(t, err)
		assert.EqualValues(t, test.wantBaseUrl, baseURL.String())
		assert.EqualValues(t, test.wantPath, s.Path())

		_, err = s.BaseURL(config.URLTypeAbsent, false)
		assert.EqualError(t, err, config.ErrURLCacheCleared.Error())
	}
}