This repository has been archived by the owner on Dec 29, 2023. It is now read-only.
forked from derekdowling/go-json-spec-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_test.go
155 lines (121 loc) · 4.05 KB
/
object_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package jsh
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestObject(t *testing.T) {
Convey("Object Tests", t, func() {
testObject := &Object{
ID: "ID123",
Type: "testObject",
Attributes: json.RawMessage(`{"foo":"bar"}`),
}
request := &http.Request{}
Convey("->NewObject()", func() {
Convey("should create a new object with populated attrs", func() {
attrs := struct {
Foo string `json:"foo"`
}{"bar"}
newObj, err := NewObject(testObject.ID, testObject.Type, attrs)
So(err, ShouldBeNil)
So(newObj.Attributes, ShouldNotBeEmpty)
})
})
Convey("->Unmarshal()", func() {
Convey("non-govalidator structs", func() {
testConversion := struct {
ID string
Foo string `json:"foo"`
}{}
Convey("Should successfully populate a valid struct", func() {
err := testObject.Unmarshal("testObject", &testConversion)
So(err, ShouldBeNil)
So(testConversion.Foo, ShouldEqual, "bar")
})
Convey("Should reject a non-matching type", func() {
err := testObject.Unmarshal("badType", &testConversion)
So(err, ShouldNotBeNil)
})
})
Convey("govalidator struct unmarshals", func() {
Convey("should not error if input validates properly", func() {
testValidation := struct {
Foo string `json:"foo" valid:"alphanum"`
}{}
err := testObject.Unmarshal("testObject", &testValidation)
So(err, ShouldBeNil)
So(testValidation.Foo, ShouldEqual, "bar")
})
Convey("should return a 422 Error correctly for a validation failure", func() {
testValidation := struct {
Foo string `valid:"ipv4,required" json:"foo"`
}{}
err := testObject.Unmarshal("testObject", &testValidation)
So(err, ShouldNotBeNil)
So(err[0].Source.Pointer, ShouldEqual, "/data/attributes/foo")
})
Convey("should return a 422 Error correctly for multiple validation failures", func() {
testManyObject := &Object{
ID: "ID123",
Type: "testObject",
Attributes: json.RawMessage(`{"foo":"bar", "baz":"4567"}`),
}
testManyValidations := struct {
Foo string `valid:"ipv4,required" json:"foo"`
Baz string `valid:"alpha,required" json:"baz"`
}{}
err := testManyObject.Unmarshal("testObject", &testManyValidations)
So(err, ShouldNotBeNil)
So(err[0].Source.Pointer, ShouldEqual, "/data/attributes/foo")
So(err[1].Source.Pointer, ShouldEqual, "/data/attributes/baz")
})
})
})
Convey("->Marshal()", func() {
Convey("should properly update attributes", func() {
attrs := map[string]string{"foo": "baz"}
err := testObject.Marshal(attrs)
So(err, ShouldBeNil)
raw, jsonErr := json.MarshalIndent(attrs, "", " ")
So(jsonErr, ShouldBeNil)
So(string(testObject.Attributes), ShouldEqual, string(raw))
})
})
Convey("->JSON()", func() {
Convey("should handle a POST response correctly", func() {
request.Method = "POST"
err := testObject.Validate(request, true)
So(err, ShouldBeNil)
So(testObject.Status, ShouldEqual, http.StatusCreated)
})
Convey("should handle a GET response correctly", func() {
request.Method = "GET"
err := testObject.Validate(request, true)
So(err, ShouldBeNil)
So(testObject.Status, ShouldEqual, http.StatusOK)
})
Convey("should handle a PATCH response correctly", func() {
request.Method = "PATCH"
err := testObject.Validate(request, true)
So(err, ShouldBeNil)
So(testObject.Status, ShouldEqual, http.StatusOK)
})
Convey("should return a formatted Error for an unsupported method Type", func() {
request.Method = "PUT"
err := testObject.Validate(request, true)
So(err, ShouldNotBeNil)
So(err.Status, ShouldEqual, http.StatusNotAcceptable)
})
})
Convey("->Send(Object)", func() {
request.Method = "POST"
writer := httptest.NewRecorder()
err := Send(writer, request, testObject)
So(err, ShouldBeNil)
So(writer.Code, ShouldEqual, http.StatusCreated)
})
})
}