This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
body_size_limit_test.go
80 lines (58 loc) · 2.47 KB
/
body_size_limit_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package bitsgo_test
import (
. "github.com/cloudfoundry-incubator/bits-service"
"github.com/cloudfoundry-incubator/bits-service/httputil"
"net/http"
"net/http/httptest"
"strings"
"io/ioutil"
"io"
)
type testHandler struct {
maxBodySize uint64
manipulatedContentLength int64
}
func (handler *testHandler) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {
defer GinkgoRecover()
if handler.manipulatedContentLength != 0 {
manipulateContentLength(request, handler.manipulatedContentLength)
}
if !HandleBodySizeLimits(responseWriter, request, handler.maxBodySize) {
return
}
_, e := io.Copy(responseWriter, request.Body)
Expect(e).NotTo(HaveOccurred())
}
func manipulateContentLength(request *http.Request, newContentLength int64) {
request.ContentLength = newContentLength
}
var _ = Describe("HandleBodySizeLimits", func() {
It("returns StatusRequestEntityTooLarge when body size exceeds maxBodySize", func() {
server := httptest.NewServer(&testHandler{maxBodySize: 5})
response, e := http.DefaultClient.Do(httputil.NewRequest("GET", server.URL, strings.NewReader("Hello world!")).Build())
Expect(e).NotTo(HaveOccurred())
Expect(response.StatusCode).To(Equal(http.StatusRequestEntityTooLarge))
server.Close()
})
It("does not check body sizes at all when maxBodySize is 0", func() {
server := httptest.NewServer(&testHandler{maxBodySize: 0})
response, e := http.DefaultClient.Do(httputil.NewRequest("GET", server.URL, strings.NewReader("Hello world!")).Build())
Expect(e).NotTo(HaveOccurred())
Expect(ioutil.ReadAll(response.Body)).To(MatchRegexp("^Hello world!$"))
server.Close()
})
It("uses the full body when the body size does not exceed maxBodySize", func() {
server := httptest.NewServer(&testHandler{maxBodySize: 20})
response, e := http.DefaultClient.Do(httputil.NewRequest("GET", server.URL, strings.NewReader("Hello world!")).Build())
Expect(e).NotTo(HaveOccurred())
Expect(ioutil.ReadAll(response.Body)).To(MatchRegexp("^Hello world!$"))
server.Close()
})
It("uses ContentLength as authoritative source for body sizes when ContentLength and body size differ", func() {
server := httptest.NewServer(&testHandler{maxBodySize: 20, manipulatedContentLength: 5})
response, e := http.DefaultClient.Do(httputil.NewRequest("GET", server.URL, strings.NewReader("Hello world!")).Build())
Expect(e).NotTo(HaveOccurred())
Expect(ioutil.ReadAll(response.Body)).To(MatchRegexp("^Hello$"))
server.Close()
})
})