Skip to content

Commit

Permalink
test api with prefix for #73
Browse files Browse the repository at this point in the history
seems to work all fine
  • Loading branch information
wwwdata committed Mar 17, 2015
1 parent 68ee3ed commit ee93c36
Showing 1 changed file with 27 additions and 41 deletions.
68 changes: 27 additions & 41 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,12 @@ var _ = Describe("RestHandler", func() {
"author": map[string]interface{}{
"id": "1",
"type": "users",
"resource": "/posts/1/author",
"resource": "/v1/posts/1/author",
},
"comments": map[string]interface{}{
"ids": []interface{}{"1"},
"type": "comments",
"resource": "/posts/1/comments",
"resource": "/v1/posts/1/comments",
},
},
}
Expand All @@ -295,12 +295,12 @@ var _ = Describe("RestHandler", func() {
"links": map[string]interface{}{
"author": map[string]interface{}{
"type": "users",
"resource": "/posts/2/author",
"resource": "/v1/posts/2/author",
},
"comments": map[string]interface{}{
"ids": []interface{}{},
"type": "comments",
"resource": "/posts/2/comments",
"resource": "/v1/posts/2/comments",
},
},
}
Expand All @@ -313,17 +313,17 @@ var _ = Describe("RestHandler", func() {
"links": map[string]interface{}{
"author": map[string]interface{}{
"type": "users",
"resource": "/posts/3/author",
"resource": "/v1/posts/3/author",
},
"comments": map[string]interface{}{
"ids": []interface{}{},
"type": "comments",
"resource": "/posts/3/comments",
"resource": "/v1/posts/3/comments",
},
},
}

api = NewAPI("")
api = NewAPI("v1")
api.AddResource(Post{}, source)
api.AddResource(User{}, &userSource{})
api.AddResource(Comment{}, &commentSource{})
Expand All @@ -332,7 +332,7 @@ var _ = Describe("RestHandler", func() {
})

It("GETs collections", func() {
req, err := http.NewRequest("GET", "/posts", nil)
req, err := http.NewRequest("GET", "/v1/posts", nil)
Expect(err).To(BeNil())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusOK))
Expand All @@ -345,7 +345,7 @@ var _ = Describe("RestHandler", func() {
})

It("GETs single objects", func() {
req, err := http.NewRequest("GET", "/posts/1", nil)
req, err := http.NewRequest("GET", "/v1/posts/1", nil)
Expect(err).To(BeNil())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusOK))
Expand All @@ -358,7 +358,7 @@ var _ = Describe("RestHandler", func() {
})

It("GETs multiple objects", func() {
req, err := http.NewRequest("GET", "/posts/1,2", nil)
req, err := http.NewRequest("GET", "/v1/posts/1,2", nil)
Expect(err).To(BeNil())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusOK))
Expand All @@ -371,31 +371,31 @@ var _ = Describe("RestHandler", func() {
})

It("GETs related struct from resource url", func() {
req, err := http.NewRequest("GET", "/posts/1/author", nil)
req, err := http.NewRequest("GET", "/v1/posts/1/author", nil)
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusOK))
Expect(rec.Body.Bytes()).To(MatchJSON(`{"data": {"id": "1", "name": "Dieter", "type": "users"}}`))
})

It("GETs related structs from resource url", func() {
req, err := http.NewRequest("GET", "/posts/1/comments", nil)
req, err := http.NewRequest("GET", "/v1/posts/1/comments", nil)
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusOK))
Expect(rec.Body.Bytes()).To(MatchJSON(`{"data": [{"id": "1", "value": "This is a stupid post!", "type": "comments"}]}`))
})

It("Gets 404 if a related struct was not found", func() {
req, err := http.NewRequest("GET", "/posts/1/unicorns", nil)
req, err := http.NewRequest("GET", "/v1/posts/1/unicorns", nil)
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusNotFound))
Expect(rec.Body.Bytes()).ToNot(BeEmpty())
})

