forked from manyminds/api2go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
error_test.go
99 lines (82 loc) · 2.88 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
90
91
92
93
94
95
96
97
98
99
package api2go
import (
"errors"
"net/http"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type ErrorMarshaler struct{}
func (e ErrorMarshaler) Marshal(i interface{}) ([]byte, error) {
return []byte{}, errors.New("this will always fail")
}
func (e ErrorMarshaler) Unmarshal(data []byte, i interface{}) error {
return nil
}
func (e ErrorMarshaler) MarshalError(error) string {
return ""
}
var _ = Describe("Errors test", func() {
Context("validate error logic", func() {
It("can create array tree", func() {
httpErr := NewHTTPError(errors.New("hi"), "hi", 0)
for i := 0; i < 20; i++ {
httpErr.Errors = append(httpErr.Errors, Error{})
}
Expect(len(httpErr.Errors)).To(Equal(20))
})
})
Context("Marshalling", func() {
It("will be marshalled correctly with default error", func() {
httpErr := NewHTTPError(nil, "Invalid use case done", http.StatusInternalServerError)
result := marshalHTTPError(httpErr)
expected := `{"errors":[{"status":"500","title":"Invalid use case done"}]}`
Expect(result).To(Equal(expected))
})
It("will be marshalled correctly without child errors", func() {
httpErr := NewHTTPError(errors.New("Bad Request"), "Bad Request", 400)
result := marshalHTTPError(httpErr)
expected := `{"errors":[{"status":"400","title":"Bad Request"}]}`
Expect(result).To(Equal(expected))
})
It("will be marshalled correctly with child errors", func() {
httpErr := NewHTTPError(errors.New("Bad Request"), "Bad Request", 500)
errorOne := Error{
ID: "001",
Links: &ErrorLinks{
About: "http://bla/blub",
},
Status: "500",
Code: "001",
Title: "Title must not be empty",
Detail: "Never occures in real life",
Source: &ErrorSource{
Pointer: "#titleField",
},
Meta: map[string]interface{}{
"creator": "api2go",
},
}
httpErr.Errors = append(httpErr.Errors, errorOne)
result := marshalHTTPError(httpErr)
expected := `{"errors":[{"id":"001","links":{"about":"http://bla/blub"},"status":"500","code":"001","title":"Title must not be empty","detail":"Never occures in real life","source":{"pointer":"#titleField"},"meta":{"creator":"api2go"}}]}`
Expect(result).To(Equal(expected))
})
It("will be marshalled correctly with child errors without links or source", func() {
httpErr := NewHTTPError(errors.New("Bad Request"), "Bad Request", 500)
errorOne := Error{
ID: "001",
Status: "500",
Code: "001",
Title: "Title must not be empty",
Detail: "Never occures in real life",
Meta: map[string]interface{}{
"creator": "api2go",
},
}
httpErr.Errors = append(httpErr.Errors, errorOne)
result := marshalHTTPError(httpErr)
expected := `{"errors":[{"id":"001","status":"500","code":"001","title":"Title must not be empty","detail":"Never occures in real life","meta":{"creator":"api2go"}}]}`
Expect(result).To(Equal(expected))
})
})
})