@@ -16,6 +16,8 @@ package e2e_test
16
16
17
17
import (
18
18
"bytes"
19
+ "compress/flate"
20
+ "compress/gzip"
19
21
"encoding/base64"
20
22
"encoding/json"
21
23
"fmt"
@@ -28,6 +30,8 @@ import (
28
30
"os"
29
31
"os/exec"
30
32
"path"
33
+ "slices"
34
+ "strconv"
31
35
"strings"
32
36
"testing"
33
37
@@ -54,6 +58,59 @@ type snapshotData struct {
54
58
Headers map [string ][]string
55
59
}
56
60
61
+ func (d snapshotData ) Decompress () error {
62
+ for _ , encoding := range d .Headers ["Content-Encoding" ] {
63
+ switch encoding {
64
+ case "gzip" , "x-gzip" :
65
+ return d .decompressGzip ()
66
+ case "deflate" :
67
+ return d .decompressDeflate ()
68
+ }
69
+ }
70
+
71
+ return nil
72
+ }
73
+
74
+ func (d snapshotData ) removeContentEncoding () {
75
+ d .Headers ["Content-Encoding" ] = nil
76
+ vary := d .Headers ["Vary" ]
77
+ if len (vary ) != 0 {
78
+ vary = slices .DeleteFunc (vary , func (s string ) bool {
79
+ return strings .EqualFold (s , "accept-encoding" )
80
+ })
81
+ d .Headers ["Vary" ] = vary
82
+ }
83
+ }
84
+
85
+ func (d snapshotData ) decompressGzip () error {
86
+ reader , err := gzip .NewReader (bytes .NewReader (d .Body ))
87
+ if err != nil {
88
+ return err
89
+ }
90
+ defer reader .Close ()
91
+ buf := new (bytes.Buffer )
92
+ if _ , err := io .Copy (buf , reader ); err != nil {
93
+ return err
94
+ }
95
+ d .Body = buf .Bytes ()
96
+ d .Headers ["Content-Length" ] = []string {strconv .Itoa (len (d .Body ))}
97
+ d .removeContentEncoding ()
98
+ return nil
99
+ }
100
+
101
+ func (d snapshotData ) decompressDeflate () error {
102
+ reader := flate .NewReader (bytes .NewReader (d .Body ))
103
+ defer reader .Close ()
104
+ buf := new (bytes.Buffer )
105
+ if _ , err := io .Copy (buf , reader ); err != nil {
106
+ return err
107
+ }
108
+ d .Body = buf .Bytes ()
109
+ d .Headers ["Content-Length" ] = []string {strconv .Itoa (len (d .Body ))}
110
+ d .removeContentEncoding ()
111
+ return nil
112
+ }
113
+
57
114
func (d snapshotData ) Compare (v snapshotData ) int {
58
115
methodCmp := strings .Compare (d .Method , v .Method )
59
116
if methodCmp != 0 {
@@ -558,6 +615,10 @@ func (g *atlasE2ETestGenerator) snapshotServer() {
558
615
559
616
data .Body = buf .Bytes ()
560
617
618
+ if err := data .Decompress (); err != nil {
619
+ return err
620
+ }
621
+
561
622
if g .lastData != nil && data .Compare (* g .lastData ) == 0 {
562
623
return nil // skip same content
563
624
}
0 commit comments