-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors_test.go
118 lines (109 loc) · 2.74 KB
/
errors_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
108
109
110
111
112
113
114
115
116
117
118
package jsonapi_test
import (
"bytes"
"encoding/json"
"fmt"
"io"
"reflect"
"testing"
"github.com/elasticpath/jsonapi"
)
func TestErrorObjectWritesExpectedErrorMessage(t *testing.T) {
input := &jsonapi.ErrorObject{Title: "Title test.", Detail: "Detail test."}
output := input.Error()
if output != fmt.Sprintf("Error: %s %s\n", input.Title, input.Detail) {
t.Fatal("Unexpected output.")
}
}
func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) {
var marshalErrorsTableTasts = map[string]struct {
In []*jsonapi.ErrorObject
Out map[string]interface{}
}{
"TestFieldsAreSerializedAsNeeded": {
In: []*jsonapi.ErrorObject{{
ID: "0",
Title: "Test title.",
Detail: "Test detail",
Status: "400",
Code: "E1100",
}},
Out: map[string]interface{}{
"errors": []interface{}{map[string]interface{}{
"id": "0",
"title": "Test title.",
"detail": "Test detail",
"status": "400",
"code": "E1100",
}},
},
},
"TestMetaFieldIsSerializedProperly": {
In: []*jsonapi.ErrorObject{{
Title: "Test title.",
Detail: "Test detail",
Meta: &map[string]interface{}{
"key": "val",
},
}},
Out: map[string]interface{}{
"errors": []interface{}{map[string]interface{}{
"title": "Test title.",
"detail": "Test detail",
"meta": map[string]interface{}{
"key": "val",
}},
},
},
},
"TestSourceFieldIsSerializedProperly": {
In: []*jsonapi.ErrorObject{{
Title: "Test title.",
Detail: "Test detail",
Source: &jsonapi.ErrorSource{
Pointer: "/data/attributes/field",
Parameter: "filter",
},
}},
Out: map[string]interface{}{
"errors": []interface{}{map[string]interface{}{
"title": "Test title.",
"detail": "Test detail",
"source": map[string]interface{}{
"parameter": "filter",
"pointer": "/data/attributes/field",
},
},
}},
},
"TestLinksFieldIsSerializedProperly": {
In: []*jsonapi.ErrorObject{{
Title: "Test title.",
Detail: "Test detail",
Links: &jsonapi.ErrorLink{
About: "/URL/to/details",
},
}},
Out: map[string]interface{}{
"errors": []interface{}{map[string]interface{}{
"title": "Test title.",
"detail": "Test detail",
"links": map[string]interface{}{
"about": "/URL/to/details",
},
}},
},
},
}
for name, test := range marshalErrorsTableTasts {
t.Run(name, func(t *testing.T) {
buffer, output := bytes.NewBuffer(nil), map[string]interface{}{}
var writer io.Writer = buffer
_ = jsonapi.MarshalErrors(writer, test.In)
json.Unmarshal(buffer.Bytes(), &output)
if !reflect.DeepEqual(output, test.Out) {
t.Fatalf("Expected: \n%#v \nto equal: \n%#v", output, test.Out)
}
})
}
}