|
| 1 | +// +build echo,!gingonic,!gorillamux |
| 2 | + |
| 3 | +package routing_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "io/ioutil" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/labstack/echo" |
| 13 | + "github.com/manyminds/api2go" |
| 14 | + "github.com/manyminds/api2go/examples/model" |
| 15 | + "github.com/manyminds/api2go/examples/resource" |
| 16 | + "github.com/manyminds/api2go/examples/storage" |
| 17 | + "github.com/manyminds/api2go/routing" |
| 18 | + |
| 19 | + . "github.com/onsi/ginkgo" |
| 20 | + . "github.com/onsi/gomega" |
| 21 | +) |
| 22 | + |
| 23 | +var _ = Describe("api2go with echo router adapter", func() { |
| 24 | + var ( |
| 25 | + router routing.Routeable |
| 26 | + e *echo.Echo |
| 27 | + api *api2go.API |
| 28 | + rec *httptest.ResponseRecorder |
| 29 | + ) |
| 30 | + |
| 31 | + BeforeSuite(func() { |
| 32 | + e = echo.New() |
| 33 | + router = routing.Echo(e) |
| 34 | + api = api2go.NewAPIWithRouting( |
| 35 | + "api", |
| 36 | + api2go.NewStaticResolver("/"), |
| 37 | + router, |
| 38 | + ) |
| 39 | + |
| 40 | + userStorage := storage.NewUserStorage() |
| 41 | + chocStorage := storage.NewChocolateStorage() |
| 42 | + api.AddResource(model.User{}, resource.UserResource{ChocStorage: chocStorage, UserStorage: userStorage}) |
| 43 | + api.AddResource(model.Chocolate{}, resource.ChocolateResource{ChocStorage: chocStorage, UserStorage: userStorage}) |
| 44 | + }) |
| 45 | + |
| 46 | + BeforeEach(func() { |
| 47 | + log.SetOutput(ioutil.Discard) |
| 48 | + rec = httptest.NewRecorder() |
| 49 | + }) |
| 50 | + |
| 51 | + Context("CRUD Tests", func() { |
| 52 | + It("will create a new user", func() { |
| 53 | + reqBody := strings.NewReader(`{"data": {"attributes": {"user-name": "Sansa Stark"}, "id": "1", "type": "users"}}`) |
| 54 | + req, err := http.NewRequest("POST", "/api/users", reqBody) |
| 55 | + Expect(err).To(BeNil()) |
| 56 | + e.ServeHTTP(rec, req) |
| 57 | + Expect(rec.Code).To(Equal(http.StatusCreated)) |
| 58 | + }) |
| 59 | + |
| 60 | + It("will find her", func() { |
| 61 | + expectedUser := ` |
| 62 | + { |
| 63 | + "data": |
| 64 | + { |
| 65 | + "attributes":{ |
| 66 | + "user-name":"Sansa Stark" |
| 67 | + }, |
| 68 | + "id":"1", |
| 69 | + "relationships":{ |
| 70 | + "sweets":{ |
| 71 | + "data":[],"links":{"related":"/api/users/1/sweets","self":"/api/users/1/relationships/sweets"} |
| 72 | + } |
| 73 | + },"type":"users" |
| 74 | + }, |
| 75 | + "meta": |
| 76 | + { |
| 77 | + "author":"The api2go examples crew","license":"wtfpl","license-url":"http://www.wtfpl.net" |
| 78 | + } |
| 79 | + }` |
| 80 | + |
| 81 | + req, err := http.NewRequest("GET", "/api/users/1", nil) |
| 82 | + Expect(err).To(BeNil()) |
| 83 | + e.ServeHTTP(rec, req) |
| 84 | + Expect(rec.Code).To(Equal(http.StatusOK)) |
| 85 | + Expect(string(rec.Body.Bytes())).To(MatchJSON((expectedUser))) |
| 86 | + }) |
| 87 | + |
| 88 | + It("can call handle", func() { |
| 89 | + handler := api.Handler() |
| 90 | + _, ok := handler.(http.Handler) |
| 91 | + Expect(ok).To(Equal(true)) |
| 92 | + }) |
| 93 | + |
| 94 | + It("update the username", func() { |
| 95 | + reqBody := strings.NewReader(`{"data": {"id": "1", "attributes": {"user-name": "Alayne"}, "type" : "users"}}`) |
| 96 | + req, err := http.NewRequest("PATCH", "/api/users/1", reqBody) |
| 97 | + Expect(err).To(BeNil()) |
| 98 | + e.ServeHTTP(rec, req) |
| 99 | + Expect(rec.Code).To(Equal(http.StatusNoContent)) |
| 100 | + }) |
| 101 | + |
| 102 | + It("will find her once again", func() { |
| 103 | + expectedUser := ` |
| 104 | + { |
| 105 | + "data": |
| 106 | + { |
| 107 | + "attributes":{ |
| 108 | + "user-name":"Alayne" |
| 109 | + }, |
| 110 | + "id":"1", |
| 111 | + "relationships":{ |
| 112 | + "sweets":{ |
| 113 | + "data":[],"links":{"related":"/api/users/1/sweets","self":"/api/users/1/relationships/sweets"} |
| 114 | + } |
| 115 | + },"type":"users" |
| 116 | + }, |
| 117 | + "meta": |
| 118 | + { |
| 119 | + "author":"The api2go examples crew","license":"wtfpl","license-url":"http://www.wtfpl.net" |
| 120 | + } |
| 121 | + }` |
| 122 | + |
| 123 | + req, err := http.NewRequest("GET", "/api/users/1", nil) |
| 124 | + Expect(err).To(BeNil()) |
| 125 | + e.ServeHTTP(rec, req) |
| 126 | + Expect(rec.Code).To(Equal(http.StatusOK)) |
| 127 | + Expect(string(rec.Body.Bytes())).To(MatchJSON((expectedUser))) |
| 128 | + }) |
| 129 | + |
| 130 | + It("will delete her", func() { |
| 131 | + req, err := http.NewRequest("DELETE", "/api/users/1", nil) |
| 132 | + Expect(err).To(BeNil()) |
| 133 | + e.ServeHTTP(rec, req) |
| 134 | + Expect(rec.Code).To(Equal(http.StatusNoContent)) |
| 135 | + }) |
| 136 | + |
| 137 | + It("won't find her anymore", func() { |
| 138 | + expected := `{"errors":[{"status":"404","title":"http error (404) User for id 1 not found and 0 more errors, User for id 1 not found"}]}` |
| 139 | + req, err := http.NewRequest("GET", "/api/users/1", nil) |
| 140 | + Expect(err).To(BeNil()) |
| 141 | + e.ServeHTTP(rec, req) |
| 142 | + Expect(rec.Code).To(Equal(http.StatusNotFound)) |
| 143 | + Expect(string(rec.Body.Bytes())).To(MatchJSON(expected)) |
| 144 | + }) |
| 145 | + }) |
| 146 | +}) |
0 commit comments