From 21ce7c2fc8c3b4a8488118fc876821ac78c850ed Mon Sep 17 00:00:00 2001 From: Alex Sonneveld Date: Fri, 15 Oct 2021 14:13:57 +1100 Subject: [PATCH] Fix strict withFiles option test for race conditions 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 --- client/withfilesoption_test.go | 90 +++++++++++++++------------ example/fileupload/fileupload_test.go | 30 +++++---- 2 files changed, 69 insertions(+), 51 deletions(-) diff --git a/client/withfilesoption_test.go b/client/withfilesoption_test.go index f3528936ff4..48b9e09d0c4 100644 --- a/client/withfilesoption_test.go +++ b/client/withfilesoption_test.go @@ -1,7 +1,6 @@ package client_test import ( - "fmt" "io" "io/ioutil" "mime" @@ -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(`{}`)) @@ -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(`{}`)) @@ -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(`{}`)) @@ -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)) } diff --git a/example/fileupload/fileupload_test.go b/example/fileupload/fileupload_test.go index 158825edfbf..d91f90e227e 100644 --- a/example/fileupload/fileupload_test.go +++ b/example/fileupload/fileupload_test.go @@ -143,13 +143,16 @@ 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 { + 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", func(t *testing.T) { @@ -195,13 +198,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) {