Skip to content

Commit 5ef657e

Browse files
committed
chore: decompress response body
1 parent 9b73a6d commit 5ef657e

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

test/e2e/atlas_e2e_test_generator_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ package e2e_test
1616

1717
import (
1818
"bytes"
19+
"compress/flate"
20+
"compress/gzip"
1921
"encoding/base64"
2022
"encoding/json"
2123
"fmt"
@@ -28,6 +30,8 @@ import (
2830
"os"
2931
"os/exec"
3032
"path"
33+
"slices"
34+
"strconv"
3135
"strings"
3236
"testing"
3337

@@ -54,6 +58,59 @@ type snapshotData struct {
5458
Headers map[string][]string
5559
}
5660

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+
57114
func (d snapshotData) Compare(v snapshotData) int {
58115
methodCmp := strings.Compare(d.Method, v.Method)
59116
if methodCmp != 0 {
@@ -558,6 +615,10 @@ func (g *atlasE2ETestGenerator) snapshotServer() {
558615

559616
data.Body = buf.Bytes()
560617

618+
if err := data.Decompress(); err != nil {
619+
return err
620+
}
621+
561622
if g.lastData != nil && data.Compare(*g.lastData) == 0 {
562623
return nil // skip same content
563624
}

0 commit comments

Comments
 (0)