forked from imroc/req
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump_test.go
62 lines (59 loc) · 1.39 KB
/
dump_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
package req
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestDumpText(t *testing.T) {
SetFlags(LstdFlags | Lcost)
reqBody := "request body"
respBody := "response body"
reqHeader := "Request-Header"
respHeader := "Response-Header"
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(respHeader, "req")
w.Write([]byte(respBody))
}
ts := httptest.NewServer(http.HandlerFunc(handler))
header := Header{
reqHeader: "hello",
}
resp, err := Post(ts.URL, header, reqBody)
if err != nil {
t.Fatal(err)
}
dump := resp.Dump()
for _, keyword := range []string{reqBody, respBody, reqHeader, respHeader} {
if !strings.Contains(dump, keyword) {
t.Errorf("dump missing part, want: %s", keyword)
}
}
}
func TestDumpUpload(t *testing.T) {
SetFlags(LreqBody)
file1 := ioutil.NopCloser(strings.NewReader("file1"))
uploads := []FileUpload{
{
FileName: "1.txt",
FieldName: "media",
File: file1,
},
}
ts := newDefaultTestServer()
r, err := Post(ts.URL, uploads, Param{"hello": "req"})
if err != nil {
t.Fatal(err)
}
dump := r.Dump()
contains := []string{
`Content-Disposition: form-data; name="hello"`,
`Content-Disposition: form-data; name="media"; filename="1.txt"`,
}
for _, contain := range contains {
if !strings.Contains(dump, contain) {
t.Errorf("multipart dump should contains: %s", contain)
}
}
}