Skip to content

Commit

Permalink
immediately close req body
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob E Dawkins committed Oct 23, 2018
1 parent 06f92ef commit 92279bf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/
.idea/
11 changes: 11 additions & 0 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ type Client struct {
httpClient *http.Client
useMultipartForm bool

//closeReq will close the request body immediately allowing for reuse of client
closeReq bool

// Log is called with various debug information.
// To log to standard out, use:
// client.Log = func(s string) { log.Println(s) }
Expand Down Expand Up @@ -114,6 +117,7 @@ func (c *Client) runWithJSON(ctx context.Context, req *Request, resp interface{}
if err != nil {
return err
}
r.Close = c.closeReq
r.Header.Set("Content-Type", "application/json; charset=utf-8")
r.Header.Set("Accept", "application/json; charset=utf-8")
for key, values := range req.Header {
Expand Down Expand Up @@ -184,6 +188,7 @@ func (c *Client) runWithPostFields(ctx context.Context, req *Request, resp inter
if err != nil {
return err
}
r.Close = c.closeReq
r.Header.Set("Content-Type", writer.FormDataContentType())
r.Header.Set("Accept", "application/json; charset=utf-8")
for key, values := range req.Header {
Expand Down Expand Up @@ -233,6 +238,12 @@ func UseMultipartForm() ClientOption {
}
}

//ImmediatelyCloseReqBody will close the req body immediately after each request body is ready
func ImmediatelyCloseReqBody() ClientOption{
return func(client *Client) {
client.closeReq = true
}
}
// ClientOption are functions that are passed into NewClient to
// modify the behaviour of the Client.
type ClientOption func(*Client)
Expand Down
27 changes: 27 additions & 0 deletions graphql_multipart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,33 @@ func TestDoUseMultipartForm(t *testing.T) {
is.Equal(calls, 1) // calls
is.Equal(responseData["something"], "yes")
}
func TestImmediatelyCloseReqBody(t *testing.T) {
is := is.New(t)
var calls int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
is.Equal(r.Method, http.MethodPost)
query := r.FormValue("query")
is.Equal(query, `query {}`)
io.WriteString(w, `{
"data": {
"something": "yes"
}
}`)
}))
defer srv.Close()

ctx := context.Background()
client := NewClient(srv.URL, ImmediatelyCloseReqBody(), UseMultipartForm())

ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
var responseData map[string]interface{}
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
is.NoErr(err)
is.Equal(calls, 1) // calls
is.Equal(responseData["something"], "yes")
}

func TestDoErr(t *testing.T) {
is := is.New(t)
Expand Down

0 comments on commit 92279bf

Please sign in to comment.