-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredirect_test.go
79 lines (61 loc) · 2.58 KB
/
redirect_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package requests
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRedirectPolicies(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/no-redirect":
_, _ = w.Write([]byte("no redirect"))
case "/redirect-1":
http.Redirect(w, r, "/redirect-2", http.StatusFound)
case "/redirect-2":
http.Redirect(w, r, "/final", http.StatusFound)
case "/final":
_, _ = w.Write([]byte("final destination"))
}
}))
defer ts.Close()
t.Run("ProhibitRedirectPolicy", func(t *testing.T) {
client := Create(nil)
client.SetRedirectPolicy(NewProhibitRedirectPolicy())
_, err := client.Get(ts.URL + "/redirect-1").Send(context.Background())
assert.Error(t, err, "Expected to receive redirect error")
assert.ErrorIs(t, err, ErrAutoRedirectDisabled, "Expected auto redirect disabled error")
})
t.Run("AllowRedirectPolicy", func(t *testing.T) {
client := Create(nil)
client.SetRedirectPolicy(NewAllowRedirectPolicy(3))
resp, err := client.Get(ts.URL + "/redirect-1").Send(context.Background())
assert.NoError(t, err, "Expected no errors")
assert.Equal(t, http.StatusOK, resp.StatusCode(), "Expected status code to be 200")
defer resp.Close() //nolint:errcheck
})
t.Run("AllowRedirectPolicy-ExceedsLimit", func(t *testing.T) {
client := Create(nil)
client.SetRedirectPolicy(NewAllowRedirectPolicy(1))
_, err := client.Get(ts.URL + "/redirect-1").Send(context.Background())
assert.Error(t, err, "Expected to receive redirection limit error")
assert.EqualError(t, err, "Get \"/redirect-2\": stopped after 1 redirects: too many redirects")
})
t.Run("RedirectSpecifiedDomainPolicy", func(t *testing.T) {
client := Create(&Config{BaseURL: ts.URL})
host := "127.0.0.1"
client.SetRedirectPolicy(NewRedirectSpecifiedDomainPolicy(host))
resp, err := client.Get("/redirect-1").Send(context.Background())
assert.NoError(t, err, "Expected no errors")
assert.Equal(t, http.StatusOK, resp.StatusCode(), "Expected status code to be 200")
defer resp.Close() //nolint:errcheck
})
t.Run("RedirectSpecifiedDomainPolicy-ProhibitDomain", func(t *testing.T) {
client := Create(nil)
client.SetRedirectPolicy(NewRedirectSpecifiedDomainPolicy("other.domain.com"))
_, err := client.Get(ts.URL + "/redirect-1").Send(context.Background())
assert.Error(t, err, "Expected domain restriction error")
assert.EqualError(t, err, "Get \"/redirect-2\": redirect is not allowed as per RedirectSpecifiedDomainPolicy", "Expected domain not allowed error")
})
}