-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
135 lines (102 loc) · 2.91 KB
/
client_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
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
129
130
131
132
133
134
135
package graphql
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
)
func TestNewClient(t *testing.T) {
t.Run("OK", func(t *testing.T) {
hf := func(w http.ResponseWriter, r *http.Request) {
if got, want := r.Header.Get("X-Test"), "header-value"; got != want {
t.Fatalf(`r.Header.Get("Example-Header") = %q, want %q`, got, want)
}
json.NewEncoder(w).Encode(map[string]interface{}{
"data": map[string]interface{}{
"test": "value in response",
},
})
}
ts := httptest.NewServer(http.HandlerFunc(hf))
defer ts.Close()
c := NewClient(ts.URL, WithHTTPClient(nil))
if c.httpClient == nil {
t.Fatalf("expected httpClient to be set")
}
req := NewRequest("query { test }")
req.Header.Add("X-Test", "header-value")
var resp struct {
Test string
}
if err := c.Run(context.Background(), req, &resp); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got, want := resp.Test, "value in response"; got != want {
t.Fatalf("resp.Test = %q, want %q", got, want)
}
})
t.Run("non-200 status code", func(t *testing.T) {
hf := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusTeapot)
}
ts := httptest.NewServer(http.HandlerFunc(hf))
defer ts.Close()
c := NewClient(ts.URL)
req := NewRequest("")
err := c.Run(context.Background(), req, nil)
if err == nil {
t.Fatalf("expected error")
}
if got, want := err.Error(), "graphql: server returned a non-200 status code: 418"; got != want {
t.Fatalf("err.Error() = %q, want %q", got, want)
}
})
t.Run("decoding response", func(t *testing.T) {
hf := func(w http.ResponseWriter, r *http.Request) {}
ts := httptest.NewServer(http.HandlerFunc(hf))
defer ts.Close()
c := NewClient(ts.URL)
req := NewRequest("")
err := c.Run(context.Background(), req, nil)
if err == nil {
t.Fatalf("expected error")
}
if got, want := err.Error(), "decoding response: EOF"; got != want {
t.Fatalf("err.Error() = %q, want %q", got, want)
}
})
t.Run("GraphQL errors", func(t *testing.T) {
hf := func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]interface{}{
"errors": []map[string]interface{}{
{"message": "first"},
{"message": "second"},
},
})
}
ts := httptest.NewServer(http.HandlerFunc(hf))
defer ts.Close()
c := NewClient(ts.URL)
req := NewRequest("")
err := c.Run(context.Background(), req, nil)
if err == nil {
t.Fatalf("expected error")
}
if got, want := err.Error(), "graphql: first; second"; got != want {
t.Fatalf("err.Error() = %q, want %q", got, want)
}
})
}
func TestClientRun(t *testing.T) {
t.Run("Done context", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
c := &Client{}
err := c.Run(ctx, nil, nil)
if !errors.Is(err, context.Canceled) {
t.Fatalf("unexpected error: %v", err)
}
})
}