Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ContentLength handler for checking the Content-Length HTTP header… #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ requests with no `Content-Type` header set.
`ContentType` only acts on `POST`, `PATCH` and `PUT` requests, so it is safe to
use it with `m.Use`. It will never block `GET` requests.

#### The `strict.ContentLength` handler factory

This handlers checks if the HTTP requests contain the `Content-Length` header.
If the `Content-Length` header is not present, the handler returns a HTTP *411 Length Required*
response.

Like `ContentType` and `ContentCharset`, `ContentLength` only acts
on `POST`, `PATCH` and `PUT` requests.

#### The `strict.ContentCharset` handler factory

Expand Down
10 changes: 10 additions & 0 deletions strict.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ func ContentType(ctypes ...string) http.HandlerFunc {
}
}

// ContentLength generates a handler that writes a 411 Length Required response if the request
// doesn't contain the Content-Length header
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a trailing ".", to be consistent with other comments in the file?

func ContentLength() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if contentMethod(r.Method) && r.Header.Get("Content-Length") == "" {
w.WriteHeader(http.StatusLengthRequired)
}
}
}

// ContentCharset generates a handler that writes a 415 Unsupported Media Type response if none of the charsets match.
// An empty charset will allow requests with no Content-Type header or no specified charset.
func ContentCharset(charsets ...string) http.HandlerFunc {
Expand Down
61 changes: 53 additions & 8 deletions strict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var _ = Describe("Negotiator", func() {
var err error
r, err = http.NewRequest("POST", "http://example.com/", nil)
Expect(err).
NotTo(HaveOccured())
NotTo(HaveOccurred())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aah, this is so annoying. If I had the time, I'd eradicate ginkgo/gomega from the tests altogether and use plain old testing.

Thanks for fixing this.

n = &negotiator{r}
})
It("should parse the Accept header correctly", func() {
Expand Down Expand Up @@ -49,7 +49,7 @@ var _ = Describe("ContentType", func() {
var err error
r, err = http.NewRequest("POST", "http://example.com/", nil)
Expect(err).
NotTo(HaveOccured())
NotTo(HaveOccurred())
w = httptest.NewRecorder()
})
It("should accept requests with a matching content type", func() {
Expand Down Expand Up @@ -97,7 +97,7 @@ var _ = Describe("ContentType", func() {
for _, m := range []string{"POST", "PATCH", "PUT"} {
r, err = http.NewRequest(m, "http://example.com/", nil)
Expect(err).
NotTo(HaveOccured())
NotTo(HaveOccurred())
r.Header.Set("Content-Type", "text/plain")
ContentType("application/json", "text/xml", "")(w, r)
Expect(w.Code).
Expand All @@ -109,7 +109,7 @@ var _ = Describe("ContentType", func() {
for _, m := range []string{"GET", "HEAD", "OPTIONS", "DELETE"} {
r, err = http.NewRequest(m, "http://example.com/", nil)
Expect(err).
NotTo(HaveOccured())
NotTo(HaveOccurred())
r.Header.Set("Content-Type", "text/plain")
ContentType("application/json", "text/xml", "")(w, r)
Expect(w.Code).
Expand All @@ -118,14 +118,59 @@ var _ = Describe("ContentType", func() {
})
})

var _ = Describe("ContentLength", func() {
var w *httptest.ResponseRecorder
var r *http.Request
BeforeEach(func() {
var err error
r, err = http.NewRequest("POST", "http://example.com/", nil)
Expect(err).
NotTo(HaveOccurred())
w = httptest.NewRecorder()
})
It("should accept with Content-Length header set", func() {
r.Header.Set("Content-Length", "4")
ContentLength()(w, r)
Expect(w.Code).
ToNot(Equal(http.StatusLengthRequired))
})
It("should block requests with no Content-Length set", func() {
ContentLength()(w, r)
Expect(w.Code).
To(Equal(http.StatusLengthRequired))
})
It("should act on block POST, PATCH and PUT requests", func() {
var err error
for _, m := range []string{"POST", "PATCH", "PUT"} {
r, err = http.NewRequest(m, "http://example.com/", nil)
Expect(err).
NotTo(HaveOccurred())
ContentLength()(w, r)
Expect(w.Code).
To(Equal(http.StatusLengthRequired))
}
})
It("should not block GET, HEAD, OPTIONS and DELETE requests", func() {
var err error
for _, m := range []string{"GET", "HEAD", "OPTIONS", "DELETE"} {
r, err = http.NewRequest(m, "http://example.com/", nil)
Expect(err).
NotTo(HaveOccurred())
ContentLength()(w, r)
Expect(w.Code).
NotTo(Equal(http.StatusLengthRequired))
}
})
})

var _ = Describe("ContentCharset", func() {
var w *httptest.ResponseRecorder
var r *http.Request
BeforeEach(func() {
var err error
r, err = http.NewRequest("POST", "http://example.com/", nil)
Expect(err).
NotTo(HaveOccured())
NotTo(HaveOccurred())
w = httptest.NewRecorder()
})
It("should accept requests with a matching charset", func() {
Expand Down Expand Up @@ -181,7 +226,7 @@ var _ = Describe("ContentCharset", func() {
for _, m := range []string{"POST", "PATCH", "PUT"} {
r, err = http.NewRequest(m, "http://example.com/", nil)
Expect(err).
NotTo(HaveOccured())
NotTo(HaveOccurred())
r.Header.Set("Content-Type", "text/plain")
ContentType("application/json", "text/xml", "")(w, r)
Expect(w.Code).
Expand All @@ -193,7 +238,7 @@ var _ = Describe("ContentCharset", func() {
for _, m := range []string{"GET", "HEAD", "OPTIONS", "DELETE"} {
r, err = http.NewRequest(m, "http://example.com/", nil)
Expect(err).
NotTo(HaveOccured())
NotTo(HaveOccurred())
r.Header.Set("Content-Type", "text/plain")
ContentType("application/json", "text/xml", "")(w, r)
Expect(w.Code).
Expand All @@ -209,7 +254,7 @@ var _ = Describe("Accept", func() {
var err error
r, err = http.NewRequest("POST", "http://example.com/", nil)
Expect(err).
NotTo(HaveOccured())
NotTo(HaveOccurred())
w = httptest.NewRecorder()
})
It("should accept requests with a matching content type", func() {
Expand Down