コード例 #1
0
ファイル: service_test.go プロジェクト: joao-parana/csfw
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
	}
}
コード例 #2
0
ファイル: service_test.go プロジェクト: joao-parana/csfw
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
	}
}
コード例 #3
0
ファイル: service_test.go プロジェクト: joao-parana/csfw
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())
}
コード例 #4
0
ファイル: service_test.go プロジェクト: joao-parana/csfw
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))
}