diff --git a/plugin/geodns/geodns_test.go b/plugin/geodns/geodns_test.go index ae33660c567..099022c43ae 100644 --- a/plugin/geodns/geodns_test.go +++ b/plugin/geodns/geodns_test.go @@ -20,11 +20,18 @@ func TestFiltering(t *testing.T) { ctx := context.Background() // to test healthchecker - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - }) go func() { - err := http.ListenAndServe("0.0.0.0:8080", nil) + mux := http.NewServeMux() + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + srv := http.Server{ + Addr: "0.0.0.0:8080", + Handler: mux, + } + + err := srv.ListenAndServe() require.NoError(t, err) }() @@ -80,7 +87,7 @@ func TestFiltering(t *testing.T) { "test.neofs": tc.records, }) geoDNS.filter.health.schema = "http://" - geoDNS.filter.health.port = ":8080" + geoDNS.filter.health.port = "8080" for _, ip := range []string{Orgrimar, WarsongHold, Stormwind} { _ = geoDNS.filter.health.cache.Set(ip, true) } diff --git a/plugin/geodns/health.go b/plugin/geodns/health.go index c3205f91301..293f487e247 100644 --- a/plugin/geodns/health.go +++ b/plugin/geodns/health.go @@ -21,6 +21,9 @@ func newHealthChecker() *checker { cache: gcache.New(1000).Expiration(30 * time.Second).Build(), client: &http.Client{ Timeout: 2 * time.Second, + CheckRedirect: func(*http.Request, []*http.Request) error { + return http.ErrUseLastResponse + }, }, schema: "http://", port: "80", diff --git a/plugin/geodns/health_test.go b/plugin/geodns/health_test.go new file mode 100644 index 00000000000..79439ee2602 --- /dev/null +++ b/plugin/geodns/health_test.go @@ -0,0 +1,36 @@ +package geodns + +import ( + "net/http" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +func TestNoRedirect(t *testing.T) { + go func() { + mux := http.NewServeMux() + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "/redirected", http.StatusMovedPermanently) + }) + mux.HandleFunc("/redirected", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusInternalServerError) + }) + srv := http.Server{ + Addr: "localhost:8888", + Handler: mux, + } + err := srv.ListenAndServe() + require.NoError(t, err) + }() + + checker := newHealthChecker() + checker.port = "8888" + + condition := func() bool { + return checker.checkOne("127.0.0.1") + } + + require.Eventually(t, condition, 5*time.Second, 500*time.Millisecond) +}