forked from Khan/genqlient
/
roundtrip.go
128 lines (113 loc) · 3.51 KB
/
roundtrip.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package integration
// Machinery for integration tests to round-trip check the JSON-marshalers and
// unmarshalers we generate.
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
"github.com/infiotinc/genqlient/graphql"
"github.com/stretchr/testify/assert"
)
// lastResponseTransport is an HTTP transport that keeps track of the last response
// that passed through it.
type lastResponseTransport struct {
wrapped http.RoundTripper
lastResponseBody []byte
}
func (t *lastResponseTransport) RoundTrip(req *http.Request) (*http.Response, error) {
resp, err := t.wrapped.RoundTrip(req)
if err != nil {
return resp, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return resp, fmt.Errorf("roundtrip failed: unreadable body: %w", err)
}
t.lastResponseBody = body
// Restore the body for the next reader:
resp.Body = io.NopCloser(bytes.NewBuffer(body))
return resp, err
}
// roundtripClient is a graphql.Client that checks that
//
// unmarshal(marshal(req)) == req && marshal(unmarshal(resp)) == resp
//
// for each request it processes.
type roundtripClient struct {
wrapped graphql.Client
transport *lastResponseTransport
t *testing.T
}
// Put JSON in a stable and human-readable format.
func (c *roundtripClient) formatJSON(b []byte) []byte {
// We don't care about key ordering, so do another roundtrip through
// interface{} to drop that.
var parsed interface{}
err := json.Unmarshal(b, &parsed)
if err != nil {
c.t.Fatal(err)
}
// When marshaling, add indents to make things human-readable.
b, err = json.MarshalIndent(parsed, "", " ")
if err != nil {
c.t.Fatal(err)
}
return b
}
func (c *roundtripClient) roundtripResponse(resp interface{}) {
var graphqlResponse struct {
Data json.RawMessage `json:"data"`
}
err := json.Unmarshal(c.transport.lastResponseBody, &graphqlResponse)
if err != nil {
c.t.Error(err)
return
}
body := c.formatJSON(graphqlResponse.Data)
// resp is constructed to be unmarshal(body), so just use it
bodyAgain, err := json.Marshal(resp)
if err != nil {
c.t.Error(err)
return
}
bodyAgain = c.formatJSON(bodyAgain)
assert.Equal(c.t, string(body), string(bodyAgain))
}
func (c *roundtripClient) MakeRequest(ctx context.Context, req *graphql.Request, resp *graphql.Response) error {
// TODO(benkraft): Also check the variables round-trip. This is a bit less
// important since most of the code is the same (and input types are
// strictly simpler), and a bit hard to do because when asserting about
// structs we need to worry about things like equality of time.Time values.
err := c.wrapped.MakeRequest(ctx, req, resp)
if err != nil {
return err
}
c.roundtripResponse(resp.Data)
return nil
}
func newRoundtripClients(t *testing.T, endpoint string) []graphql.Client {
return []graphql.Client{newRoundtripClient(t, endpoint), newRoundtripGetClient(t, endpoint)}
}
func newRoundtripClient(t *testing.T, endpoint string) graphql.Client {
transport := &lastResponseTransport{wrapped: http.DefaultTransport}
httpClient := &http.Client{Transport: transport}
return &roundtripClient{
wrapped: graphql.NewClient(endpoint, httpClient),
transport: transport,
t: t,
}
}
func newRoundtripGetClient(t *testing.T, endpoint string) graphql.Client {
transport := &lastResponseTransport{wrapped: http.DefaultTransport}
httpClient := &http.Client{Transport: transport}
return &roundtripClient{
wrapped: graphql.NewClientUsingGet(endpoint, httpClient),
transport: transport,
t: t,
}
}