|
| 1 | +package api2go |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "strings" |
| 7 | + |
| 8 | + . "github.com/onsi/ginkgo" |
| 9 | + . "github.com/onsi/gomega" |
| 10 | +) |
| 11 | + |
| 12 | +type ObjectInitializerResource struct{} |
| 13 | + |
| 14 | +func (s ObjectInitializerResource) InitializeObject(obj interface{}) { |
| 15 | + if post, ok := obj.(*Post); ok { |
| 16 | + post.Title = "New Title" |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +func (s ObjectInitializerResource) FindOne(ID string, req Request) (Responder, error) { |
| 21 | + return nil, nil |
| 22 | +} |
| 23 | + |
| 24 | +func (s ObjectInitializerResource) Create(obj interface{}, req Request) (Responder, error) { |
| 25 | + return &Response{Res: obj, Code: http.StatusCreated}, nil |
| 26 | +} |
| 27 | + |
| 28 | +func (s ObjectInitializerResource) Delete(ID string, req Request) (Responder, error) { |
| 29 | + return nil, nil |
| 30 | +} |
| 31 | + |
| 32 | +func (s ObjectInitializerResource) Update(obj interface{}, req Request) (Responder, error) { |
| 33 | + return nil, nil |
| 34 | +} |
| 35 | + |
| 36 | +var _ = Describe("Test resource implementing the ObjectInitializer interface", func() { |
| 37 | + var ( |
| 38 | + api *API |
| 39 | + rec *httptest.ResponseRecorder |
| 40 | + body *strings.Reader |
| 41 | + ) |
| 42 | + BeforeEach(func() { |
| 43 | + api = NewAPI("v1") |
| 44 | + api.AddResource(Post{}, ObjectInitializerResource{}) |
| 45 | + rec = httptest.NewRecorder() |
| 46 | + body = strings.NewReader(` |
| 47 | + { |
| 48 | + "data": { |
| 49 | + "attributes": {}, |
| 50 | + "id": "blubb", |
| 51 | + "type": "posts" |
| 52 | + } |
| 53 | + } |
| 54 | + `) |
| 55 | + }) |
| 56 | + |
| 57 | + It("Create", func() { |
| 58 | + req, err := http.NewRequest("POST", "/v1/posts", body) |
| 59 | + Expect(err).ToNot(HaveOccurred()) |
| 60 | + api.Handler().ServeHTTP(rec, req) |
| 61 | + |
| 62 | + Expect(rec.Body.String()).To(MatchJSON(` |
| 63 | + { |
| 64 | + "data": { |
| 65 | + "type": "posts", |
| 66 | + "id": "blubb", |
| 67 | + "attributes": { |
| 68 | + "title": "New Title", |
| 69 | + "value": null |
| 70 | + }, |
| 71 | + "relationships": { |
| 72 | + "author": { |
| 73 | + "links": { |
| 74 | + "self": "/v1/posts/blubb/relationships/author", |
| 75 | + "related": "/v1/posts/blubb/author" |
| 76 | + }, |
| 77 | + "data": null |
| 78 | + }, |
| 79 | + "bananas": { |
| 80 | + "links": { |
| 81 | + "self": "/v1/posts/blubb/relationships/bananas", |
| 82 | + "related": "/v1/posts/blubb/bananas" |
| 83 | + }, |
| 84 | + "data": [] |
| 85 | + }, |
| 86 | + "comments": { |
| 87 | + "links": { |
| 88 | + "self": "/v1/posts/blubb/relationships/comments", |
| 89 | + "related": "/v1/posts/blubb/comments" |
| 90 | + }, |
| 91 | + "data": [] |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + `)) |
| 97 | + Expect(rec.Code).To(Equal(http.StatusCreated)) |
| 98 | + }) |
| 99 | +}) |
0 commit comments