Repository navigation
Expand file tree
/
Copy pathmain_test.go
More file actions
152 lines (136 loc) · 4.21 KB
/
Copy pathmain_test.go
File metadata and controls
152 lines (136 loc) · 4.21 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func newTestServer(t *testing.T, upstreamHandler http.HandlerFunc) (*httptest.Server, *httptest.Server) {
t.Helper()
upstream := httptest.NewServer(upstreamHandler)
t.Cleanup(upstream.Close)
upstreamURL, err := url.Parse(upstream.URL)
if err != nil {
t.Fatal(err)
}
proxy := httptest.NewServer(newHandler(newProxy(upstreamURL)))
t.Cleanup(proxy.Close)
return proxy, upstream
}
func TestForwardsRefererAndStripsCookie(t *testing.T) {
var gotReferer, gotCookie string
proxy, upstream := newTestServer(t, func(w http.ResponseWriter, r *http.Request) {
gotReferer = r.Header.Get("Referer")
gotCookie = r.Header.Get("Cookie")
w.Write([]byte("page"))
})
req, _ := http.NewRequest(http.MethodGet, proxy.URL+"/sugang/cc/cc103.action?openSchyy=2026", nil)
req.Header.Set("Cookie", "session=secret")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("status = %d, want 200", resp.StatusCode)
}
if gotReferer != upstream.URL {
t.Errorf("referer = %q, want %q", gotReferer, upstream.URL)
}
if gotCookie != "" {
t.Errorf("cookie forwarded upstream: %q", gotCookie)
}
}
func TestStripsSetCookie(t *testing.T) {
proxy, _ := newTestServer(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Set-Cookie", "JSESSIONID=abc")
w.Write([]byte("page"))
})
resp, err := http.Get(proxy.URL + "/sugang/cc/cc103.action")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if got := resp.Header.Get("Set-Cookie"); got != "" {
t.Errorf("set-cookie passed through: %q", got)
}
}
func TestCacheControlFallbackForStatic(t *testing.T) {
proxy, _ := newTestServer(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/kor/css/cached.css" {
w.Header().Set("Cache-Control", "no-store")
}
w.Write([]byte("body"))
})
resp, err := http.Get(proxy.URL + "/kor/css/style.css")
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if got := resp.Header.Get("Cache-Control"); got != "public, max-age=86400" {
t.Errorf("static cache-control = %q", got)
}
resp, err = http.Get(proxy.URL + "/kor/css/cached.css")
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if got := resp.Header.Get("Cache-Control"); got != "no-store" {
t.Errorf("upstream cache-control overridden: %q", got)
}
resp, err = http.Get(proxy.URL + "/sugang/cc/cc103.action")
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if got := resp.Header.Get("Cache-Control"); got != "" {
t.Errorf("page got cache-control: %q", got)
}
}
func TestPathWhitelist(t *testing.T) {
proxy, _ := newTestServer(t, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
})
cases := []struct {
method string
path string
want int
}{
{http.MethodGet, "/sugang/cc/cc103.action", http.StatusOK},
{http.MethodPost, "/sugang/cc/cc103ajax.action", http.StatusOK},
{http.MethodPost, "/sugang/cc/cc101ajax.action", http.StatusOK},
{http.MethodGet, "/kor/css/style.css", http.StatusOK},
{http.MethodGet, "/adm/js/default.js", http.StatusOK},
{http.MethodGet, "/sugang/co/co010.action", http.StatusNotFound},
{http.MethodGet, "/sugang/cc/cc200.action", http.StatusNotFound},
{http.MethodGet, "/sugang/cc/login.action", http.StatusNotFound},
{http.MethodDelete, "/sugang/cc/cc103.action", http.StatusMethodNotAllowed},
{http.MethodPost, "/kor/css/style.css", http.StatusMethodNotAllowed},
{http.MethodGet, "/", http.StatusNotFound},
{http.MethodGet, "/v1/lectures", http.StatusNotFound},
}
for _, c := range cases {
req, _ := http.NewRequest(c.method, proxy.URL+c.path, nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if resp.StatusCode != c.want {
t.Errorf("%s %s = %d, want %d", c.method, c.path, resp.StatusCode, c.want)
}
}
}
func TestHealthz(t *testing.T) {
proxy, _ := newTestServer(t, func(w http.ResponseWriter, r *http.Request) {
t.Error("healthz must not hit upstream")
})
resp, err := http.Get(proxy.URL + "/healthz")
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("status = %d, want 200", resp.StatusCode)
}
}