-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
170 lines (145 loc) · 3.32 KB
/
http_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
170
package httpclient
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
)
var timeout = 5
func readAllString(r io.Reader) (string, error) {
var buf bytes.Buffer
if _, err := buf.ReadFrom(r); err != nil {
return "", err
}
return buf.String(), nil
}
var copyHandlerFunc = http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
result := ""
defer r.Body.Close()
switch r.Method {
case "GET":
result = r.RequestURI
case "POST":
r.ParseMultipartForm(32 << 20)
if r.MultipartForm != nil {
values := r.MultipartForm.Value["test"]
if len(values) > 0 {
fmt.Fprintf(w, values[0])
return
}
}
result = r.PostFormValue("test")
case "PUT":
temp, _ := ioutil.ReadAll(r.Body)
result = string(temp)
case "DELETE":
temp, _ := ioutil.ReadAll(r.Body)
result = string(temp)
default:
}
fmt.Fprintf(w, result)
// io.Copy(w, r.Body)
},
)
func TestGet(t *testing.T) {
ts := httptest.NewServer(copyHandlerFunc)
defer ts.Close()
body, code, err := Get(ts.URL+"/TestGet", nil, timeout)
if err != nil {
t.Fatal(err)
}
if code != 200 || body != "/TestGet" {
t.Fatal("get response error")
}
}
func TestPost(t *testing.T) {
ts := httptest.NewServer(copyHandlerFunc)
defer ts.Close()
param := make(map[string]interface{})
param["test"] = "TestPost"
body, code, err := Post(ts.URL, param, nil, timeout, false)
if err != nil {
t.Fatal(err)
}
if code != 200 || body != "TestPost" {
t.Fatal("post response error")
}
param["test"] = "TestMultipart"
body, code, err = Post(ts.URL, param, nil, timeout, true)
if err != nil {
t.Fatal(err)
}
if code != 200 || body != "TestMultipart" {
t.Fatal("multipart response error")
}
}
func TestGetPost(t *testing.T) {
ts := httptest.NewServer(copyHandlerFunc)
defer ts.Close()
param := make(map[string]interface{})
param["test"] = "TestPost"
body, code, err := GetPost(ts.URL, param, param, timeout)
if err != nil {
t.Fatal(err)
}
if code != 200 || body != "TestPost" {
t.Fatal("post response error")
}
param["test"] = "TestMultipart"
body, code, err = Post(ts.URL, param, nil, timeout, true)
if err != nil {
t.Fatal(err)
}
if code != 200 || body != "TestMultipart" {
t.Fatal("multipart response error")
}
}
func TestPut(t *testing.T) {
ts := httptest.NewServer(copyHandlerFunc)
defer ts.Close()
body, code, err := Put(ts.URL, "TestPut", timeout)
if err != nil {
t.Fatal(err)
}
if code != 200 || body != "TestPut" {
t.Fatal("put response error")
}
}
func TestDelete(t *testing.T) {
ts := httptest.NewServer(copyHandlerFunc)
defer ts.Close()
body, code, err := Delete(ts.URL, "TestDelete", timeout)
if err != nil {
t.Fatal(err)
}
if code != 200 || body != "TestDelete" {
t.Fatal("delete response error")
}
}
func TestTimeout(t *testing.T) {
ts := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Duration(timeout) * time.Millisecond)
// w.WriteHeader(500)
fmt.Fprintf(w, "success")
}),
)
defer ts.Close()
//超时时间返回
body, code, err := Get(ts.URL+"/TestGet", nil, (timeout + 10))
if err != nil {
t.Fatal(err)
}
if code != 200 || body != "success" {
t.Fatal("get response error")
}
body, code, err = Get(ts.URL+"/TestGet", nil, (timeout - 1))
if err == nil {
t.Fatal(err)
}
}