-
Notifications
You must be signed in to change notification settings - Fork 432
/
transforms.go
51 lines (44 loc) · 1.29 KB
/
transforms.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
package transforms
import (
"bytes"
"compress/gzip"
"encoding/json"
"io"
"net/http"
)
const (
invalidDecompressorResponse = "Failed to decompress bytes"
)
// WithDecompressorTransform returns a Gomega Transform that decompresses
// a slice of bytes and returns the corresponding string
func WithDecompressorTransform() func(b []byte) string {
return func(b []byte) string {
reader, err := gzip.NewReader(bytes.NewBuffer(b))
if err != nil {
return invalidDecompressorResponse
}
defer reader.Close()
body, err := io.ReadAll(reader)
if err != nil {
return invalidDecompressorResponse
}
return string(body)
}
}
// WithHeaderValues returns a Gomega Transform that extracts the header
// values from the http Response, for the provided header name
func WithHeaderValues(header string) func(response *http.Response) []string {
return func(response *http.Response) []string {
return response.Header.Values(header)
}
}
// WithJsonBody returns a Gomega Transform that extracts the JSON body from the
// response and returns it as a map[string]interface{}
func WithJsonBody() func(b []byte) map[string]interface{} {
return func(b []byte) map[string]interface{} {
// parse the response body as JSON
var bodyJson map[string]interface{}
json.Unmarshal(b, &bodyJson)
return bodyJson
}
}