-
Notifications
You must be signed in to change notification settings - Fork 6
/
endpoint_test.go
59 lines (54 loc) · 1.7 KB
/
endpoint_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
package slimhttp
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestJSONEndpointWrapper(t *testing.T) {
wr := httptest.NewRecorder()
r := NewBaseRouter()
handler := r.endpointWrapper(func(r *http.Request) (interface{}, error) {
return newTestStruct(), nil
}, encodeJSON)
handler(wr, nil)
body, status, err := result(wr)
equal(t, err, nil)
equal(t, status, 200)
equal(t, body, `{"string_key":"string-val","int_key":5,"float_key":13.37}`+"\n")
}
func TestXMLEndpointWrapper(t *testing.T) {
wr := httptest.NewRecorder()
r := NewBaseRouter()
handler := r.endpointWrapper(func(r *http.Request) (interface{}, error) {
return newTestStruct(), nil
}, encodeXML)
handler(wr, nil)
body, status, err := result(wr)
equal(t, err, nil)
equal(t, status, 200)
equal(t, body, `<?xml version="1.0" encoding="UTF-8"?>`+"\n"+`<testStruct><stringKey>string-val</stringKey><intKey>5</intKey><floatKey>13.37</floatKey></testStruct>`)
}
func TestTextEndpointWrapper(t *testing.T) {
wr := httptest.NewRecorder()
r := NewBaseRouter()
handler := r.endpointWrapper(func(r *http.Request) (interface{}, error) {
return "Here's some text!", nil
}, encodeText)
handler(wr, nil)
body, status, err := result(wr)
equal(t, err, nil)
equal(t, status, 200)
equal(t, body, "Here's some text!")
}
func TestHTMLEndpointWrapper(t *testing.T) {
wr := httptest.NewRecorder()
r := NewBaseRouter()
handler := r.endpointWrapper(func(r *http.Request) (interface{}, error) {
return newTestWebpage(), nil
}, encodeHTML)
handler(wr, nil)
body, status, err := result(wr)
equal(t, err, nil)
equal(t, status, 200)
equal(t, body, "<string-test>string-val</string-test><br/><int-test>5</int-test><br/><float-test>13.37</float-test>")
}