-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for Multipart requests (#13)
* Implement file upload types * Publish fields of Upload struct * Rollback accidental change * Remove double encode * Fix parsing slice of files * Add test extract files * Add prepareMultipart test * Fix some misspells reported by goreportcard.com * Fix introspection go fmt
- Loading branch information
Showing
7 changed files
with
258 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package graphql | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"mime/multipart" | ||
"strconv" | ||
) | ||
|
||
type File interface { | ||
io.Reader | ||
io.Closer | ||
} | ||
type Upload struct { | ||
File File | ||
FileName string | ||
} | ||
|
||
type UploadMap []struct { | ||
upload Upload | ||
positions []string | ||
} | ||
|
||
func (u *UploadMap) UploadMap() map[string][]string { | ||
var result = make(map[string][]string) | ||
|
||
for idx, attachment := range *u { | ||
result[strconv.Itoa(idx)] = attachment.positions | ||
} | ||
|
||
return result | ||
} | ||
|
||
func (u *UploadMap) NotEmpty() bool { | ||
return len(*u) > 0 | ||
} | ||
|
||
func (u *UploadMap) Add(upload Upload, varName string) { | ||
*u = append(*u, struct { | ||
upload Upload | ||
positions []string | ||
}{ | ||
upload, | ||
[]string{fmt.Sprintf("variables.%s", varName)}, | ||
}) | ||
} | ||
|
||
// function extracts attached files and sets respective variables to null | ||
func extractFiles(input *QueryInput) *UploadMap { | ||
uploadMap := &UploadMap{} | ||
|
||
for varName, value := range input.Variables { | ||
switch valueTyped := value.(type) { | ||
case Upload: | ||
uploadMap.Add(valueTyped, varName) | ||
input.Variables[varName] = nil | ||
case []interface{}: | ||
for i, uploadVal := range valueTyped { | ||
if upload, ok := uploadVal.(Upload); ok { | ||
uploadMap.Add(upload, fmt.Sprintf("%s.%d", varName, i)) | ||
} | ||
valueTyped[i] = nil | ||
} | ||
input.Variables[varName] = valueTyped | ||
default: | ||
//noop | ||
} | ||
} | ||
return uploadMap | ||
} | ||
|
||
func prepareMultipart(payload []byte, uploadMap *UploadMap) (body []byte, contentType string, err error) { | ||
var b = bytes.Buffer{} | ||
var fw io.Writer | ||
|
||
w := multipart.NewWriter(&b) | ||
|
||
fw, err = w.CreateFormField("operations") | ||
if err != nil { | ||
return | ||
} | ||
|
||
_, err = fw.Write(payload) | ||
if err != nil { | ||
return | ||
} | ||
|
||
fw, err = w.CreateFormField("map") | ||
if err != nil { | ||
return | ||
} | ||
|
||
err = json.NewEncoder(fw).Encode(uploadMap.UploadMap()) | ||
if err != nil { | ||
return | ||
} | ||
|
||
for index, uploadVariable := range *uploadMap { | ||
fw, err := w.CreateFormFile(strconv.Itoa(index), uploadVariable.upload.FileName) | ||
if err != nil { | ||
return b.Bytes(), w.FormDataContentType(), err | ||
} | ||
|
||
_, err = io.Copy(fw, uploadVariable.upload.File) | ||
if err != nil { | ||
return b.Bytes(), w.FormDataContentType(), err | ||
} | ||
} | ||
|
||
err = w.Close() | ||
if err != nil { | ||
return | ||
} | ||
|
||
return b.Bytes(), w.FormDataContentType(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package graphql | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"io/ioutil" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestExtractFiles(t *testing.T) { | ||
|
||
upload1 := Upload{nil, "file1"} | ||
upload2 := Upload{nil, "file2"} | ||
upload3 := Upload{nil, "file3"} | ||
|
||
input := &QueryInput{ | ||
Variables: map[string]interface{}{ | ||
"stringParam": "hello world", | ||
"someFile": upload1, | ||
"allFiles": []interface{}{ | ||
upload2, | ||
upload3, | ||
}, | ||
"integerParam": 10, | ||
}, | ||
} | ||
|
||
actual := extractFiles(input) | ||
|
||
expected := &UploadMap{} | ||
expected.Add(upload1, "someFile") | ||
expected.Add(upload2, "allFiles.0") | ||
expected.Add(upload3, "allFiles.1") | ||
|
||
assert.Equal(t, expected, actual) | ||
} | ||
|
||
func TestPrepareMultipart(t *testing.T) { | ||
upload1 := Upload{ioutil.NopCloser(bytes.NewBufferString("File1Contents")), "file1"} | ||
upload2 := Upload{ioutil.NopCloser(bytes.NewBufferString("File2Contents")), "file2"} | ||
upload3 := Upload{ioutil.NopCloser(bytes.NewBufferString("File3Contents")), "file3"} | ||
|
||
uploadMap := &UploadMap{} | ||
uploadMap.Add(upload1, "someFile") | ||
uploadMap.Add(upload2, "allFiles.0") | ||
uploadMap.Add(upload3, "allFiles.1") | ||
|
||
payload, _ := json.Marshal(map[string]interface{}{ | ||
"query": "mutation TestFileUpload($someFile: Upload!,$allFiles: [Upload!]!) {upload(file: $someFile) uploadMulti(files: $allFiles)}", | ||
"variables": map[string]interface{}{ | ||
"someFile": nil, | ||
"allFiles": []interface{}{nil, nil}, | ||
}, | ||
"operationName": "TestFileUpload", | ||
}) | ||
|
||
body, contentType, err := prepareMultipart(payload, uploadMap) | ||
|
||
headerParts := strings.Split(contentType, "; boundary=") | ||
rawBody := []string{ | ||
"--%[1]s", | ||
"Content-Disposition: form-data; name=\"operations\"", | ||
"", | ||
"{\"operationName\":\"TestFileUpload\",\"query\":\"mutation TestFileUpload($someFile: Upload!,$allFiles: [Upload!]!) {upload(file: $someFile) uploadMulti(files: $allFiles)}\",\"variables\":{\"allFiles\":[null,null],\"someFile\":null}}", | ||
"--%[1]s", | ||
"Content-Disposition: form-data; name=\"map\"", | ||
"", | ||
"{\"0\":[\"variables.someFile\"],\"1\":[\"variables.allFiles.0\"],\"2\":[\"variables.allFiles.1\"]}\n", | ||
"--%[1]s", | ||
"Content-Disposition: form-data; name=\"0\"; filename=\"file1\"", | ||
"Content-Type: application/octet-stream", | ||
"", | ||
"File1Contents", | ||
"--%[1]s", | ||
"Content-Disposition: form-data; name=\"1\"; filename=\"file2\"", | ||
"Content-Type: application/octet-stream", | ||
"", | ||
"File2Contents", | ||
"--%[1]s", | ||
"Content-Disposition: form-data; name=\"2\"; filename=\"file3\"", | ||
"Content-Type: application/octet-stream", | ||
"", | ||
"File3Contents", | ||
"--%[1]s--", | ||
"", | ||
} | ||
|
||
expected := fmt.Sprintf(strings.Join(rawBody, "\r\n"), headerParts[1]) | ||
|
||
assert.Equal(t, "multipart/form-data", headerParts[0]) | ||
assert.Equal(t, expected, string(body)) | ||
assert.Nil(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters