@@ -19,6 +19,7 @@ import (
1919
2020 "github.com/andybalholm/brotli"
2121 "github.com/google/go-cmp/cmp"
22+ "golang.org/x/net/html"
2223)
2324
2425func TestRoundTripper (t * testing.T ) {
@@ -49,6 +50,16 @@ func TestRoundTripper(t *testing.T) {
4950 })
5051}
5152
53+ func getScriptTag (t * testing.T , nonce string ) string {
54+ script := reloadScript (nonce )
55+ var buf bytes.Buffer
56+ err := html .Render (& buf , script )
57+ if err != nil {
58+ t .Fatalf ("unexpected error rendering script tag: %v" , err )
59+ }
60+ return buf .String ()
61+ }
62+
5263func TestProxy (t * testing.T ) {
5364 t .Run ("plain: non-html content is not modified" , func (t * testing.T ) {
5465 // Arrange
@@ -136,16 +147,18 @@ func TestProxy(t *testing.T) {
136147 r .Header .Set ("Content-Type" , "text/html, charset=utf-8" )
137148 r .Header .Set ("Content-Length" , "26" )
138149
139- expectedString := insertScriptTagIntoBody ("" , `<html><body></body></html>` )
140- if ! strings .Contains (expectedString , getScriptTag ("" )) {
150+ expectedString , err := insertScriptTagIntoBody ("" , `<html><body></body></html>` )
151+ if err != nil {
152+ t .Fatalf ("unexpected error inserting script: %v" , err )
153+ }
154+ if ! strings .Contains (expectedString , getScriptTag (t , "" )) {
141155 t .Fatalf ("expected the script tag to be inserted, but it wasn't: %q" , expectedString )
142156 }
143157
144158 // Act
145159 log := slog .New (slog .NewJSONHandler (io .Discard , nil ))
146160 h := New (log , "127.0.0.1" , 7474 , & url.URL {Scheme : "http" , Host : "example.com" })
147- err := h .modifyResponse (r )
148- if err != nil {
161+ if err = h .modifyResponse (r ); err != nil {
149162 t .Fatalf ("unexpected error: %v" , err )
150163 }
151164
@@ -178,16 +191,18 @@ func TestProxy(t *testing.T) {
178191 const nonce = "this-is-the-nonce"
179192 r .Header .Set ("Content-Security-Policy" , fmt .Sprintf ("script-src 'nonce-%s'" , nonce ))
180193
181- expectedString := insertScriptTagIntoBody (nonce , `<html><body></body></html>` )
182- if ! strings .Contains (expectedString , getScriptTag (nonce )) {
194+ expectedString , err := insertScriptTagIntoBody (nonce , `<html><body></body></html>` )
195+ if err != nil {
196+ t .Fatalf ("unexpected error inserting script: %v" , err )
197+ }
198+ if ! strings .Contains (expectedString , getScriptTag (t , nonce )) {
183199 t .Fatalf ("expected the script tag to be inserted, but it wasn't: %q" , expectedString )
184200 }
185201
186202 // Act
187203 log := slog .New (slog .NewJSONHandler (io .Discard , nil ))
188204 h := New (log , "127.0.0.1" , 7474 , & url.URL {Scheme : "http" , Host : "example.com" })
189- err := h .modifyResponse (r )
190- if err != nil {
205+ if err = h .modifyResponse (r ); err != nil {
191206 t .Fatalf ("unexpected error: %v" , err )
192207 }
193208
@@ -218,8 +233,11 @@ func TestProxy(t *testing.T) {
218233 r .Header .Set ("Content-Type" , "text/html, charset=utf-8" )
219234 r .Header .Set ("Content-Length" , "26" )
220235
221- expectedString := insertScriptTagIntoBody ("" , `<html><body><script>console.log("<body></body>")</script></body></html>` )
222- if ! strings .Contains (expectedString , getScriptTag ("" )) {
236+ expectedString , err := insertScriptTagIntoBody ("" , `<html><body><script>console.log("<body></body>")</script></body></html>` )
237+ if err != nil {
238+ t .Fatalf ("unexpected error inserting script: %v" , err )
239+ }
240+ if ! strings .Contains (expectedString , getScriptTag (t , "" )) {
223241 t .Fatalf ("expected the script tag to be inserted, but it wasn't: %q" , expectedString )
224242 }
225243 if ! strings .Contains (expectedString , `console.log("<body></body>")` ) {
@@ -229,8 +247,7 @@ func TestProxy(t *testing.T) {
229247 // Act
230248 log := slog .New (slog .NewJSONHandler (io .Discard , nil ))
231249 h := New (log , "127.0.0.1" , 7474 , & url.URL {Scheme : "http" , Host : "example.com" })
232- err := h .modifyResponse (r )
233- if err != nil {
250+ if err = h .modifyResponse (r ); err != nil {
234251 t .Fatalf ("unexpected error: %v" , err )
235252 }
236253
@@ -295,7 +312,10 @@ func TestProxy(t *testing.T) {
295312 }
296313 gzw .Close ()
297314
298- expectedString := insertScriptTagIntoBody ("" , body )
315+ expectedString , err := insertScriptTagIntoBody ("" , body )
316+ if err != nil {
317+ t .Fatalf ("unexpected error inserting script: %v" , err )
318+ }
299319
300320 var expectedBytes bytes.Buffer
301321 gzw = gzip .NewWriter (& expectedBytes )
@@ -356,7 +376,10 @@ func TestProxy(t *testing.T) {
356376 }
357377 brw .Close ()
358378
359- expectedString := insertScriptTagIntoBody ("" , body )
379+ expectedString , err := insertScriptTagIntoBody ("" , body )
380+ if err != nil {
381+ t .Fatalf ("unexpected error inserting script: %v" , err )
382+ }
360383
361384 var expectedBytes bytes.Buffer
362385 brw = brotli .NewWriter (& expectedBytes )
0 commit comments