forked from manyminds/api2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_entity_path_test.go
151 lines (132 loc) · 3.59 KB
/
api_entity_path_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
package api2go
import (
"net/http"
"net/http/httptest"
"strings"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type SconeTaste struct {
ID string `json:"-"`
Taste string `json:"taste"`
}
func (s SconeTaste) GetID() string {
return s.ID
}
func (s *SconeTaste) SetID(ID string) error {
s.ID = ID
return nil
}
func (s SconeTaste) GetName() string {
return "Scone-tastes"
}
func (s SconeTaste) GetPath() string {
//Leading and trailing slashes are not required. Including in test to make sure we don't end up with double slashes
return "/food/sconeTastes/"
}
type SconeResource struct{}
func (s SconeResource) FindOne(ID string, req Request) (Responder, error) {
return &Response{Res: SconeTaste{ID: "blubb", Taste: "Very Bad"}}, nil
}
func (s SconeResource) FindAll(req Request) (Responder, error) {
return &Response{Res: []SconeTaste{
{
ID: "1",
Taste: "Very Good",
},
{
ID: "2",
Taste: "Very Bad",
},
}}, nil
}
func (s SconeResource) Create(obj interface{}, req Request) (Responder, error) {
e := obj.(SconeTaste)
e.ID = "newID"
return &Response{
Res: e,
Code: http.StatusCreated,
}, nil
}
func (s SconeResource) Delete(ID string, req Request) (Responder, error) {
return &Response{
Res: SconeTaste{ID: ID},
Code: http.StatusNoContent,
}, nil
}
func (s SconeResource) Update(obj interface{}, req Request) (Responder, error) {
return &Response{
Res: obj,
Code: http.StatusNoContent,
}, nil
}
var _ = Describe("Test route renaming with EntityPather interface", func() {
var (
api *API
rec *httptest.ResponseRecorder
body *strings.Reader
)
BeforeEach(func() {
api = NewAPIWithRouting(testPrefix, NewStaticResolver(""), newTestRouter())
api.AddResource(SconeTaste{}, SconeResource{})
rec = httptest.NewRecorder()
body = strings.NewReader(`
{
"data": {
"attributes": {
"taste": "smells awful"
},
"id": "blubb",
"type": "Scone-tastes"
}
}
`)
})
// check that renaming works, we do not test every single route here, the name variable is used
// for each route, we just check the 5 basic ones. Marshalling and Unmarshalling is tested with
// this again too.
It("FindAll returns 200", func() {
req, err := http.NewRequest("GET", "/v1/food/sconeTastes", nil)
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusOK))
})
It("FindOne", func() {
req, err := http.NewRequest("GET", "/v1/food/sconeTastes/12345", nil)
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusOK))
})
It("Delete", func() {
req, err := http.NewRequest("DELETE", "/v1/food/sconeTastes/12345", nil)
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusNoContent))
})
It("Create", func() {
req, err := http.NewRequest("POST", "/v1/food/sconeTastes", body)
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
// the response is always the one record returned by FindOne, the implementation does not
// check the ID here and returns something new ...
Expect(rec.Body.String()).To(MatchJSON(`
{
"data": {
"attributes": {
"taste": "smells awful"
},
"id": "newID",
"type": "Scone-tastes"
}
}
`))
Expect(rec.Code).To(Equal(http.StatusCreated))
})
It("Update", func() {
req, err := http.NewRequest("PATCH", "/v1/food/sconeTastes/blubb", body)
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Body.String()).To(Equal(""))
Expect(rec.Code).To(Equal(http.StatusNoContent))
})
})