@@ -161,6 +161,49 @@ func TestProxy(t *testing.T) {
161
161
t .Errorf ("unexpected response body (-got +want):\n %s" , diff )
162
162
}
163
163
})
164
+ t .Run ("plain: body tags get the script inserted ignoring js with body tags" , func (t * testing.T ) {
165
+ // Arrange
166
+ r := & http.Response {
167
+ Body : io .NopCloser (strings .NewReader (`<html><body><script>console.log("<body></body>")</script></body></html>` )),
168
+ Header : make (http.Header ),
169
+ Request : & http.Request {
170
+ URL : & url.URL {
171
+ Scheme : "http" ,
172
+ Host : "example.com" ,
173
+ },
174
+ },
175
+ }
176
+ r .Header .Set ("Content-Type" , "text/html, charset=utf-8" )
177
+ r .Header .Set ("Content-Length" , "26" )
178
+
179
+ expectedString := insertScriptTagIntoBody (`<html><body><script>console.log("<body></body>")</script></body></html>` )
180
+ if ! strings .Contains (expectedString , scriptTag ) {
181
+ t .Fatalf ("expected the script tag to be inserted, but it wasn't: %q" , expectedString )
182
+ }
183
+ if ! strings .Contains (expectedString , `console.log("<body></body>")` ) {
184
+ t .Fatalf ("expected the script tag to be inserted, but mangled the html: %q" , expectedString )
185
+ }
186
+
187
+ // Act
188
+ log := slog .New (slog .NewJSONHandler (io .Discard , nil ))
189
+ h := New (log , "127.0.0.1" , 7474 , & url.URL {Scheme : "http" , Host : "example.com" })
190
+ err := h .modifyResponse (r )
191
+ if err != nil {
192
+ t .Fatalf ("unexpected error: %v" , err )
193
+ }
194
+
195
+ // Assert
196
+ if r .Header .Get ("Content-Length" ) != fmt .Sprintf ("%d" , len (expectedString )) {
197
+ t .Errorf ("expected content length to be %d, got %v" , len (expectedString ), r .Header .Get ("Content-Length" ))
198
+ }
199
+ actualBody , err := io .ReadAll (r .Body )
200
+ if err != nil {
201
+ t .Fatalf ("unexpected error reading response: %v" , err )
202
+ }
203
+ if diff := cmp .Diff (expectedString , string (actualBody )); diff != "" {
204
+ t .Errorf ("unexpected response body (-got +want):\n %s" , diff )
205
+ }
206
+ })
164
207
t .Run ("gzip: non-html content is not modified" , func (t * testing.T ) {
165
208
// Arrange
166
209
r := & http.Response {
0 commit comments