-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttpProxy_test.go
More file actions
64 lines (55 loc) · 1.56 KB
/
Copy pathhttpProxy_test.go
File metadata and controls
64 lines (55 loc) · 1.56 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
package vproxy
import (
"bufio"
"bytes"
"fmt"
"io"
"net"
"net/http"
"testing"
"time"
"github.com/issue9/assert/v4"
)
func Test_httpProxy_ServeHTTP(t *testing.T) {
as := assert.New(t, true)
tests := []struct {
method string
addr string
url string
req string
statusCode int
}{
{req: "GET http://www.baidu.com/index.html HTTP/1.1\r\nHost:abcdef\r\nConnection:Keep-Alive\r\n\r\n", statusCode: 200},
{req: "GET https://www.baidu.com/ HTTP/1.1\r\nHost:www.baidu.com\r\nConnection:Keep-Alive\r\n\r\n", statusCode: 200},
{req: "GET /index.html?123 HTTP/1.1\r\nHost:www.baidu.com\r\nConnection:Keep-Alive\r\n\r\n", statusCode: 200},
{req: "GET https://kyfw.12306.cn/ HTTP/1.1\r\nHost:kyfw.12306.cn\r\nConnection:Keep-Alive\r\n\r\n", statusCode: 302},
}
// 服务器
hp := newProxyHTTP(&Proxy{
DataBufioSize: 1024,
DialContext: defaultDial.DialContext,
})
srv := &http.Server{
Handler: http.HandlerFunc(hp.ServeHTTP),
}
l, err := net.Listen("tcp", "127.0.0.1:0")
as.NotError(err)
defer l.Close()
laddr := l.Addr().String()
fmt.Println("服务器IP: ", laddr)
go srv.Serve(l)
time.Sleep(time.Second * 2)
for _, test := range tests {
netConn, err := net.Dial("tcp", laddr)
as.NotError(err)
req, err := http.ReadRequest(bufio.NewReader(bytes.NewBufferString(test.req)))
as.NotError(err)
req.WriteProxy(netConn)
br := bufio.NewReader(netConn)
res, err := http.ReadResponse(br, &http.Request{})
as.NotError(err)
as.Equal(res.StatusCode, test.statusCode)
io.Copy(io.Discard, res.Body)
netConn.Close()
}
}