Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Update GQLgen test client to work with multipart form data" #1659

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"regexp"

"github.com/mitchellh/mapstructure"
)
Expand Down Expand Up @@ -121,18 +120,15 @@ func (p *Client) newRequest(query string, options ...Option) (*http.Request, err
option(bd)
}

contentType := bd.HTTP.Header.Get("Content-Type")
switch {
case regexp.MustCompile(`multipart/form-data; ?boundary=.*`).MatchString(contentType):
break
case "application/json" == contentType:
switch bd.HTTP.Header.Get("Content-Type") {
case "application/json":
requestBody, err := json.Marshal(bd)
if err != nil {
return nil, fmt.Errorf("encode: %w", err)
}
bd.HTTP.Body = ioutil.NopCloser(bytes.NewBuffer(requestBody))
default:
panic("unsupported encoding " + bd.HTTP.Header.Get("Content-Type"))
panic("unsupported encoding" + bd.HTTP.Header.Get("Content-Type"))
}

return bd.HTTP, nil
Expand Down
41 changes: 0 additions & 41 deletions client/client_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package client_test

import (
"bytes"
"encoding/json"
"io/ioutil"
"mime/multipart"
"net/http"
"net/textproto"
"testing"

"github.com/99designs/gqlgen/client"
Expand Down Expand Up @@ -42,44 +39,6 @@ func TestClient(t *testing.T) {
require.Equal(t, "bob", resp.Name)
}

func TestClientMultipartFormData(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
bodyBytes, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)
require.Contains(t, string(bodyBytes), `Content-Disposition: form-data; name="operations"`)
require.Contains(t, string(bodyBytes), `{"query":"mutation ($input: Input!) {}","variables":{"file":{}}`)
require.Contains(t, string(bodyBytes), `Content-Disposition: form-data; name="map"`)
require.Contains(t, string(bodyBytes), `{"0":["variables.file"]}`)
require.Contains(t, string(bodyBytes), `Content-Disposition: form-data; name="0"; filename="example.txt"`)
require.Contains(t, string(bodyBytes), `Content-Type: text/plain`)
require.Contains(t, string(bodyBytes), `Hello World`)

w.Write([]byte(`{}`))
})

c := client.New(h)

var resp struct{}
c.MustPost("{ id }", &resp,
func(bd *client.Request) {
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
bodyWriter.WriteField("operations", `{"query":"mutation ($input: Input!) {}","variables":{"file":{}}`)
bodyWriter.WriteField("map", `{"0":["variables.file"]}`)

h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", `form-data; name="0"; filename="example.txt"`)
h.Set("Content-Type", "text/plain")
ff, _ := bodyWriter.CreatePart(h)
ff.Write([]byte("Hello World"))
bodyWriter.Close()

bd.HTTP.Body = ioutil.NopCloser(bodyBuf)
bd.HTTP.Header.Set("Content-Type", bodyWriter.FormDataContentType())
},
)
}

func TestAddHeader(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "ASDF", r.Header.Get("Test-Key"))
Expand Down
133 changes: 0 additions & 133 deletions client/withfilesoption.go

This file was deleted.

Loading