-
Notifications
You must be signed in to change notification settings - Fork 0
/
resp_test.go
130 lines (122 loc) · 2.85 KB
/
resp_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
package req
import (
"encoding/json"
"encoding/xml"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestToJSON(t *testing.T) {
type Result struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
r1 := Result{
Code: 1,
Msg: "ok",
}
handler := func(w http.ResponseWriter, r *http.Request) {
data, _ := json.Marshal(&r1)
w.Write(data)
}
ts := httptest.NewServer(http.HandlerFunc(handler))
r, err := Get(ts.URL)
if err != nil {
t.Fatal(err)
}
var r2 Result
err = r.ToJSON(&r2)
if err != nil {
t.Fatal(err)
}
if r1 != r2 {
t.Errorf("json response body = %+v; want = %+v", r2, r1)
}
}
func TestToXML(t *testing.T) {
type Result struct {
XMLName xml.Name
Code int `xml:"code"`
Msg string `xml:"msg"`
}
r1 := Result{
XMLName: xml.Name{Local: "result"},
Code: 1,
Msg: "ok",
}
handler := func(w http.ResponseWriter, r *http.Request) {
data, _ := xml.Marshal(&r1)
w.Write(data)
}
ts := httptest.NewServer(http.HandlerFunc(handler))
r, err := Get(ts.URL)
if err != nil {
t.Fatal(err)
}
var r2 Result
err = r.ToXML(&r2)
if err != nil {
t.Fatal(err)
}
if r1 != r2 {
t.Errorf("xml response body = %+v; want = %+v", r2, r1)
}
}
func TestFormat(t *testing.T) {
SetFlags(LstdFlags | Lcost)
reqHeader := "Request-Header"
respHeader := "Response-Header"
reqBody := "request body"
respBody1 := "response body 1"
respBody2 := "response body 2"
respBody := fmt.Sprintf("%s\n%s", respBody1, respBody2)
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(respHeader, "req")
w.Write([]byte(respBody))
}
ts := httptest.NewServer(http.HandlerFunc(handler))
// %v
r, err := Post(ts.URL, reqBody, Header{reqHeader: "hello"})
if err != nil {
t.Fatal(err)
}
str := fmt.Sprintf("%v", r)
for _, keyword := range []string{ts.URL, reqBody, respBody} {
if !strings.Contains(str, keyword) {
t.Errorf("format %%v output lack of part, want: %s", keyword)
}
}
// %-v
str = fmt.Sprintf("%-v", r)
for _, keyword := range []string{ts.URL, respBody1 + " " + respBody2} {
if !strings.Contains(str, keyword) {
t.Errorf("format %%-v output lack of part, want: %s", keyword)
}
}
// %+v
str = fmt.Sprintf("%+v", r)
for _, keyword := range []string{reqBody, respBody, reqHeader, respHeader} {
if !strings.Contains(str, keyword) {
t.Errorf("format %%+v output lack of part, want: %s", keyword)
}
}
}
func TestBytesAndString(t *testing.T) {
respBody := "response body"
handler := func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(respBody))
}
ts := httptest.NewServer(http.HandlerFunc(handler))
r, err := Get(ts.URL)
if err != nil {
t.Fatal(err)
}
if string(r.Bytes()) != respBody {
t.Errorf("response body = %s; want = %s", r.Bytes(), respBody)
}
if r.String() != respBody {
t.Errorf("response body = %s; want = %s", r.String(), respBody)
}
}