This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathflags_test.go
64 lines (61 loc) · 2.42 KB
/
flags_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
package gandalf
import (
"bytes"
"fmt"
"net/http"
"testing"
)
func TestHostOverrideFlags(t *testing.T) {
cases := []struct {
urlin string
https bool
root string
ohost string
osuffix string
urlout string
}{
{"http://target", false, "", "", "", "http://target"},
{"http://target", false, "", "", ".lan", "http://target.lan"},
{"http://target/foo", false, "", "", ".lan", "http://target.lan/foo"},
{"http://target", false, "", "mock", "", "http://mock"},
{"http://target/foo", false, "", "mock", "", "http://mock/foo"},
{"http://target/foo", false, "", "mock", ".lan", "http://mock.lan/foo"},
{"http://target", false, "", "mock:8080", "", "http://mock:8080"},
{"http://target/foo", false, "", "mock:8080", "", "http://mock:8080/foo"},
{"http://target", false, "", "mock:8080", ".lan", "http://mock.lan:8080"},
{"http://target/foo", false, "", "mock:8080", ".lan", "http://mock.lan:8080/foo"},
{"http://target/foo", false, "", "mock:8080", "lan", "http://mock.lan:8080/foo"},
{"http://target/foo", true, "", "mock:8080", ".lan", "https://mock.lan:8080/foo"},
{"https://target/foo", true, "", "mock:8080", ".lan", "https://mock.lan:8080/foo"},
{"https://target/foo", false, "", "mock:8080", ".lan", "https://mock.lan:8080/foo"},
{"http://target/foo", false, "api", "mock:8080", "lan", "http://mock.lan:8080/api/foo"},
{"http://target/foo", false, "/api", "mock:8080", "lan", "http://mock.lan:8080/api/foo"},
{"http://target/foo", false, "/api/", "mock:8080", "lan", "http://mock.lan:8080/api/foo"},
{"http://target/foo", false, "api/", "mock:8080", "lan", "http://mock.lan:8080/api/foo"},
}
overrides := func(host, suffix, root string, https bool) {
OverrideHost = host
OverrideHostSuffix = suffix
OverrideHTTPS = https
OverrideWebroot = root
}
for i, tt := range cases {
t.Run(fmt.Sprintf("Case %d", i+1), func(st *testing.T) {
defer overrides(OverrideHost, OverrideHostSuffix, OverrideWebroot, OverrideHTTPS)
overrides(tt.ohost, tt.osuffix, tt.root, tt.https)
req, err := http.NewRequest("GET", tt.urlin, bytes.NewBuffer([]byte{}))
if err != nil {
st.Fatal(err)
}
if req.URL.String() != tt.urlin {
st.Fatalf("Request URL %s does not match input %s", req.URL.String(), tt.urlin)
}
maybeOverrideHost(req)
if req.URL.String() != tt.urlout {
st.Fatalf("Request URL post override %s does not match expected %s",
req.URL.String(),
tt.urlout)
}
})
}
}