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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
|
package api2go
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"github.com/manyminds/api2go/jsonapi"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type SomeData struct {
ID string `json:"-"`
Data string `json:"data"`
CustomerID string `json:"customerId"`
}
func (s SomeData) GetID() string {
return s.ID
}
func (s *SomeData) SetID(ID string) error {
s.ID = ID
return nil
}
type SomeResource struct{}
func (s SomeResource) FindOne(ID string, req Request) (Responder, error) {
return &Response{Res: SomeData{ID: "12345", Data: "A Brezzn"}}, nil
}
func (s SomeResource) Create(obj interface{}, req Request) (Responder, error) {
incoming := obj.(SomeData)
switch incoming.ID {
case "":
return &Response{Res: SomeData{ID: "12345", Data: "A Brezzn"}, Code: http.StatusCreated}, nil
case "accept":
return &Response{Res: SomeData{ID: "someID"}, Code: http.StatusAccepted}, nil
case "forbidden":
return &Response{}, NewHTTPError(nil, "Forbidden", http.StatusForbidden)
case "conflict":
return &Response{}, NewHTTPError(nil, "Conflict", http.StatusConflict)
case "invalid":
return &Response{Res: SomeData{}, Code: http.StatusTeapot}, nil
default:
return &Response{Res: SomeData{ID: incoming.ID}, Code: http.StatusNoContent}, nil
}
}
func (s SomeResource) Delete(ID string, req Request) (Responder, error) {
switch ID {
case "200":
return &Response{Code: http.StatusOK, Meta: map[string]interface{}{"some": "cool stuff"}}, nil
case "202":
return &Response{Code: http.StatusAccepted}, nil
default:
return &Response{Code: http.StatusNoContent}, nil
}
}
func (s SomeResource) Update(obj interface{}, req Request) (Responder, error) {
incoming := obj.(SomeData)
switch incoming.Data {
case "override me":
return &Response{Code: http.StatusOK}, nil
case "delayed":
return &Response{Code: http.StatusAccepted}, nil
case "new value":
return &Response{Code: http.StatusNoContent}, nil
case "fail":
return &Response{}, NewHTTPError(nil, "Fail", http.StatusForbidden)
case "invalid":
return &Response{Code: http.StatusTeapot}, nil
default:
return &Response{Code: http.StatusNoContent}, nil
}
}
type ResourceReadOnly struct{}
func (r ResourceReadOnly) FindOne(ID string, req Request) (Responder, error) {
return &Response{Res: SomeData{ID: "12345", Data: "A Brezzn"}, Code: http.StatusOK}, nil
}
type ResourceCreationOnly struct{}
func (r ResourceCreationOnly) Create(obj interface{}, req Request) (Responder, error) {
return &Response{Res: SomeData{ID: "12345", Data: "A Brezzn"}, Code: http.StatusCreated}, nil
}
type ResourceUpdateOnly struct{}
func (r ResourceUpdateOnly) FindOne(ID string, req Request) (Responder, error) {
return &Response{Res: SomeData{ID: "12345", Data: "A Brezzn"}, Code: http.StatusOK}, nil
}
func (r ResourceUpdateOnly) Update(obj interface{}, req Request) (Responder, error) {
return &Response{Res: SomeData{ID: "12345", Data: "A Brezzn"}, Code: http.StatusOK}, nil
}
type ResourceDeletionOnly struct{}
func (r ResourceDeletionOnly) Delete(id string, req Request) (Responder, error) {
return &Response{Code: http.StatusNoContent}, nil
}
var _ = Describe("Test interface api type casting", func() {
var (
api *API
rec *httptest.ResponseRecorder
)
BeforeEach(func() {
api = NewAPI("v1")
api.AddResource(SomeData{}, SomeResource{})
rec = httptest.NewRecorder()
})
It("FindAll returns 404 for simple CRUD", func() {
req, err := http.NewRequest("GET", "/v1/someDatas", nil)
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusNotFound))
})
It("Works for a normal FindOne", func() {
req, err := http.NewRequest("GET", "/v1/someDatas/12345", nil)
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusOK))
})
It("Post works with lowercase renaming", func() {
reqBody := strings.NewReader(`{"data": {"attributes":{"customerId": "2" }, "type": "someDatas"}}`)
req, err := http.NewRequest("POST", "/v1/someDatas", reqBody)
Expect(err).To(BeNil())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusCreated))
})
})
var _ = Describe("Test return code behavior", func() {
var (
api *API
rec *httptest.ResponseRecorder
payload, payloadID SomeData
)
BeforeEach(func() {
api = NewAPI("v1")
api.AddResource(SomeData{}, SomeResource{})
rec = httptest.NewRecorder()
payloadID = SomeData{ID: "12345", Data: "A Brezzn"}
payload = SomeData{Data: "A Brezzn"}
})
Context("Create", func() {
post := func(payload SomeData) {
m, err := jsonapi.Marshal(payload)
Expect(err).ToNot(HaveOccurred())
req, err := http.NewRequest("POST", "/v1/someDatas", strings.NewReader(string(m)))
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
}
It("returns object with 201 created", func() {
post(payload)
Expect(rec.Code).To(Equal(http.StatusCreated))
var actual SomeData
err := jsonapi.Unmarshal(rec.Body.Bytes(), &actual)
Expect(err).ToNot(HaveOccurred())
Expect(payloadID).To(Equal(actual))
})
It("return no content 204 with client side generated id", func() {
post(payloadID)
Expect(rec.Code).To(Equal(http.StatusNoContent))
Expect(rec.Body.String()).To(BeEmpty())
})
It("return accepted and no content", func() {
post(SomeData{ID: "accept", Data: "nothing"})
Expect(rec.Code).To(Equal(http.StatusAccepted))
Expect(rec.Body.String()).To(BeEmpty())
})
It("does not accept invalid return codes", func() {
post(SomeData{ID: "invalid"})
Expect(rec.Code).To(Equal(http.StatusInternalServerError))
var err HTTPError
json.Unmarshal(rec.Body.Bytes(), &err)
Expect(err.Errors[0]).To(Equal(Error{
Title: "invalid status code 418 from resource someDatas for method Create",
Status: strconv.Itoa(http.StatusInternalServerError)}))
})
It("handles forbidden 403 error", func() {
post(SomeData{ID: "forbidden", Data: "i am so forbidden"})
Expect(rec.Code).To(Equal(http.StatusForbidden))
var err HTTPError
json.Unmarshal(rec.Body.Bytes(), &err)
Expect(err.Errors[0]).To(Equal(Error{Title: "Forbidden", Status: strconv.Itoa(http.StatusForbidden)}))
})
It("handles 409 conflict error", func() {
post(SomeData{ID: "conflict", Data: "no force push here"})
Expect(rec.Code).To(Equal(http.StatusConflict))
var err HTTPError
json.Unmarshal(rec.Body.Bytes(), &err)
Expect(err.Errors[0]).To(Equal(Error{Title: "Conflict", Status: strconv.Itoa(http.StatusConflict)}))
})
})
Context("Update", func() {
patch := func(payload SomeData) {
m, err := jsonapi.Marshal(payload)
Expect(err).ToNot(HaveOccurred())
req, err := http.NewRequest("PATCH", "/v1/someDatas/12345", strings.NewReader(string(m)))
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
}
It("returns 200 ok if the server modified a field", func() {
patch(SomeData{ID: "12345", Data: "override me"})
Expect(rec.Code).To(Equal(http.StatusOK))
var actual SomeData
err := jsonapi.Unmarshal(rec.Body.Bytes(), &actual)
Expect(err).ToNot(HaveOccurred())
Expect(payloadID).To(Equal(actual))
})
It("returns 202 Accepted if update is delayed", func() {
patch(SomeData{ID: "12345", Data: "delayed"})
Expect(rec.Code).To(Equal(http.StatusAccepted))
Expect(rec.Body.String()).To(BeEmpty())
})
It("returns 204 No Content if update was accepted", func() {
patch(SomeData{ID: "12345", Data: "new value"})
Expect(rec.Code).To(Equal(http.StatusNoContent))
Expect(rec.Body.String()).To(BeEmpty())
})
It("does not accept invalid return codes", func() {
patch(SomeData{ID: "12345", Data: "invalid"})
Expect(rec.Code).To(Equal(http.StatusInternalServerError))
var err HTTPError
json.Unmarshal(rec.Body.Bytes(), &err)
Expect(err.Errors[0]).To(Equal(Error{
Title: "invalid status code 418 from resource someDatas for method Update",
Status: strconv.Itoa(http.StatusInternalServerError)}))
})
// We do not check everything again like in Create, because it's always the same handleError
// method that get's called.
It("handles error cases", func() {
patch(SomeData{ID: "12345", Data: "fail"})
Expect(rec.Code).To(Equal(http.StatusForbidden), "we do not allow failes here!")
var err HTTPError
json.Unmarshal(rec.Body.Bytes(), &err)
Expect(err.Errors[0]).To(Equal(Error{Title: "Fail", Status: strconv.Itoa(http.StatusForbidden)}))
})
})
Context("Delete", func() {
delete := func(ID string) {
req, err := http.NewRequest("DELETE", "/v1/someDatas/"+ID, nil)
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
}
It("returns 200 ok if there is some meta data", func() {
delete("200")
Expect(rec.Code).To(Equal(http.StatusOK))
var response map[string]interface{}
json.Unmarshal(rec.Body.Bytes(), &response)
Expect(response).To(Equal(map[string]interface{}{
"meta": map[string]interface{}{
"some": "cool stuff",
},
}))
})
It("returns 202 accepted if deletion is delayed", func() {
delete("202")
Expect(rec.Code).To(Equal(http.StatusAccepted))
Expect(rec.Body.String()).To(BeEmpty())
})
It("return 204 No Content if deletion just worked", func() {
delete("204")
Expect(rec.Code).To(Equal(http.StatusNoContent))
Expect(rec.Body.String()).To(BeEmpty())
})
})
})
var _ = Describe("Test partial CRUD implementation : Creator", func() {
var (
api *API
rec *httptest.ResponseRecorder
payload, payloadID SomeData
)
BeforeEach(func() {
api = NewAPI("v1")
api.AddResource(SomeData{}, ResourceCreationOnly{})
rec = httptest.NewRecorder()
payloadID = SomeData{ID: "12345", Data: "A Brezzn"}
payload = SomeData{Data: "A Brezzn"}
})
Context("Create", func() {
post := func(payload SomeData) {
m, err := jsonapi.Marshal(payload)
Expect(err).ToNot(HaveOccurred())
req, err := http.NewRequest("POST", "/v1/someDatas", strings.NewReader(string(m)))
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
}
It("returns 201", func() {
post(payload)
Expect(rec.Code).To(Equal(http.StatusCreated))
var actual SomeData
err := jsonapi.Unmarshal(rec.Body.Bytes(), &actual)
Expect(err).ToNot(HaveOccurred())
Expect(payloadID).To(Equal(actual))
})
It("returns 404 on every other routes", func() {
// Test PATCH
m, err := jsonapi.Marshal(payload)
Expect(err).ToNot(HaveOccurred())
reqPatch, errPatch := http.NewRequest("PATCH", "/v1/someDatas/12345", strings.NewReader(string(m)))
Expect(errPatch).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, reqPatch)
Expect(rec.Code).To(Equal(http.StatusNotFound))
// Test DELETE
reqDelete, errDelete := http.NewRequest("DELETE", "/v1/someDatas/12345", nil)
Expect(errDelete).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, reqDelete)
Expect(rec.Code).To(Equal(http.StatusNotFound))
// Test GET item
reqRead, errRead := http.NewRequest("GET", "/v1/someDatas/12345", nil)
Expect(errRead).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, reqRead)
Expect(rec.Code).To(Equal(http.StatusNotFound))
})
})
})
var _ = Describe("Test partial CRUD implementation : Updater", func() {
var (
api *API
rec *httptest.ResponseRecorder
payload, payloadID SomeData
)
BeforeEach(func() {
api = NewAPI("v1")
api.AddResource(SomeData{}, ResourceUpdateOnly{})
rec = httptest.NewRecorder()
payloadID = SomeData{ID: "12345", Data: "A Brezzn"}
payload = SomeData{Data: "A Brezzn"}
})
Context("Update", func() {
patch := func(payload SomeData) {
m, err := jsonapi.Marshal(payload)
Expect(err).ToNot(HaveOccurred())
req, err := http.NewRequest("PATCH", "/v1/someDatas/12345", strings.NewReader(string(m)))
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
}
It("returns 200", func() {
patch(SomeData{ID: "12345", Data: "override me"})
Expect(rec.Code).To(Equal(http.StatusOK))
var actual SomeData
err := jsonapi.Unmarshal(rec.Body.Bytes(), &actual)
Expect(err).ToNot(HaveOccurred())
Expect(payloadID).To(Equal(actual))
})
It("returns 404 on every other routes", func() {
// Test POST
m, err := jsonapi.Marshal(payload)
Expect(err).ToNot(HaveOccurred())
reqPost, errPost := http.NewRequest("POST", "/v1/someDatas", strings.NewReader(string(m)))
Expect(errPost).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, reqPost)
Expect(rec.Code).To(Equal(http.StatusMethodNotAllowed))
// Test DELETE
reqDelete, errDelete := http.NewRequest("DELETE", "/v1/someDatas/12345", nil)
Expect(errDelete).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, reqDelete)
Expect(rec.Code).To(Equal(http.StatusMethodNotAllowed))
// Test GET item
reqRead, errRead := http.NewRequest("GET", "/v1/someDatas/12345", nil)
Expect(errRead).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, reqRead)
Expect(rec.Code).To(Equal(http.StatusMethodNotAllowed))
})
})
})
var _ = Describe("Test partial CRUD implementation : Deleter", func() {
var (
api *API
rec *httptest.ResponseRecorder
payload, payloadID SomeData
)
BeforeEach(func() {
api = NewAPI("v1")
api.AddResource(SomeData{}, ResourceDeletionOnly{})
rec = httptest.NewRecorder()
payloadID = SomeData{ID: "12345", Data: "A Brezzn"}
payload = SomeData{Data: "A Brezzn"}
})
Context("Delete", func() {
delete := func() {
req, err := http.NewRequest("DELETE", "/v1/someDatas/1234", nil)
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
}
It("returns 204", func() {
delete()
Expect(rec.Code).To(Equal(http.StatusNoContent))
})
It("returns 404 on every other routes", func() {
// Test POST
m, err := jsonapi.Marshal(payload)
Expect(err).ToNot(HaveOccurred())
reqPost, errPost := http.NewRequest("POST", "/v1/someDatas", strings.NewReader(string(m)))
Expect(errPost).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, reqPost)
Expect(rec.Code).To(Equal(http.StatusMethodNotAllowed))
// Test PATCH
reqPatch, errPatch := http.NewRequest("PATCH", "/v1/someDatas/12345", strings.NewReader(string(m)))
Expect(errPatch).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, reqPatch)
Expect(rec.Code).To(Equal(http.StatusMethodNotAllowed))
// Test GET item
reqRead, errRead := http.NewRequest("GET", "/v1/someDatas/12345", nil)
Expect(errRead).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, reqRead)
Expect(rec.Code).To(Equal(http.StatusMethodNotAllowed))
})
})
})
|