Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
[#15] Fix redirects
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Kirillov <denis@nspcc.ru>
  • Loading branch information
KirillovDenis authored and alexvanin committed Nov 9, 2021
1 parent fc21ced commit 2766190
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
17 changes: 12 additions & 5 deletions plugin/geodns/geodns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}()

Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions plugin/geodns/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
36 changes: 36 additions & 0 deletions plugin/geodns/health_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 2766190

Please sign in to comment.