-
Notifications
You must be signed in to change notification settings - Fork 12
/
error_test.go
89 lines (68 loc) · 2.19 KB
/
error_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
package jsh
import (
"net/http"
"net/http/httptest"
"strconv"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestError(t *testing.T) {
Convey("Error Tests", t, func() {
request := &http.Request{}
writer := httptest.NewRecorder()
testErrorObject := &Error{
Status: http.StatusBadRequest,
Title: "Fail",
Detail: "So badly",
}
Convey("->Validate()", func() {
Convey("should not fail for a valid Error", func() {
err := testErrorObject.Validate(request, true)
So(err, ShouldBeNil)
})
Convey("422 Status Formatting", func() {
testErrorObject.Status = 422
Convey("should accept a properly formatted 422 error", func() {
testErrorObject.Source.Pointer = "/data/attributes/test"
err := testErrorObject.Validate(request, true)
So(err, ShouldBeNil)
})
Convey("should error if Source.Pointer isn't set", func() {
err := testErrorObject.Validate(request, true)
So(err, ShouldNotBeNil)
})
})
Convey("should fail for an out of range HTTP error status", func() {
testErrorObject.Status = http.StatusOK
err := testErrorObject.Validate(request, true)
So(err, ShouldNotBeNil)
})
})
Convey("->Send()", func() {
testError := &Error{
Status: http.StatusForbidden,
Title: "Forbidden",
Detail: "Can't Go Here",
}
Convey("should send a properly formatted JSON error", func() {
err := Send(writer, request, testError)
So(err, ShouldBeNil)
So(writer.Code, ShouldEqual, http.StatusForbidden)
contentLength, convErr := strconv.Atoi(writer.HeaderMap.Get("Content-Length"))
So(convErr, ShouldBeNil)
So(contentLength, ShouldBeGreaterThan, 0)
So(writer.HeaderMap.Get("Content-Type"), ShouldEqual, ContentType)
})
Convey("should work for an ErrorList", func() {
errorList := ErrorList{testError}
err := Send(writer, request, errorList)
So(err, ShouldBeNil)
So(writer.Code, ShouldEqual, http.StatusForbidden)
contentLength, convErr := strconv.Atoi(writer.HeaderMap.Get("Content-Length"))
So(convErr, ShouldBeNil)
So(contentLength, ShouldBeGreaterThan, 0)
So(writer.HeaderMap.Get("Content-Type"), ShouldEqual, ContentType)
})
})
})
}