-
Notifications
You must be signed in to change notification settings - Fork 6
/
max_body_bytes_test.go
119 lines (102 loc) · 3.24 KB
/
max_body_bytes_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2016, Janoš Guljaš <[email protected]>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package web
import (
"bytes"
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestMaxBodyBytesHandler_Pass(t *testing.T) {
h := MaxBodyBytesHandler{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("ok"))
}),
}
r := httptest.NewRequest("POST", "/", nil)
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
if w.Body.String() != "ok" {
t.Errorf("expected response body %q, got %q", "ok", w.Body.String())
}
}
func TestMaxBodyBytesHandler_Block(t *testing.T) {
h := MaxBodyBytesHandler{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := io.Copy(io.Discard, r.Body); err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
}
}),
Limit: 10,
BodyFunc: func(r *http.Request) (body string, err error) {
return http.StatusText(http.StatusRequestEntityTooLarge), nil
},
ContentType: "text/plain",
}
r := httptest.NewRequest("POST", "/", bytes.NewReader([]byte("12345678901")))
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
b, err := io.ReadAll(w.Result().Body)
if err != nil {
t.Error(err)
}
if string(b) != http.StatusText(http.StatusRequestEntityTooLarge)+"\n" {
t.Errorf("expected response body %q, got %q", http.StatusText(http.StatusRequestEntityTooLarge)+"\n", string(b))
}
contentType := w.Result().Header.Get("Content-Type")
if contentType != "text/plain" {
t.Errorf("expected response Content-Type header %q, got %q", "text/plain", contentType)
}
}
func TestMaxBodyBytesHandler_PanicError(t *testing.T) {
h := MaxBodyBytesHandler{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := io.Copy(io.Discard, r.Body); err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
}
}),
Limit: 10,
BodyFunc: func(r *http.Request) (body string, err error) {
return "", errTestMaxBodyBytesHandler
},
}
defer func() {
if err := recover(); err != errTestMaxBodyBytesHandler {
t.Errorf("expected %v error, got %v", errTestMaxBodyBytesHandler, err)
}
}()
r := httptest.NewRequest("POST", "/", bytes.NewReader([]byte("12345678901")))
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
}
var errTestMaxBodyBytesHandler = errors.New("test")
func TestMaxBodyBytesHandler_CustomError(t *testing.T) {
var e error
h := MaxBodyBytesHandler{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := io.Copy(io.Discard, r.Body); err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
}
}),
Limit: 10,
BodyFunc: func(r *http.Request) (body string, err error) {
return "", errTestMaxBodyBytesHandler
},
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
e = err
},
}
r := httptest.NewRequest("POST", "/", bytes.NewReader([]byte("12345678901")))
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
if e != errTestMaxBodyBytesHandler {
t.Errorf("expected %v error, got %v", errTestMaxBodyBytesHandler, e)
}
}