forked from michaljanocko/pancors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpancors_test.go
169 lines (154 loc) · 3.98 KB
/
pancors_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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package pancors
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
type expected struct {
statusCode int
headers map[string]string
}
type params struct {
userAgent string
referer string
}
type test struct {
name string
url string
params params
expected expected
}
func TestProxy(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(HandleProxy))
defer testServer.Close()
testCustomServer := httptest.NewServer(http.HandlerFunc(HandleProxyWith("example.com", "false")))
defer testCustomServer.Close()
echoServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "OK")
t.Log("one")
}))
defer echoServer.Close()
refererServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Referer") == "https://example.com" {
fmt.Fprint(w, "Referer is OK")
} else {
http.Error(w, "Referer is not 'https://example.com'", http.StatusBadRequest)
}
}))
defer refererServer.Close()
tests := []test{
{
"HTTPS URL with params",
echoServer.URL,
params{userAgent: "Test"},
expected{
http.StatusOK,
map[string]string{
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true",
},
},
},
{
"HTTP URL with params",
echoServer.URL,
params{userAgent: "Test"},
expected{
http.StatusOK,
map[string]string{
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true",
},
},
},
{
"Empty URL",
"",
params{userAgent: "Test"},
expected{statusCode: http.StatusBadRequest},
},
{
"Non-HTTP(S) URL",
"ftp://example.com",
params{userAgent: "Test"},
expected{statusCode: http.StatusBadRequest},
},
{
"Referer header",
refererServer.URL,
params{userAgent: "Test", referer: "https://example.com"},
expected{statusCode: http.StatusOK},
},
{
"Wrong Referer header",
refererServer.URL,
params{userAgent: "Test", referer: "https://elpmaxe.com"},
expected{statusCode: http.StatusBadRequest},
},
{
"Missing User-Agent header",
echoServer.URL,
params{},
expected{statusCode: http.StatusBadRequest},
},
}
customTests := []test{
{
"Success reponse with specified custom headers",
echoServer.URL,
params{userAgent: "Test"},
expected{
http.StatusOK,
map[string]string{
"Access-Control-Allow-Origin": "example.com",
"Access-Control-Allow-Credentials": "false",
},
},
},
}
testServerURL, err := url.Parse(testServer.URL)
if err != nil {
t.Fatalf("Could not parse test server's URL, got %v", err)
}
q := testServerURL.Query()
for _, testCase := range tests {
t.Run(testCase.name, runTest(testCase, testServerURL, q))
}
testServerURL, err = url.Parse(testCustomServer.URL)
if err != nil {
t.Fatalf("Could not parse test server's URL, got %v", err)
}
q = testServerURL.Query()
for _, testCase := range customTests {
t.Run(testCase.name, runTest(testCase, testServerURL, q))
}
}
func runTest(testCase test, testServerURL *url.URL, q url.Values) func(*testing.T) {
return func(t *testing.T) {
q.Set("url", testCase.url)
q.Set("referer", testCase.params.referer)
testServerURL.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(context.Background(), "GET", testServerURL.String(), nil)
if err != nil {
t.Fatalf("Could not prepare a request, got %v", err)
}
req.Header.Add("User-Agent", testCase.params.userAgent)
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("Could not fetch testing data")
}
defer res.Body.Close()
if res.StatusCode != testCase.expected.statusCode {
t.Errorf("Expected HTTP status code %d, got %d", testCase.expected.statusCode, res.StatusCode)
}
for header, expected := range testCase.expected.headers {
actual := res.Header.Get(header)
if actual != expected {
t.Errorf("Expected header %s to be %s, got: %v", header, expected, actual)
}
}
}
}