Skip to content

Commit

Permalink
Fix strict withFiles option test for race conditions
Browse files Browse the repository at this point in the history
Fix a problem with how strict the test's expected response was for tests
with files in their request, since it always expected a strict order of
files input that is somewhat random or dependent on what OS it is
running the test on and/or race condition
  • Loading branch information
Sonna committed Oct 15, 2021
1 parent 27f3e11 commit 78489d6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 51 deletions.
90 changes: 51 additions & 39 deletions client/withfilesoption_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package client_test

import (
"fmt"
"io"
"io/ioutil"
"mime"
Expand Down Expand Up @@ -45,17 +44,16 @@ func TestWithFiles(t *testing.T) {
require.NoError(t, err)

contentDisposition := p.Header.Get("Content-Disposition")
fmt.Printf("Part %q: %q\n", contentDisposition, slurp)

if contentDisposition == `form-data; name="operations"` {
require.Equal(t, `{"query":"{ id }","variables":{"file":{}}}`, string(slurp))
require.EqualValues(t, `{"query":"{ id }","variables":{"file":{}}}`, slurp)
}
if contentDisposition == `form-data; name="map"` {
require.Equal(t, `{"0":["variables.file"]}`, string(slurp))
require.EqualValues(t, `{"0":["variables.file"]}`, slurp)
}
if regexp.MustCompile(`form-data; name="0"; filename=.*`).MatchString(contentDisposition) {
require.Equal(t, `text/plain; charset=utf-8`, p.Header.Get("Content-Type"))
require.Equal(t, `The quick brown fox jumps over the lazy dog`, string(slurp))
require.EqualValues(t, `The quick brown fox jumps over the lazy dog`, slurp)
}
}
w.Write([]byte(`{}`))
Expand Down Expand Up @@ -88,21 +86,25 @@ func TestWithFiles(t *testing.T) {
require.NoError(t, err)

contentDisposition := p.Header.Get("Content-Disposition")
fmt.Printf("Part %q: %q\n", contentDisposition, slurp)

if contentDisposition == `form-data; name="operations"` {
require.Equal(t, `{"query":"{ id }","variables":{"input":{"files":[{},{}]}}}`, string(slurp))
require.EqualValues(t, `{"query":"{ id }","variables":{"input":{"files":[{},{}]}}}`, slurp)
}
if contentDisposition == `form-data; name="map"` {
require.Equal(t, `{"0":["variables.input.files.0"],"1":["variables.input.files.1"]}`, string(slurp))
}
if regexp.MustCompile(`form-data; name="0"; filename=.*`).MatchString(contentDisposition) {
require.Equal(t, `text/plain; charset=utf-8`, p.Header.Get("Content-Type"))
require.Equal(t, `The quick brown fox jumps over the lazy dog`, string(slurp))
}
if regexp.MustCompile(`form-data; name="1"; filename=.*`).MatchString(contentDisposition) {
// returns `{"0":["variables.input.files.0"],"1":["variables.input.files.1"]}`
// but the order of file inputs is unpredictable between different OS systems
require.Contains(t, string(slurp), `{"0":`)
require.Contains(t, string(slurp), `["variables.input.files.0"]`)
require.Contains(t, string(slurp), `,"1":`)
require.Contains(t, string(slurp), `["variables.input.files.1"]`)
require.Contains(t, string(slurp), `}`)
}
if regexp.MustCompile(`form-data; name="[0,1]"; filename=.*`).MatchString(contentDisposition) {
require.Equal(t, `text/plain; charset=utf-8`, p.Header.Get("Content-Type"))
require.Equal(t, `hello world`, string(slurp))
require.Contains(t, []string{
`The quick brown fox jumps over the lazy dog`,
`hello world`,
}, string(slurp))
}
}
w.Write([]byte(`{}`))
Expand Down Expand Up @@ -137,25 +139,28 @@ func TestWithFiles(t *testing.T) {
require.NoError(t, err)

contentDisposition := p.Header.Get("Content-Disposition")
fmt.Printf("Part %q: %q\n", contentDisposition, slurp)

if contentDisposition == `form-data; name="operations"` {
require.Equal(t, `{"query":"{ id }","variables":{"req":{"files":[{},{}],"foo":{"bar":{}}}}}`, string(slurp))
require.EqualValues(t, `{"query":"{ id }","variables":{"req":{"files":[{},{}],"foo":{"bar":{}}}}}`, slurp)
}
if contentDisposition == `form-data; name="map"` {
require.Equal(t, `{"0":["variables.req.files.0"],"1":["variables.req.files.1"],"2":["variables.req.foo.bar"]}`, string(slurp))
}
if regexp.MustCompile(`form-data; name="0"; filename=.*`).MatchString(contentDisposition) {
require.Equal(t, `text/plain; charset=utf-8`, p.Header.Get("Content-Type"))
require.Equal(t, `The quick brown fox jumps over the lazy dog`, string(slurp))
}
if regexp.MustCompile(`form-data; name="1"; filename=.*`).MatchString(contentDisposition) {
require.Equal(t, `text/plain; charset=utf-8`, p.Header.Get("Content-Type"))
require.Equal(t, `hello world`, string(slurp))
}
if regexp.MustCompile(`form-data; name="2"; filename=.*`).MatchString(contentDisposition) {
// returns `{"0":["variables.req.files.0"],"1":["variables.req.files.1"],"2":["variables.req.foo.bar"]}`
// but the order of file inputs is unpredictable between different OS systems
require.Contains(t, string(slurp), `{"0":`)
require.Contains(t, string(slurp), `["variables.req.files.0"]`)
require.Contains(t, string(slurp), `,"1":`)
require.Contains(t, string(slurp), `["variables.req.files.1"]`)
require.Contains(t, string(slurp), `,"2":`)
require.Contains(t, string(slurp), `["variables.req.foo.bar"]`)
require.Contains(t, string(slurp), `}`)
}
if regexp.MustCompile(`form-data; name="[0,1,2]"; filename=.*`).MatchString(contentDisposition) {
require.Equal(t, `text/plain; charset=utf-8`, p.Header.Get("Content-Type"))
require.Equal(t, `La-Li-Lu-Le-Lo`, string(slurp))
require.Contains(t, []string{
`The quick brown fox jumps over the lazy dog`,
`La-Li-Lu-Le-Lo`,
`hello world`,
}, string(slurp))
}
}
w.Write([]byte(`{}`))
Expand Down Expand Up @@ -193,21 +198,28 @@ func TestWithFiles(t *testing.T) {
require.NoError(t, err)

contentDisposition := p.Header.Get("Content-Disposition")
fmt.Printf("Part %q: %q\n", contentDisposition, slurp)

if contentDisposition == `form-data; name="operations"` {
require.Equal(t, `{"query":"{ id }","variables":{"files":[{},{},{}]}}`, string(slurp))
require.EqualValues(t, `{"query":"{ id }","variables":{"files":[{},{},{}]}}`, slurp)
}
if contentDisposition == `form-data; name="map"` {
require.Equal(t, `{"0":["variables.files.0","variables.files.2"],"1":["variables.files.1"]}`, string(slurp))
}
if regexp.MustCompile(`form-data; name="0"; filename=.*`).MatchString(contentDisposition) {
require.Equal(t, `text/plain; charset=utf-8`, p.Header.Get("Content-Type"))
require.Equal(t, `The quick brown fox jumps over the lazy dog`, string(slurp))
}
if regexp.MustCompile(`form-data; name="1"; filename=.*`).MatchString(contentDisposition) {
require.EqualValues(t, `{"0":["variables.files.0","variables.files.2"],"1":["variables.files.1"]}`, slurp)
// returns `{"0":["variables.files.0","variables.files.2"],"1":["variables.files.1"]}`
// but the order of file inputs is unpredictable between different OS systems
require.Contains(t, string(slurp), `{"0":`)
require.Contains(t, string(slurp), `["variables.files.0"`)
require.Contains(t, string(slurp), `,"1":`)
require.Contains(t, string(slurp), `"variables.files.1"]`)
require.Contains(t, string(slurp), `"variables.files.2"]`)
require.NotContains(t, string(slurp), `,"2":`)
require.Contains(t, string(slurp), `}`)
}
if regexp.MustCompile(`form-data; name="[0,1]"; filename=.*`).MatchString(contentDisposition) {
require.Equal(t, `text/plain; charset=utf-8`, p.Header.Get("Content-Type"))
require.Equal(t, `hello world`, string(slurp))
require.Contains(t, []string{
`The quick brown fox jumps over the lazy dog`,
`hello world`,
}, string(slurp))
}
require.False(t, regexp.MustCompile(`form-data; name="2"; filename=.*`).MatchString(contentDisposition))
}
Expand Down
31 changes: 19 additions & 12 deletions example/fileupload/fileupload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,17 @@ func TestFileUpload(t *testing.T) {
err := gql.Post(mutation, &result, gqlclient.Var("files", []*os.File{a1TxtFile, b1TxtFile}), gqlclient.WithFiles())
require.Nil(t, err)
require.Equal(t, 1, result.MultipleUpload[0].ID)
require.Contains(t, result.MultipleUpload[0].Name, "a.txt")
require.Equal(t, "test1", result.MultipleUpload[0].Content)
require.Equal(t, "text/plain; charset=utf-8", result.MultipleUpload[0].ContentType)
require.Equal(t, 2, result.MultipleUpload[1].ID)
require.Contains(t, result.MultipleUpload[1].Name, "b.txt")
require.Equal(t, "test2", result.MultipleUpload[1].Content)
require.Equal(t, "text/plain; charset=utf-8", result.MultipleUpload[1].ContentType)
for _, mu := range result.MultipleUpload {
switch mu.Name
case "a.txt":
require.Equal(t, "test1", mu.Content)
}
if mu.Name == "b.txt" {
require.Equal(t, "test2", mu.Content)
}
require.Equal(t, "text/plain; charset=utf-8", mu.ContentType)
}
})

