Skip to content

Commit 99559ca

Browse files
committed
Merge pull request #74 from univedo/trailingSlashes
Introduce method to disable automatic redirects
2 parents 62ebdde + 8338471 commit 99559ca

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

api.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ func NewAPI(prefix string) *API {
7777
}
7878
}
7979

80+
//SetRedirectTrailingSlash enables 307 redirects on urls ending with /
81+
//when disabled, an URL ending with / will 404
82+
func (api *API) SetRedirectTrailingSlash(enabled bool) {
83+
if api.router == nil {
84+
panic("router must not be nil")
85+
}
86+
87+
api.router.RedirectTrailingSlash = enabled
88+
}
89+
8090
// Request holds additional information for FindOne and Find Requests
8191
type Request struct {
8292
QueryParams map[string][]string

api_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,24 @@ var _ = Describe("RestHandler", func() {
433433
}))
434434
})
435435

436+
It("POSTSs new objects with trailing slash automatic redirect enabled", func() {
437+
reqBody := strings.NewReader(`{"posts": [{"title": "New Post"}]}`)
438+
req, err := http.NewRequest("POST", "/posts/", reqBody)
439+
Expect(err).To(BeNil())
440+
api.SetRedirectTrailingSlash(true)
441+
api.Handler().ServeHTTP(rec, req)
442+
Expect(rec.Code).To(Equal(http.StatusTemporaryRedirect))
443+
})
444+
445+
It("POSTSs new objects with trailing slash automatic redirect disabled", func() {
446+
reqBody := strings.NewReader(`{"posts": [{"title": "New Post"}]}`)
447+
req, err := http.NewRequest("POST", "/posts/", reqBody)
448+
Expect(err).To(BeNil())
449+
api.SetRedirectTrailingSlash(false)
450+
api.Handler().ServeHTTP(rec, req)
451+
Expect(rec.Code).To(Equal(http.StatusNotFound))
452+
})
453+
436454
It("POSTSs multiple objects", func() {
437455
reqBody := strings.NewReader(`{"posts": [{"title": "New Post"}, {"title" : "Second New Post"}]}`)
438456
req, err := http.NewRequest("POST", "/posts", reqBody)

0 commit comments

Comments
 (0)