-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy_test.go
More file actions
240 lines (198 loc) · 5.48 KB
/
proxy_test.go
File metadata and controls
240 lines (198 loc) · 5.48 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package proxy
import (
"crypto/rand"
"net"
"net/http"
"net/http/httptest"
"net/http/httputil"
"testing"
"time"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/omalloc/proxy/selector"
)
func TestNew(t *testing.T) {
tests := []struct {
name string
opts []Option
}{
{
name: "default options",
opts: nil,
},
{
name: "with custom dialer",
opts: []Option{
WithDialer(&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 5 * time.Second,
}),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := New(tt.opts...)
assert.NotNil(t, p)
assert.NotNil(t, p.selector)
assert.NotNil(t, p.dialer)
assert.NotNil(t, p.clientMap)
})
}
}
func TestReverseProxy_Do(t *testing.T) {
// 创建测试服务器
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()
// 创建测试节点
node := &mockNode{scheme: "http", addr: ts.URL[7:]} // 移除 "http://" 前缀
p := New()
p.Apply([]selector.Node{node})
req, err := http.NewRequest("GET", ts.URL, nil)
assert.NoError(t, err)
resp, err := p.Do(req)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
func TestReverseProxy_Apply(t *testing.T) {
p := New()
nodes := []selector.Node{
&mockNode{scheme: "http", addr: "localhost:8080"},
&mockNode{scheme: "http", addr: "localhost:8081"},
}
p.Apply(nodes)
// 验证节点是否被正确应用
client := p.find("localhost:8080")
assert.NotNil(t, client)
}
func TestWithOptions(t *testing.T) {
mockActivate := func(client *http.Client) {}
tests := []struct {
name string
opt Option
}{
{
name: "WithInitialNodes",
opt: WithInitialNodes([]selector.Node{&mockNode{scheme: "http", addr: "localhost:8080"}}),
},
{
name: "WithActivateMock",
opt: WithActivateMock(mockActivate),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := New(tt.opt)
assert.NotNil(t, p)
})
}
}
func TestMockRequest(t *testing.T) {
// no node proxy
proxyClient := New(WithActivateMock(httpmock.ActivateNonDefault))
// mock 1.apk
httpmock.RegisterResponder(http.MethodGet, "http://example.com/path/to/1.apk", func(r *http.Request) (*http.Response, error) {
buf := make([]byte, 2<<10)
_, err := rand.Read(buf)
return httpmock.NewBytesResponse(http.StatusOK, buf), err
})
req, _ := http.NewRequest(http.MethodGet, "http://example.com/path/to/1.apk", nil)
resp, err := proxyClient.Do(req)
if err == nil {
t.Fatal(selector.ErrNoAvailable)
}
assert.Equal(t, err, selector.ErrNoAvailable)
assert.Nil(t, resp)
// add node
proxyClient.Apply([]selector.Node{&mockNode{scheme: "http", addr: "localhost:8888"}})
resp, err = proxyClient.Do(req)
assert.Nil(t, err)
assert.NotNil(t, resp)
assert.Equal(t, resp.StatusCode, http.StatusOK)
md, _ := httputil.DumpResponse(resp, false)
t.Logf("all response info: %v", string(md))
}
func TestReUseClient(t *testing.T) {
proxyClient := New(
WithInitialNodes([]selector.Node{&mockNode{"http", "127.0.0.1:8888"}}),
WithActivateMock(httpmock.ActivateNonDefault),
)
// mock 1.apk
httpmock.RegisterResponder(http.MethodGet, "http://example.com/path/to/1.apk", func(r *http.Request) (*http.Response, error) {
buf := make([]byte, 2<<10)
_, err := rand.Read(buf)
return httpmock.NewBytesResponse(http.StatusOK, buf), err
})
req, _ := http.NewRequest(http.MethodGet, "http://example.com/path/to/1.apk", nil)
resp, err := proxyClient.Do(req)
assert.NotEqual(t, err, selector.ErrNoAvailable)
assert.NotNil(t, resp)
assert.Equal(t, resp.StatusCode, http.StatusOK)
// re-use client
resp, err = proxyClient.Do(req)
assert.NotEqual(t, err, selector.ErrNoAvailable)
assert.NotNil(t, resp)
assert.Equal(t, resp.StatusCode, http.StatusOK)
}
func TestRedirectLocation(t *testing.T) {
proxyClient := New(
WithInitialNodes([]selector.Node{&mockNode{"http", "127.0.0.1:8888"}}),
WithActivateMock(httpmock.ActivateNonDefault),
)
// mock 1.apk
httpmock.RegisterResponder(http.MethodGet, "http://example.com/path/to/1.apk", func(r *http.Request) (*http.Response, error) {
buf := make([]byte, 2<<10)
_, err := rand.Read(buf)
return httpmock.NewBytesResponse(http.StatusOK, buf), err
})
redirectCount := 0
// mock 301
httpmock.RegisterResponder(http.MethodGet, "http://example.com/path/to/301", func(r *http.Request) (*http.Response, error) {
redirectCount++
if redirectCount > 10 {
return &http.Response{
StatusCode: http.StatusMovedPermanently,
Header: http.Header{
"Location": []string{"http://example.com/path/to/1.apk"},
},
}, nil
}
return &http.Response{
StatusCode: http.StatusMovedPermanently,
Header: http.Header{
"Location": []string{"http://example.com/path/to/301"},
},
}, nil
})
req, _ := http.NewRequest(http.MethodGet, "http://example.com/path/to/1.apk", nil)
resp, err := proxyClient.Do(req)
assert.Nil(t, err)
assert.NotEqual(t, err, selector.ErrNoAvailable)
assert.NotNil(t, resp)
assert.Equal(t, resp.StatusCode, http.StatusOK)
}
// mockNode 实现 selector.Node 接口
type mockNode struct {
scheme string
addr string
}
func (n *mockNode) Address() string {
return n.addr
}
func (n *mockNode) Scheme() string {
return n.scheme
}
func (n *mockNode) Weight() float64 {
return 1.0
}
func (n *mockNode) InitialWeight() *int64 {
return nil
}
func (n *mockNode) Version() string {
return "v1.0.0"
}
func (n *mockNode) Metadata() map[string]string {
return nil
}