-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
redirect_test.go
62 lines (49 loc) · 1.61 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
package main
import (
"net/http"
"net/url"
"testing"
)
func TestGetRedirectGet(t *testing.T) {
// Constructed URL to match a nginx redirect:
// `return 302 https://login.luzifer.io/login?go=$scheme://$http_host$request_uri;`
testURL := "https://example.com/login?go=https://example.com/inner?foo=bar&bar=foo"
expectURL := "https://example.com/inner?bar=foo&foo=bar"
req, _ := http.NewRequest(http.MethodGet, testURL, nil)
rURL, err := getRedirectURL(req, "")
if err != nil {
t.Errorf("getRedirectURL caused an error in GET: %s", err)
}
if expectURL != rURL {
t.Errorf("Result did not match expected URL: %q != %q", rURL, expectURL)
}
}
func TestGetRedirectFallback(t *testing.T) {
testURL := "https://example.com/login"
expectURL := "https://example.com/default"
req, _ := http.NewRequest(http.MethodGet, testURL, nil)
rURL, err := getRedirectURL(req, expectURL)
if err != nil {
t.Errorf("getRedirectURL caused an error in GET: %s", err)
}
if expectURL != rURL {
t.Errorf("Result did not match expected URL: %q != %q", rURL, expectURL)
}
}
func TestGetRedirectPost(t *testing.T) {
testURL := "https://example.com/login"
expectURL := "https://example.com/inner?foo=bar"
body := url.Values{
"go": []string{expectURL},
"other": []string{"param"},
}
req, _ := http.NewRequest(http.MethodPost, testURL, nil)
req.Form = body // Force-set the form values to emulate parsed form
rURL, err := getRedirectURL(req, "")
if err != nil {
t.Errorf("getRedirectURL caused an error in POST: %s", err)
}
if expectURL != rURL {
t.Errorf("Result did not match expected URL: %q != %q", rURL, expectURL)
}
}