forked from manyminds/api2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_object_initializer_test.go
99 lines (88 loc) · 2.22 KB
/
api_object_initializer_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 (
"net/http"
"net/http/httptest"
"strings"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type ObjectInitializerResource struct{}
func (s ObjectInitializerResource) InitializeObject(obj interface{}) {
if post, ok := obj.(*Post); ok {
post.Title = "New Title"
}
}
func (s ObjectInitializerResource) FindOne(ID string, req Request) (Responder, error) {
return nil, nil
}
func (s ObjectInitializerResource) Create(obj interface{}, req Request) (Responder, error) {
return &Response{Res: obj, Code: http.StatusCreated}, nil
}
func (s ObjectInitializerResource) Delete(ID string, req Request) (Responder, error) {
return nil, nil
}
func (s ObjectInitializerResource) Update(obj interface{}, req Request) (Responder, error) {
return nil, nil
}
var _ = Describe("Test resource implementing the ObjectInitializer interface", func() {
var (
api *API
rec *httptest.ResponseRecorder
body *strings.Reader
)
BeforeEach(func() {
api = NewAPIWithRouting(testPrefix, NewStaticResolver(""), newTestRouter())
api.AddResource(Post{}, ObjectInitializerResource{})
rec = httptest.NewRecorder()
body = strings.NewReader(`
{
"data": {
"attributes": {},
"id": "blubb",
"type": "posts"
}
}
`)
})
It("Create", func() {
req, err := http.NewRequest("POST", "/v1/posts", body)
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Body.String()).To(MatchJSON(`
{
"data": {
"type": "posts",
"id": "blubb",
"attributes": {
"title": "New Title",
"value": null
},
"relationships": {
"author": {
"links": {
"self": "/v1/posts/blubb/relationships/author",
"related": "/v1/posts/blubb/author"
},
"data": null
},
"bananas": {
"links": {
"self": "/v1/posts/blubb/relationships/bananas",
"related": "/v1/posts/blubb/bananas"
},
"data": []
},
"comments": {
"links": {
"self": "/v1/posts/blubb/relationships/comments",
"related": "/v1/posts/blubb/comments"
},
"data": []
}
}
}
}
`))
Expect(rec.Code).To(Equal(http.StatusCreated))
})
})