/
shttp_test.go
107 lines (97 loc) · 2.85 KB
/
shttp_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
package shttp_test
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/apriendeau/shttp"
"github.com/stretchr/testify/assert"
)
type Sample struct {
Hello string `json:"hello" xml:"hello"`
}
func TestWriteJSON(t *testing.T) {
assert := assert.New(t)
var b = struct {
Hello string `json:"hello"`
}{"world"}
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "/test", nil)
assert.NoError(err)
// test content-negotiation application/json
r.Header.Set("Accept", "application/json")
err = shttp.Write(w, r, b, 200)
assert.NoError(err)
assert.Equal(w.Code, 200)
assert.Equal(w.Body.String(), `{"hello":"world"}`)
}
func TestWriteXML(t *testing.T) {
assert := assert.New(t)
b := Sample{
Hello: "world",
}
// test content-negotiation application/xml
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "/test", nil)
r.Header.Set("Accept", "application/xml")
err = shttp.Write(w, r, b, 200)
assert.NoError(err)
assert.Equal(w.Code, 200)
assert.Equal(w.Body.String(), "<Sample><hello>world</hello></Sample>")
}
func TestReadJSON(t *testing.T) {
assert := assert.New(t)
body := strings.NewReader(`{"hello":"world"}`)
req, err := http.NewRequest("POST", "/test", body)
assert.NoError(err)
req.Header.Set("Content-Type", "application/json")
var b Sample
err = shttp.Read(req, &b)
assert.Nil(err)
assert.Equal(b.Hello, "world")
}
func TestReadXML(t *testing.T) {
assert := assert.New(t)
body := strings.NewReader(`<Sample><hello>world</hello></Sample>`)
req, err := http.NewRequest("POST", "/test", body)
assert.NoError(err)
req.Header.Set("Content-Type", "application/xml")
var b Sample
err = shttp.Read(req, &b)
assert.Nil(err)
assert.Equal(b.Hello, "world")
}
func TestErrorJSON(t *testing.T) {
assert := assert.New(t)
e := errors.New("test")
w := httptest.NewRecorder()
req, err := http.NewRequest("POST", "/test", nil)
assert.NoError(err)
err = shttp.Error(w, req, e, 200)
assert.NoError(err)
assert.Equal(w.Code, 200, "should be expected status code")
assert.Contains(w.Body.String(), `"description":"test"`, "should be formatted as JSON error")
}
func TestErrorXML(t *testing.T) {
assert := assert.New(t)
e := errors.New("test")
w := httptest.NewRecorder()
req, err := http.NewRequest("POST", "/test", nil)
assert.NoError(err)
req.Header.Set("Accept", "application/xml")
err = shttp.Error(w, req, e, 503)
assert.NoError(err)
assert.Equal(w.Code, 503, "should be expected status code")
assert.Contains(w.Body.String(), "<title>Service Unavailable</title>")
}
func TestStatus(t *testing.T) {
assert := assert.New(t)
w := httptest.NewRecorder()
req, err := http.NewRequest("POST", "/test", nil)
assert.NoError(err)
err = shttp.Status(w, req, 204)
assert.NoError(err)
assert.Equal(w.Code, 204, "should be expected status code")
assert.Equal(w.Body.String(), "", "should be an empty response")
}