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
|
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
}
}
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())
})
})
})
|