forked from ergoz/krakend-sizelimit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsizelimit_test.go
148 lines (129 loc) · 3.24 KB
/
sizelimit_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package sizelimit
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
)
func TestIntNoUnit(t *testing.T) {
cfg := new(Config)
cfg.MaxSize = "10"
parsed := ParseMaxSize(cfg)
if parsed != 10 {
t.Fatalf("MaxSize parsed incorrectly. Got %d", parsed)
}
}
func TestFloatNoUnit(t *testing.T) {
cfg := new(Config)
cfg.MaxSize = "10.0"
parsed := ParseMaxSize(cfg)
if parsed != 10 {
t.Fatalf("MaxSize parsed incorrectly. Got %d", parsed)
}
}
func TestB(t *testing.T) {
cfg := new(Config)
cfg.MaxSize = "10B"
parsed := ParseMaxSize(cfg)
if parsed != 10 {
t.Fatalf("MaxSize parsed incorrectly. Got %d", parsed)
}
}
func TestBFloat(t *testing.T) {
cfg := new(Config)
cfg.MaxSize = "10.7B"
parsed := ParseMaxSize(cfg)
if parsed != 10 {
t.Fatalf("MaxSize parsed incorrectly. Got %d", parsed)
}
}
func TestKB(t *testing.T) {
cfg := new(Config)
cfg.MaxSize = "10kB"
parsed := ParseMaxSize(cfg)
if parsed != 10000 {
t.Fatalf("MaxSize parsed incorrectly. Got %d", parsed)
}
}
func TestKBFloat(t *testing.T) {
cfg := new(Config)
cfg.MaxSize = "10.7kB"
parsed := ParseMaxSize(cfg)
if parsed != 10700 {
t.Fatalf("MaxSize parsed incorrectly. Got %d", parsed)
}
}
func TestMB(t *testing.T) {
cfg := new(Config)
cfg.MaxSize = "10MB"
parsed := ParseMaxSize(cfg)
if parsed != 10000000 {
t.Fatalf("MaxSize parsed incorrectly. Got %d", parsed)
}
}
func TestGB(t *testing.T) {
cfg := new(Config)
cfg.MaxSize = "10GB"
parsed := ParseMaxSize(cfg)
if parsed != 10000000000 {
t.Fatalf("MaxSize parsed incorrectly. Got %d", parsed)
}
}
func TestTB(t *testing.T) {
cfg := new(Config)
cfg.MaxSize = "10TB"
parsed := ParseMaxSize(cfg)
if parsed != 10000000000000 {
t.Fatalf("MaxSize parsed incorrectly. Got %d", parsed)
}
}
func TestRequestSizeBelowLimit(t *testing.T) {
body := []byte("hello")
router := makeRouter(int64(len(body))+1, body)
res := performRequest(body, router)
if res.Code != http.StatusOK {
t.Fatalf("returned %v '%s'. should return 200", res.Code, res.Body)
}
}
func TestRequestSizeAtLimit(t *testing.T) {
body := []byte("hello")
router := makeRouter(int64(len(body)), body)
res := performRequest(body, router)
if res.Code != http.StatusOK {
t.Fatalf("returned %v %s. should return 200", res.Code, res.Body)
}
}
func TestRequestSizeAboveLimit(t *testing.T) {
body := []byte("hello")
router := makeRouter(int64(len(body))-1, body)
res := performRequest(body, router)
if res.Code != http.StatusRequestEntityTooLarge {
t.Fatalf("returned %v %s. should return 413", res.Code, res.Body)
}
}
func makeRouter(limit int64, bodySent []byte) *gin.Engine {
router := gin.New()
limiter := func(c *gin.Context) {
LimiterFactory(limit, func(c *gin.Context) { c.Next() })(c)
}
success := func(c *gin.Context) {
body, _ := io.ReadAll(c.Request.Body)
if !bytes.Equal(body, bodySent) {
c.String(http.StatusInternalServerError, string(body))
} else {
c.String(http.StatusOK, "OK")
}
}
router.POST("/test", limiter, success)
return router
}
func performRequest(body []byte, router *gin.Engine) *httptest.ResponseRecorder {
buf := new(bytes.Buffer)
buf.Write(body)
r := httptest.NewRequest(http.MethodPost, "/test", buf)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
return w
}