func ipErrorFinalHandler(t *testing.T) ctxhttp.HandlerFunc { return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { ipc, err, ok := geoip.FromContextCountry(ctx) assert.Nil(t, ipc) assert.True(t, ok) assert.EqualError(t, err, geoip.ErrCannotGetRemoteAddr.Error()) return nil } }
func finalHandlerFinland(t *testing.T) ctxhttp.HandlerFunc { return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { ipc, err, ok := geoip.FromContextCountry(ctx) assert.NotNil(t, ipc) assert.True(t, ok) assert.NoError(t, err) assert.Exactly(t, "2a02:d200::", ipc.IP.String()) assert.Exactly(t, "FI", ipc.Country.Country.IsoCode) return nil } }
func TestWithIsCountryAllowedByIPErrorStoreManager(t *testing.T) { s := mustGetTestService() defer deferClose(t, s.GeoIP) finalHandler := ctxhttp.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { ipc, err, ok := geoip.FromContextCountry(ctx) assert.Nil(t, ipc) assert.False(t, ok) assert.NoError(t, err) return nil }) countryHandler := s.WithIsCountryAllowedByIP()(finalHandler) rec := httptest.NewRecorder() req, err := http.NewRequest("GET", "http://corestore.io", nil) assert.NoError(t, err) assert.EqualError(t, countryHandler.ServeHTTPContext(context.Background(), rec, req), store.ErrContextServiceNotFound.Error()) }
func TestWithCountryByIPErrorGetCountryByIP(t *testing.T) { s := mustGetTestService() s.GeoIP = geoReaderMock{} defer deferClose(t, s.GeoIP) finalHandler := ctxhttp.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { ipc, err, ok := geoip.FromContextCountry(ctx) assert.Nil(t, ipc) assert.True(t, ok) assert.EqualError(t, err, "Failed to read country from MMDB") return nil }) countryHandler := s.WithCountryByIP()(finalHandler) rec := httptest.NewRecorder() req, err := http.NewRequest("GET", "http://corestore.io", nil) assert.NoError(t, err) req.Header.Set("X-Forwarded-For", "2a02:d200::") assert.NoError(t, countryHandler.ServeHTTPContext(context.Background(), rec, req)) }