-
Notifications
You must be signed in to change notification settings - Fork 3
/
request_test.go
157 lines (136 loc) · 3.51 KB
/
request_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
package gofofa
import (
"compress/gzip"
"fmt"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"net"
"net/http"
"net/http/httptest"
"testing"
)
var (
fetchHander = func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v1/gzip.json":
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
gz.Write([]byte(`{"text":"hello world"}`))
return
case "/api/v1/contentLengthError.json":
w.Header().Set("Content-Length", "aaa")
return
}
}
)
type tcpTestServer struct {
listener net.Listener
}
func (ts *tcpTestServer) URL() string {
return "http://" + ts.listener.Addr().String()
}
func (ts *tcpTestServer) Close() {
ts.listener.Close()
}
func newLocalListener() net.Listener {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
if l, err = net.Listen("tcp6", "[::1]:0"); err != nil {
panic(fmt.Sprintf("httptest: failed to listen on a port: %v", err))
}
}
return l
}
func newTcpTestServer(handler func(conn net.Conn, data []byte) error) *tcpTestServer {
ts := &tcpTestServer{
listener: newLocalListener(),
}
go func() {
data := make([]byte, 1024)
var err error
var n int
var conn net.Conn
for {
conn, err = ts.listener.Accept()
if err != nil {
break
}
n, err = conn.Read(data)
if err != nil {
break
}
err = handler(conn, data[:n])
if err != nil {
break
}
conn.Close()
}
}()
return ts
}
func TestClient_Fetch(t *testing.T) {
_, err := NewClient(WithURL("http://127.0.0.1:55"))
assert.Error(t, err)
ts := httptest.NewServer(http.HandlerFunc(fetchHander))
defer ts.Close()
cli := &Client{
Server: ts.URL,
APIVersion: "v1",
httpClient: &http.Client{},
logger: logrus.New(),
}
// 解析异常
var a map[string]interface{}
err = cli.Fetch("", nil, &a)
assert.Error(t, err)
// gzip
err = cli.Fetch("gzip.json", nil, &a)
assert.Nil(t, err)
assert.Equal(t, "hello world", a["text"].(string))
// content Length Error
err = cli.Fetch("contentLengthError.json", nil, &a)
assert.Contains(t, err.Error(), "unexpected end of JSON input")
// read all error 需要构造一个恶意服务器
s := newTcpTestServer(func(conn net.Conn, data []byte) error {
_, err := conn.Write([]byte("HTTP/1.1 200 OK\r\nContent-Length: 10\r\nConnection: close\r\n\r\na"))
return err
})
defer s.Close()
cli = &Client{
Server: s.URL(),
APIVersion: "v1",
httpClient: &http.Client{},
logger: logrus.New(),
}
err = cli.Fetch("/", nil, &a)
assert.Contains(t, err.Error(), "unexpected EOF")
// 构造content length 的atoi 异常, 需要构造一个恶意服务器
s1 := newTcpTestServer(func(conn net.Conn, data []byte) error {
_, err := conn.Write([]byte("HTTP/1.1 200 OK\r\nContent-Length: -\r\nConnection: close\r\n\r\na"))
return err
})
defer s1.Close()
cli = &Client{
Server: s1.URL(),
APIVersion: "v1",
httpClient: &http.Client{},
logger: logrus.New(),
}
err = cli.Fetch("/", nil, &a)
assert.Contains(t, err.Error(), "bad Content-Length")
// 构造错误的gzip, 需要构造一个恶意服务器
s2 := newTcpTestServer(func(conn net.Conn, data []byte) error {
_, err := conn.Write([]byte("HTTP/1.1 200 OK\r\nContent-Length: 1\r\nContent-Encoding: gzip\r\nConnection: close\r\n\r\na"))
return err
})
defer s2.Close()
cli = &Client{
Server: s2.URL(),
APIVersion: "v1",
httpClient: &http.Client{},
logger: logrus.New(),
}
err = cli.Fetch("/", nil, &a)
assert.Contains(t, err.Error(), "unexpected EOF")
}