Skip to content

Commit 9835de6

Browse files
author
Eric Rutherford
committed
adding a check if there is a failure to parse and returning an error about the status code if it was not 200
accounting for both paths where there might be a non-200 response
1 parent 05b17f3 commit 9835de6

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go:
44
- 1.7.x
55
- 1.8.x
66
- 1.9.x
7+
- 1.10.x
78

89
before_install:
910
- go get github.com/golang/lint/golint

graphql.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ import (
4242
"github.com/pkg/errors"
4343
)
4444

45+
const errorNon200Template = "graphql: server returned a non-200 status code: %v"
46+
4547
// Client is a client for interacting with a GraphQL API.
4648
type Client struct {
4749
endpoint string
@@ -134,6 +136,9 @@ func (c *Client) runWithJSON(ctx context.Context, req *Request, resp interface{}
134136
}
135137
c.logf("<< %s", buf.String())
136138
if err := json.NewDecoder(&buf).Decode(&gr); err != nil {
139+
if res.StatusCode != http.StatusOK {
140+
return fmt.Errorf(errorNon200Template, res.StatusCode)
141+
}
137142
return errors.Wrap(err, "decoding response")
138143
}
139144
if len(gr.Errors) > 0 {
@@ -201,6 +206,9 @@ func (c *Client) runWithPostFields(ctx context.Context, req *Request, resp inter
201206
}
202207
c.logf("<< %s", buf.String())
203208
if err := json.NewDecoder(&buf).Decode(&gr); err != nil {
209+
if res.StatusCode != http.StatusOK {
210+
return fmt.Errorf(errorNon200Template, res.StatusCode)
211+
}
204212
return errors.Wrap(err, "decoding response")
205213
}
206214
if len(gr.Errors) > 0 {

graphql_json_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,60 @@ func TestDoJSON(t *testing.T) {
4141
is.Equal(responseData["something"], "yes")
4242
}
4343

44+
func TestDoJSONServerError(t *testing.T) {
45+
is := is.New(t)
46+
var calls int
47+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
48+
calls++
49+
is.Equal(r.Method, http.MethodPost)
50+
b, err := ioutil.ReadAll(r.Body)
51+
is.NoErr(err)
52+
is.Equal(string(b), `{"query":"query {}","variables":null}`+"\n")
53+
w.WriteHeader(http.StatusInternalServerError)
54+
io.WriteString(w, `Internal Server Error`)
55+
}))
56+
defer srv.Close()
57+
58+
ctx := context.Background()
59+
client := NewClient(srv.URL)
60+
61+
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
62+
defer cancel()
63+
var responseData map[string]interface{}
64+
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
65+
is.Equal(calls, 1) // calls
66+
is.Equal(err.Error(), "graphql: server returned a non-200 status code: 500")
67+
}
68+
69+
func TestDoJSONBadRequestErr(t *testing.T) {
70+
is := is.New(t)
71+
var calls int
72+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
73+
calls++
74+
is.Equal(r.Method, http.MethodPost)
75+
b, err := ioutil.ReadAll(r.Body)
76+
is.NoErr(err)
77+
is.Equal(string(b), `{"query":"query {}","variables":null}`+"\n")
78+
w.WriteHeader(http.StatusBadRequest)
79+
io.WriteString(w, `{
80+
"errors": [{
81+
"message": "miscellaneous message as to why the the request was bad"
82+
}]
83+
}`)
84+
}))
85+
defer srv.Close()
86+
87+
ctx := context.Background()
88+
client := NewClient(srv.URL)
89+
90+
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
91+
defer cancel()
92+
var responseData map[string]interface{}
93+
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
94+
is.Equal(calls, 1) // calls
95+
is.Equal(err.Error(), "graphql: miscellaneous message as to why the the request was bad")
96+
}
97+
4498
func TestQueryJSON(t *testing.T) {
4599
is := is.New(t)
46100

graphql_multipart_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,56 @@ func TestDoErr(t *testing.T) {
9090
is.Equal(err.Error(), "graphql: Something went wrong")
9191
}
9292

93+
func TestDoServerErr(t *testing.T) {
94+
is := is.New(t)
95+
var calls int
96+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
97+
calls++
98+
is.Equal(r.Method, http.MethodPost)
99+
query := r.FormValue("query")
100+
is.Equal(query, `query {}`)
101+
w.WriteHeader(http.StatusInternalServerError)
102+
io.WriteString(w, `Internal Server Error`)
103+
}))
104+
defer srv.Close()
105+
106+
ctx := context.Background()
107+
client := NewClient(srv.URL, UseMultipartForm())
108+
109+
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
110+
defer cancel()
111+
var responseData map[string]interface{}
112+
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
113+
is.Equal(err.Error(), "graphql: server returned a non-200 status code: 500")
114+
}
115+
116+
func TestDoBadRequestErr(t *testing.T) {
117+
is := is.New(t)
118+
var calls int
119+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
120+
calls++
121+
is.Equal(r.Method, http.MethodPost)
122+
query := r.FormValue("query")
123+
is.Equal(query, `query {}`)
124+
w.WriteHeader(http.StatusBadRequest)
125+
io.WriteString(w, `{
126+
"errors": [{
127+
"message": "miscellaneous message as to why the the request was bad"
128+
}]
129+
}`)
130+
}))
131+
defer srv.Close()
132+
133+
ctx := context.Background()
134+
client := NewClient(srv.URL, UseMultipartForm())
135+
136+
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
137+
defer cancel()
138+
var responseData map[string]interface{}
139+
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
140+
is.Equal(err.Error(), "graphql: miscellaneous message as to why the the request was bad")
141+
}
142+
93143
func TestDoNoResponse(t *testing.T) {
94144
is := is.New(t)
95145
var calls int

0 commit comments

Comments
 (0)