-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_test.go
More file actions
208 lines (194 loc) · 5.21 KB
/
proxy_test.go
File metadata and controls
208 lines (194 loc) · 5.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"testing"
)
func TestProxy_ServeHTTP(t *testing.T) {
cases := []struct {
baseDomain string
listenAddr string
listenPort string
destAddr string
destPort string
expectMsg string
expectCode int
}{
{
baseDomain: "127.0.0.1.xip.io",
listenAddr: "example.127.0.0.1.xip.io",
listenPort: "6335",
destAddr: "127.0.0.1",
destPort: "3665",
expectMsg: "hello",
expectCode: 200,
},
}
for _, tc := range cases {
// Create test server
tDestServer := newTestDestServer(tc.destAddr, tc.destPort, tc.expectMsg)
tDestServer.Start()
defer tDestServer.Close()
// Set test server URL to testDB
testDB := newTestDB(tc.destAddr+":"+tc.destPort, "")
defer testDB.close()
// Create proxy
tProxyServer, err := newTestProxyServer(tc.listenPort, tc.destPort, tc.baseDomain, testDB, tc.listenAddr)
if err != nil {
t.Fatal(err)
}
tProxyServer.Start()
defer tProxyServer.Close()
// Get upstream response
resp, err := http.Get(tProxyServer.URL)
if err != nil {
t.Fatalf("Failed to http.Get(proxy): %s", err)
}
defer resp.Body.Close()
actualMsg, _ := ioutil.ReadAll(resp.Body)
actualCode := resp.StatusCode
if string(actualMsg) != tc.expectMsg {
t.Fatalf("Response Body should be '%s', but '%s'", tc.expectMsg, string(actualMsg))
}
if actualCode != tc.expectCode {
t.Fatalf("Response StatusCode should be '%d', but '%d'", tc.expectCode, actualCode)
}
}
}
func newTestDestServer(destAddr, destPort, expectMsg string) *httptest.Server {
s := httptest.NewUnstartedServer(http.HandlerFunc(
func(w http.ResponseWriter, req *http.Request) {
fmt.Fprint(w, expectMsg)
}))
listener, _ := net.Listen("tcp", destAddr+":"+destPort)
s.Listener = listener
return s
}
func newTestProxyServer(listenPort, destPort, baseDomain string, testDB DB, listenAddr string) (*httptest.Server, error) {
p, err := newProxy(destPort, baseDomain, testDB, false)
if err != nil {
return nil, err
}
proxy := httptest.NewUnstartedServer(p)
listener, _ := net.Listen("tcp", listenAddr+":"+listenPort)
proxy.Listener = listener
return proxy, nil
}
func TestProxy_splitVirtualHostName(t *testing.T) {
cases := []struct {
host string
baseDomain string
expected string
}{
{
host: "example.foo.bar.127.0.0.1.xip.io:3342",
baseDomain: "127.0.0.1.xip.io",
expected: "example.foo.bar",
},
{
host: "foo.bar.example.com:80",
baseDomain: "example.com",
expected: "foo.bar",
},
}
for _, tc := range cases {
p, _ := newProxy("", tc.baseDomain, nil, false)
actual, err := p.splitVirtualHostName(tc.host)
if err != nil {
t.Fatal(err)
}
if tc.expected != actual {
t.Fatalf("Proxy.splitVirtualHostName('%s') => '%s', expected '%s'", tc.host, actual, tc.expected)
}
}
}
func TestProxy_fetchDestURL(t *testing.T) {
cases := []struct {
virtualHostName string
rawDestURL string
defaultDestURL string
expected string
}{
{
virtualHostName: "exist",
rawDestURL: "http://example.com:9999",
defaultDestURL: "",
expected: "http://example.com:9999",
},
{
virtualHostName: "not.exist",
rawDestURL: "",
defaultDestURL: "http://example.com:9999",
expected: "http://example.com:9999",
},
{
virtualHostName: "exist",
rawDestURL: "http://example.com:9999/{}/foo/{}",
defaultDestURL: "",
expected: "http://example.com:9999/exist/foo/exist",
},
}
for _, tc := range cases {
testDB := newTestDB(tc.rawDestURL, tc.defaultDestURL)
p, err := newProxy("", "", testDB, false)
if err != nil {
t.Fatal(err)
}
actualURL, err := p.fetchDestURL(tc.virtualHostName, "/")
if err != nil {
t.Fatal(err)
}
if actual := actualURL.String(); tc.expected != actual {
t.Fatalf("Proxy.fetchDestURL('%s') => '%s' ,expected '%s'", tc.virtualHostName, actual, tc.expected)
}
}
}
func TestProxy_buildDestURL(t *testing.T) {
cases := []struct {
rawDestURL string
virtualHostName string
destPort string
expected string
}{
{
rawDestURL: "http://example.com",
virtualHostName: "Virtual.Host.Name",
destPort: "9999",
expected: "http://example.com:9999",
},
{
rawDestURL: "http://example.com:5555",
virtualHostName: "Virtual.Host.Name",
destPort: "9999",
expected: "http://example.com:5555",
},
{
rawDestURL: "http://example.com?q1=foo&q2=bar",
virtualHostName: "Virtual.Host.Name",
destPort: "9999",
expected: "http://example.com:9999?q1=foo&q2=bar",
},
{
rawDestURL: "http://example.com/{}/foo/{}",
virtualHostName: "Virtual.Host.Name",
destPort: "9999",
expected: "http://example.com:9999/Virtual.Host.Name/foo/Virtual.Host.Name",
},
}
for _, tc := range cases {
p, err := newProxy(tc.destPort, "", nil, false)
if err != nil {
t.Fatal(err)
}
actualURL, err := p.buildDestURL(tc.rawDestURL, tc.virtualHostName)
if err != nil {
t.Fatal(err)
}
if actual := actualURL.String(); tc.expected != actual {
t.Fatalf("Proxy.buildDestURL('%s', '%s') => '%s', expected '%s'", tc.rawDestURL, tc.virtualHostName, actual, tc.expected)
}
}
}