-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform_test.go
77 lines (74 loc) · 1.74 KB
/
transform_test.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package hypert
import (
"bytes"
"io"
"net/http"
"testing"
)
// User represents a user in the system.
// TODO: This type is currently unused but will be used in future tests.
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
func TestTransformResponseFormatJSON(t *testing.T) {
tests := []struct {
name string
body string
want string
transform ResponseTransform
}{
{
name: "Simple JSON",
body: `{"name":"John","age":30}`,
want: `{
"name": "John",
"age": 30
}`,
transform: TransformResponseFormatJSON(),
},
{
name: "JSON with nested object",
body: `{"name":"John","age":30,"address":{"city":"New York","country":"USA"}}`,
want: `{
"name": "John",
"age": 30,
"address": {
"city": "New York",
"country": "USA"
}
}`,
transform: TransformResponseFormatJSON(),
},
{
name: "composed",
body: `"wassup`,
want: `{
"initial": "transformation"
}`,
transform: ComposeTransforms(
ResponseTransformFunc(func(r *http.Response) *http.Response {
r.Body = io.NopCloser(bytes.NewBufferString(`{"initial":"transformation"}`))
return r
}),
TransformResponseFormatJSON(),
),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := &http.Response{Body: io.NopCloser(bytes.NewBufferString(tt.body))}
gotTransformed := tt.transform.TransformResponse(got)
bodyBytes, err := io.ReadAll(gotTransformed.Body)
if err != nil {
t.Fatalf("Failed to read response body: %v", err)
}
gotTransformed.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
defer gotTransformed.Body.Close()
if string(bodyBytes) != tt.want {
t.Errorf("Response body = %v, want %v", string(bodyBytes), tt.want)
}
got.Body.Close()
})
}
}