Skip to content

Commit d2c0d31

Browse files
authored
Merge pull request #23 from Melaninneal/master
immediately close req body
2 parents 06f92ef + 92279bf commit d2c0d31

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212

1313
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
1414
.glide/
15+
.idea/

graphql.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ type Client struct {
4848
httpClient *http.Client
4949
useMultipartForm bool
5050

51+
//closeReq will close the request body immediately allowing for reuse of client
52+
closeReq bool
53+
5154
// Log is called with various debug information.
5255
// To log to standard out, use:
5356
// client.Log = func(s string) { log.Println(s) }
@@ -114,6 +117,7 @@ func (c *Client) runWithJSON(ctx context.Context, req *Request, resp interface{}
114117
if err != nil {
115118
return err
116119
}
120+
r.Close = c.closeReq
117121
r.Header.Set("Content-Type", "application/json; charset=utf-8")
118122
r.Header.Set("Accept", "application/json; charset=utf-8")
119123
for key, values := range req.Header {
@@ -184,6 +188,7 @@ func (c *Client) runWithPostFields(ctx context.Context, req *Request, resp inter
184188
if err != nil {
185189
return err
186190
}
191+
r.Close = c.closeReq
187192
r.Header.Set("Content-Type", writer.FormDataContentType())
188193
r.Header.Set("Accept", "application/json; charset=utf-8")
189194
for key, values := range req.Header {
@@ -233,6 +238,12 @@ func UseMultipartForm() ClientOption {
233238
}
234239
}
235240

241+
//ImmediatelyCloseReqBody will close the req body immediately after each request body is ready
242+
func ImmediatelyCloseReqBody() ClientOption{
243+
return func(client *Client) {
244+
client.closeReq = true
245+
}
246+
}
236247
// ClientOption are functions that are passed into NewClient to
237248
// modify the behaviour of the Client.
238249
type ClientOption func(*Client)

graphql_multipart_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,33 @@ func TestDoUseMultipartForm(t *testing.T) {
6262
is.Equal(calls, 1) // calls
6363
is.Equal(responseData["something"], "yes")
6464
}
65+
func TestImmediatelyCloseReqBody(t *testing.T) {
66+
is := is.New(t)
67+
var calls int
68+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
69+
calls++
70+
is.Equal(r.Method, http.MethodPost)
71+
query := r.FormValue("query")
72+
is.Equal(query, `query {}`)
73+
io.WriteString(w, `{
74+
"data": {
75+
"something": "yes"
76+
}
77+
}`)
78+
}))
79+
defer srv.Close()
80+
81+
ctx := context.Background()
82+
client := NewClient(srv.URL, ImmediatelyCloseReqBody(), UseMultipartForm())
83+
84+
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
85+
defer cancel()
86+
var responseData map[string]interface{}
87+
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
88+
is.NoErr(err)
89+
is.Equal(calls, 1) // calls
90+
is.Equal(responseData["something"], "yes")
91+
}
6592

6693
func TestDoErr(t *testing.T) {
6794
is := is.New(t)

0 commit comments

Comments
 (0)