It("404s", func() {
req, err := http.NewRequest("GET", "/posts/23", nil)
req, err := http.NewRequest("GET", "/v1/posts/23", nil)
Expect(err).To(BeNil())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusNotFound))
Expand All @@ -405,11 +405,11 @@ var _ = Describe("RestHandler", func() {

It("POSTSs new objects", func() {
reqBody := strings.NewReader(`{"posts": [{"title": "New Post"}]}`)
req, err := http.NewRequest("POST", "/posts", reqBody)
req, err := http.NewRequest("POST", "/v1/posts", reqBody)
Expect(err).To(BeNil())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusCreated))
Expect(rec.Header().Get("Location")).To(Equal("/posts/4"))
Expect(rec.Header().Get("Location")).To(Equal("/v1/posts/4"))
var result map[string]interface{}
Expect(json.Unmarshal(rec.Body.Bytes(), &result)).To(BeNil())
Expect(result).To(Equal(map[string]interface{}{
Expand All @@ -421,12 +421,12 @@ var _ = Describe("RestHandler", func() {
"links": map[string]interface{}{
"author": map[string]interface{}{
"type": "users",
"resource": "/posts/4/author",
"resource": "/v1/posts/4/author",
},
"comments": map[string]interface{}{
"ids": []interface{}{},
"type": "comments",
"resource": "/posts/4/comments",
"resource": "/v1/posts/4/comments",
},
},
},
Expand All @@ -435,7 +435,7 @@ var _ = Describe("RestHandler", func() {

It("POSTSs new objects with trailing slash automatic redirect enabled", func() {
reqBody := strings.NewReader(`{"posts": [{"title": "New Post"}]}`)
req, err := http.NewRequest("POST", "/posts/", reqBody)
req, err := http.NewRequest("POST", "/v1/posts/", reqBody)
Expect(err).To(BeNil())
api.SetRedirectTrailingSlash(true)
api.Handler().ServeHTTP(rec, req)
Expand All @@ -444,7 +444,7 @@ var _ = Describe("RestHandler", func() {

It("POSTSs new objects with trailing slash automatic redirect disabled", func() {
reqBody := strings.NewReader(`{"posts": [{"title": "New Post"}]}`)
req, err := http.NewRequest("POST", "/posts/", reqBody)
req, err := http.NewRequest("POST", "/v1/posts/", reqBody)
Expect(err).To(BeNil())
api.SetRedirectTrailingSlash(false)
api.Handler().ServeHTTP(rec, req)
Expand All @@ -453,7 +453,7 @@ var _ = Describe("RestHandler", func() {

It("POSTSs multiple objects", func() {
reqBody := strings.NewReader(`{"posts": [{"title": "New Post"}, {"title" : "Second New Post"}]}`)
req, err := http.NewRequest("POST", "/posts", reqBody)
req, err := http.NewRequest("POST", "/v1/posts", reqBody)
Expect(err).To(BeNil())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusInternalServerError))
Expand All @@ -463,7 +463,7 @@ var _ = Describe("RestHandler", func() {

It("PUTSs multiple objects", func() {
reqBody := strings.NewReader(`{"posts": [{"title": "New Post"}, {"title" : "Second New Post"}]}`)
req, err := http.NewRequest("PUT", "/posts/1", reqBody)
req, err := http.NewRequest("PUT", "/v1/posts/1", reqBody)
Expect(err).To(BeNil())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusInternalServerError))
Expand All @@ -472,23 +472,23 @@ var _ = Describe("RestHandler", func() {
})

It("OPTIONS on collection route", func() {
req, err := http.NewRequest("OPTIONS", "/posts", nil)
req, err := http.NewRequest("OPTIONS", "/v1/posts", nil)
api.Handler().ServeHTTP(rec, req)
Expect(err).To(BeNil())
Expect(rec.Code).To(Equal(http.StatusNoContent))
Expect(rec.Header().Get("Allow")).To(Equal("GET,POST,OPTIONS"))
})

It("OPTIONS on element route", func() {
req, err := http.NewRequest("OPTIONS", "/posts/1", nil)
req, err := http.NewRequest("OPTIONS", "/v1/posts/1", nil)
api.Handler().ServeHTTP(rec, req)
Expect(err).To(BeNil())
Expect(rec.Code).To(Equal(http.StatusNoContent))
Expect(rec.Header().Get("Allow")).To(Equal("GET,PUT,DELETE,OPTIONS"))
})

It("DELETEs", func() {
req, err := http.NewRequest("DELETE", "/posts/1", nil)
req, err := http.NewRequest("DELETE", "/v1/posts/1", nil)
Expect(err).To(BeNil())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusNoContent))
Expand All @@ -497,7 +497,7 @@ var _ = Describe("RestHandler", func() {

It("UPDATEs", func() {
reqBody := strings.NewReader(`{"posts": {"id": "1", "title": "New Title"}}`)
req, err := http.NewRequest("PUT", "/posts/1", reqBody)
req, err := http.NewRequest("PUT", "/v1/posts/1", reqBody)
Expect(err).To(BeNil())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusNoContent))
Expand All @@ -506,7 +506,7 @@ var _ = Describe("RestHandler", func() {

It("UPDATEs as array", func() {
reqBody := strings.NewReader(`{"posts": [{"id": "1", "title": "New Title"}]}`)
req, err := http.NewRequest("PUT", "/posts/1", reqBody)
req, err := http.NewRequest("PUT", "/v1/posts/1", reqBody)
Expect(err).To(BeNil())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusNoContent))
Expand Down Expand Up @@ -581,20 +581,6 @@ var _ = Describe("RestHandler", func() {
})
})

Context("when prefixing routes", func() {
It("has correct Location when creating", func() {
api := NewAPI("v1")
api.AddResource(Post{}, &fixtureSource{map[string]*Post{}})
rec := httptest.NewRecorder()
reqBody := strings.NewReader(`{"posts": [{"title": "New Post"}]}`)
req, err := http.NewRequest("POST", "/v1/posts", reqBody)
Expect(err).To(BeNil())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusCreated))
Expect(rec.Header().Get("Location")).To(Equal("/v1/posts/1"))
})
})

Context("marshal errors correctly", func() {
var (
source *fixtureSource
Expand Down

0 comments on commit ee93c36

Please sign in to comment.