-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirect_test.go
98 lines (87 loc) · 2.57 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//go:build !integration
// +build !integration
package egothic
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
func TestRedirect(t *testing.T) {
tests := []struct {
name string
url string
forceHTMLRedirect bool
expectedStatus int
expectedBody string
expectError bool
}{
{
name: "Valid URL - Server-side redirect",
url: "https://example.com",
forceHTMLRedirect: false,
expectedStatus: http.StatusSeeOther,
expectedBody: "",
expectError: false,
},
{
name: "Empty URL",
url: "",
forceHTMLRedirect: false,
expectedStatus: http.StatusBadRequest,
expectedBody: "Empty URL provided for redirect",
expectError: false,
},
{
name: "Client-side redirect",
url: "https://example.com",
forceHTMLRedirect: true,
expectedStatus: http.StatusOK,
expectedBody: "<html><head><meta http-equiv=\"refresh\" content=\"0;url=https://example.com\">",
expectError: false,
},
{
name: "Relative URL",
url: "/relative/path",
forceHTMLRedirect: false,
expectedStatus: http.StatusSeeOther,
expectedBody: "",
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
if tt.forceHTMLRedirect {
req.Header.Set("X-Force-HTML-Redirect", "true")
}
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
err := Redirect(c, tt.url)
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.expectedStatus, rec.Code)
if tt.expectedBody != "" {
assert.True(t, strings.Contains(rec.Body.String(), tt.expectedBody))
}
// Check headers
assert.Equal(t, "no-store, no-cache, must-revalidate, max-age=0", rec.Header().Get("Cache-Control"))
assert.Equal(t, "no-cache", rec.Header().Get("Pragma"))
assert.Equal(t, "0", rec.Header().Get("Expires"))
// Add new checks for redirect URL
if tt.expectedStatus == http.StatusSeeOther {
assert.Equal(t, tt.url, rec.Header().Get("Location"), "Location header should match the input URL")
} else if tt.forceHTMLRedirect {
expectedContent := fmt.Sprintf("0;url=%s", tt.url)
assert.Contains(t, rec.Body.String(), expectedContent, "HTML redirect content should contain the input URL")
}
})
}
}