-
Notifications
You must be signed in to change notification settings - Fork 9
/
body_test.go
241 lines (203 loc) · 5.7 KB
/
body_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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package luddite
import (
"bytes"
"encoding/xml"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
)
type sample struct {
XMLName xml.Name `json:"-" xml:"sample"`
Id int `json:"id" xml:"id"`
Name string `json:"name" xml:"name"`
Flag bool `json:"flag" xml:"flag"`
Data []byte `json:"data" xml:"data"`
Timestamp time.Time `json:"timestamp" xml:"timestamp"`
}
const (
sampleId = 1234
sampleName = "dave"
sampleData = "Hello world"
sampleJsonBody = "{\"id\":1234,\"name\":\"dave\",\"flag\":true,\"data\":\"SGVsbG8gd29ybGQ=\",\"timestamp\":\"2015-03-18T14:30:00Z\"}"
sampleXmlBody = "<sample><id>1234</id><name>dave</name><flag>true</flag><data>Hello world</data><timestamp>2015-03-18T14:30:00Z</timestamp></sample>"
sampleUrlencodedBody = "id=1234&name=dave&flag=true×tamp=2015-03-18T14:30:00Z"
)
var (
sampleTimestamp = time.Date(2015, 3, 18, 14, 30, 0, 0, time.UTC)
)
type sampleResource struct {
NotImplementedResource
}
func (r *sampleResource) New() interface{} {
return &sample{}
}
func (r *sampleResource) Id(value interface{}) string {
return strconv.Itoa(value.(*sample).Id)
}
func TestReadJson(t *testing.T) {
req, _ := http.NewRequest("GET", "/", strings.NewReader(sampleJsonBody))
req.Header[HeaderContentType] = []string{ContentTypeJson + "; charset=UTF-8"}
v := &sample{}
if err := ReadRequest(req, v); err != nil {
t.Fatal(err)
}
if v.Id != sampleId {
t.Error("JSON int deserialization failed")
}
if v.Name != sampleName {
t.Error("JSON string deserialization failed")
}
if !v.Flag {
t.Error("JSON bool deserialization failed")
}
if !bytes.Equal(v.Data, []byte(sampleData)) {
t.Error("JSON binary deserialization failed")
}
if v.Timestamp != sampleTimestamp {
t.Error("JSON date deserialization failed")
}
}
func TestWriteJson(t *testing.T) {
s := &sample{
Id: sampleId,
Name: sampleName,
Flag: true,
Data: []byte(sampleData),
Timestamp: sampleTimestamp,
}
rw := httptest.NewRecorder()
rw.Header().Add(HeaderContentType, ContentTypeJson)
if err := WriteResponse(rw, http.StatusOK, s); err != nil {
t.Fatal(err)
}
if rw.Code != http.StatusOK {
t.Error("status code never written")
}
if rw.Body != nil {
if body := string(rw.Body.String()); body != sampleJsonBody {
t.Errorf("JSON serialization failed, got: %s, expected: %s\n", body, sampleJsonBody)
}
} else {
t.Error("body never written")
}
}
func TestReadXml(t *testing.T) {
req, _ := http.NewRequest("GET", "/", strings.NewReader(sampleXmlBody))
req.Header[HeaderContentType] = []string{ContentTypeXml + "; charset=UTF-8"}
v := &sample{}
if err := ReadRequest(req, v); err != nil {
t.Fatal(err)
}
if v.Id != sampleId {
t.Error("XML int deserialization failed")
}
if v.Name != sampleName {
t.Error("XML string deserialization failed")
}
if !v.Flag {
t.Error("XML bool deserialization failed")
}
if !bytes.Equal(v.Data, []byte(sampleData)) {
t.Error("XML binary deserialization failed")
}
if v.Timestamp != sampleTimestamp {
t.Error("XML date deserialization failed")
}
}
func TestWriteXml(t *testing.T) {
s := &sample{
Id: sampleId,
Name: sampleName,
Flag: true,
Data: []byte(sampleData),
Timestamp: sampleTimestamp,
}
rw := httptest.NewRecorder()
rw.Header().Add(HeaderContentType, ContentTypeXml)
if err := WriteResponse(rw, http.StatusOK, s); err != nil {
t.Fatal(err)
}
if rw.Code != http.StatusOK {
t.Error("status code never written")
}
if rw.Body != nil {
if body := string(rw.Body.String()); body != sampleXmlBody {
t.Errorf("XML serialization failed, got: %s, expected: %s\n", body, sampleXmlBody)
}
} else {
t.Error("body never written")
}
}
func TestWriteHtml(t *testing.T) {
// Write []byte
rw := httptest.NewRecorder()
rw.Header().Add(HeaderContentType, ContentTypeHtml)
if err := WriteResponse(rw, http.StatusOK, []byte(sampleData)); err != nil {
t.Fatal(err)
}
if rw.Code != http.StatusOK {
t.Error("status code never written")
}
if rw.Body != nil {
if body := string(rw.Body.String()); body != sampleData {
t.Errorf("HTML body write failed, got: %s, expected: %s\n", body, sampleData)
}
} else {
t.Error("body never written")
}
// Write string
rw = httptest.NewRecorder()
rw.Header().Add(HeaderContentType, ContentTypeHtml)
if err := WriteResponse(rw, http.StatusOK, sampleData); err != nil {
t.Fatal(err)
}
if rw.Code != http.StatusOK {
t.Error("status code never written")
}
if rw.Body != nil {
if body := string(rw.Body.String()); body != sampleData {
t.Errorf("HTML body write failed, got: %s, expected: %s\n", body, sampleData)
}
} else {
t.Error("body never written")
}
// Write other type
s := &sample{
Id: sampleId,
Name: sampleName,
Flag: true,
Data: []byte(sampleData),
Timestamp: sampleTimestamp,
}
rw = httptest.NewRecorder()
rw.Header().Add(HeaderContentType, ContentTypeHtml)
if err := WriteResponse(rw, http.StatusOK, s); err != nil {
t.Fatal(err)
}
if rw.Code != http.StatusOK {
t.Error("status code never written")
}
}
func TestReadUrlencoded(t *testing.T) {
req, _ := http.NewRequest("POST", "/", strings.NewReader(sampleUrlencodedBody))
req.Header[HeaderContentType] = []string{ContentTypeWwwFormUrlencoded}
v := &sample{}
if err := ReadRequest(req, v); err != nil {
t.Fatal(err)
}
if v.Id != sampleId {
t.Error("Urlencoded int deserialization failed")
}
if v.Name != sampleName {
t.Error("Urlencoded string deserialization failed")
}
if !v.Flag {
t.Error("Urlencoded bool deserialization failed")
}
if v.Timestamp != sampleTimestamp {
t.Error("Urlencoded date deserialization failed")
}
}