-
Notifications
You must be signed in to change notification settings - Fork 3
/
util_test.go
65 lines (52 loc) · 1.89 KB
/
util_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
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func Test_util_writeResponse(t *testing.T) {
u := util{}
rw := httptest.NewRecorder()
expCode := http.StatusGone
expBody := fmt.Sprintf(`{"code":%d,"message":"gone!!"}`, expCode)
u.writeResponse(rw, expCode, expBody)
assert.Equal(t, rw.Code, expCode, "badRequest() returned unexpected status code")
assert.Equal(t, rw.Body.String(), expBody, "badRequest() returned unexpected body")
}
// Tests that the util.badRequest() function behaves properly.
func Test_util_badRequest(t *testing.T) {
u := util{}
enc := JSONEncoder{}
rw := httptest.NewRecorder()
msg := "baaaaad request"
expCode := http.StatusBadRequest
expBody := fmt.Sprintf(`{"code":%d,"message":"%s"}`, expCode, msg)
u.badRequest(rw, enc, msg)
assert.Equal(t, rw.Code, expCode, "badRequest() returned unexpected status code")
assert.Equal(t, rw.Body.String(), expBody, "badRequest() returned unexpected body")
}
// Tests that the util.notFound() function behaves properly.
func Test_util_notFound(t *testing.T) {
u := util{}
enc := JSONEncoder{}
rw := httptest.NewRecorder()
msg := "missing!"
expCode := http.StatusNotFound
expBody := fmt.Sprintf(`{"code":%d,"message":"%s"}`, expCode, msg)
u.notFound(rw, enc, msg)
assert.Equal(t, rw.Code, expCode, "notFound() returned unexpected status code")
assert.Equal(t, rw.Body.String(), expBody, "notFound() returned unexpected body")
}
// Tests that the util.conflict() function behaves properly.
func Test_util_conflict(t *testing.T) {
u := util{}
enc := JSONEncoder{}
rw := httptest.NewRecorder()
msg := "already exists!"
expCode := http.StatusConflict
expBody := fmt.Sprintf(`{"code":%d,"message":"%s"}`, expCode, msg)
u.conflict(rw, enc, msg)
assert.Equal(t, rw.Code, expCode, "conflict() returned unexpected status code")
assert.Equal(t, rw.Body.String(), expBody, "conflict() returned unexpected body")
}