t.Run("valid file list upload with payload", func(t *testing.T) {
Expand Down Expand Up @@ -195,13 +199,16 @@ func TestFileUpload(t *testing.T) {
}), gqlclient.WithFiles())
require.Nil(t, err)
require.Equal(t, 1, result.MultipleUploadWithPayload[0].ID)
require.Contains(t, result.MultipleUploadWithPayload[0].Name, "a.txt")
require.Equal(t, "test1", result.MultipleUploadWithPayload[0].Content)
require.Equal(t, "text/plain; charset=utf-8", result.MultipleUploadWithPayload[0].ContentType)
require.Equal(t, 2, result.MultipleUploadWithPayload[1].ID)
require.Contains(t, result.MultipleUploadWithPayload[1].Name, "b.txt")
require.Equal(t, "test2", result.MultipleUploadWithPayload[1].Content)
require.Equal(t, "text/plain; charset=utf-8", result.MultipleUploadWithPayload[1].ContentType)
for _, mu := range result.MultipleUploadWithPayload {
if mu.Name == "a.txt" {
require.Equal(t, "test1", mu.Content)
}
if mu.Name == "b.txt" {
require.Equal(t, "test2", mu.Content)
}
require.Equal(t, "text/plain; charset=utf-8", mu.ContentType)
}
})

t.Run("valid file list upload with payload and file reuse", func(t *testing.T) {
Expand Down

0 comments on commit 78489d6

Please sign in to comment.