-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathrewritebody_test.go
173 lines (160 loc) · 3.73 KB
/
rewritebody_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package plugin_rewritebody
import (
"bytes"
"context"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"testing"
)
func TestServeHTTP(t *testing.T) {
tests := []struct {
desc string
contentEncoding string
rewrites []Rewrite
lastModified bool
resBody string
expResBody string
expLastModified bool
}{
{
desc: "should replace foo by bar",
rewrites: []Rewrite{
{
Regex: "foo",
Replacement: "bar",
},
},
resBody: "foo is the new bar",
expResBody: "bar is the new bar",
},
{
desc: "should replace foo by bar, then by foo",
rewrites: []Rewrite{
{
Regex: "foo",
Replacement: "bar",
},
{
Regex: "bar",
Replacement: "foo",
},
},
resBody: "foo is the new bar",
expResBody: "foo is the new foo",
},
{
desc: "should not replace anything if content encoding is not identity or empty",
rewrites: []Rewrite{
{
Regex: "foo",
Replacement: "bar",
},
},
contentEncoding: "gzip",
resBody: "foo is the new bar",
expResBody: "foo is the new bar",
},
{
desc: "should replace foo by bar if content encoding is identity",
rewrites: []Rewrite{
{
Regex: "foo",
Replacement: "bar",
},
},
contentEncoding: "identity",
resBody: "foo is the new bar",
expResBody: "bar is the new bar",
},
{
desc: "should not remove the last modified header",
rewrites: []Rewrite{
{
Regex: "foo",
Replacement: "bar",
},
},
contentEncoding: "identity",
lastModified: true,
resBody: "foo is the new bar",
expResBody: "bar is the new bar",
expLastModified: true,
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
config := &Config{
LastModified: test.lastModified,
Rewrites: test.rewrites,
}
next := func(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Content-Encoding", test.contentEncoding)
rw.Header().Set("Last-Modified", "Thu, 02 Jun 2016 06:01:08 GMT")
rw.Header().Set("Content-Length", strconv.Itoa(len(test.resBody)))
rw.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(rw, test.resBody)
}
rewriteBody, err := New(context.Background(), http.HandlerFunc(next), config, "rewriteBody")
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/", nil)
rewriteBody.ServeHTTP(recorder, req)
if _, exists := recorder.Result().Header["Last-Modified"]; exists != test.expLastModified {
t.Errorf("got last-modified header %v, want %v", exists, test.expLastModified)
}
if _, exists := recorder.Result().Header["Content-Length"]; exists {
t.Error("The Content-Length Header must be deleted")
}
if !bytes.Equal([]byte(test.expResBody), recorder.Body.Bytes()) {
t.Errorf("got body %q, want %q", recorder.Body.Bytes(), test.expResBody)
}
})
}
}
func TestNew(t *testing.T) {
tests := []struct {
desc string
rewrites []Rewrite
expErr bool
}{
{
desc: "should return no error",
rewrites: []Rewrite{
{
Regex: "foo",
Replacement: "bar",
},
{
Regex: "bar",
Replacement: "foo",
},
},
expErr: false,
},
{
desc: "should return an error",
rewrites: []Rewrite{
{
Regex: "*",
Replacement: "bar",
},
},
expErr: true,
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
config := &Config{
Rewrites: test.rewrites,
}
_, err := New(context.Background(), nil, config, "rewriteBody")
if test.expErr && err == nil {
t.Fatal("expected error on bad regexp format")
}
})
}
}