This repository has been archived by the owner on May 4, 2018. It is now read-only.
/
render_test.go
75 lines (60 loc) · 2.2 KB
/
render_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
package zest
import (
"errors"
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
type stubController struct {
r *Render
status int
obj interface{}
}
type stubErrController struct {
r *Render
status int
apiErr *APIError
err error
}
func (c *stubController) Handler(w http.ResponseWriter, r *http.Request) {
c.r.JSON(w, c.status, c.obj)
}
func (c *stubErrController) Handler(w http.ResponseWriter, r *http.Request) {
c.r.JSONError(w, c.status, c.apiErr, c.err)
}
// TestJSON runs tests on the render JSON method.
func TestJSON(t *testing.T) {
a := assert.New(t)
// r := require.New(t)
render := NewRender()
str := "foobar"
c := &stubController{r: render, status: http.StatusOK, obj: str}
a.HTTPBodyContains(c.Handler, "GET", "/", url.Values{}, "foobar")
c.obj = &str
a.HTTPBodyContains(c.Handler, "GET", "/", url.Values{}, "foobar")
c.obj = nil
a.HTTPBodyNotContains(c.Handler, "GET", "/", url.Values{}, "foobar")
}
// TestJSONError runs tests on the render JSONError method.
func TestJSONError(t *testing.T) {
a := assert.New(t)
// r := require.New(t)
render := NewRender()
apiErr := &APIError{Description: "An error occured.", ErrorCode: "ERROR"}
c := &stubErrController{r: render, status: http.StatusInternalServerError, apiErr: apiErr, err: errors.New("undefined error")}
a.HTTPBodyContains(c.Handler, "GET", "/", url.Values{}, "500")
a.HTTPBodyContains(c.Handler, "GET", "/", url.Values{}, "An error occured.")
a.HTTPBodyContains(c.Handler, "GET", "/", url.Values{}, "undefined error")
a.HTTPBodyContains(c.Handler, "GET", "/", url.Values{}, "ERROR")
c.apiErr = nil
a.HTTPBodyContains(c.Handler, "GET", "/", url.Values{}, "500")
a.HTTPBodyNotContains(c.Handler, "GET", "/", url.Values{}, "An error occured.")
a.HTTPBodyContains(c.Handler, "GET", "/", url.Values{}, "undefined error")
a.HTTPBodyNotContains(c.Handler, "GET", "/", url.Values{}, "ERROR")
c.err = nil
a.HTTPBodyContains(c.Handler, "GET", "/", url.Values{}, "500")
a.HTTPBodyNotContains(c.Handler, "GET", "/", url.Values{}, "An error occured.")
a.HTTPBodyNotContains(c.Handler, "GET", "/", url.Values{}, "undefined error")
a.HTTPBodyNotContains(c.Handler, "GET", "/", url.Values{}, "ERROR")
}