From aeccbce0fe78665cd0d7d55c42b4df142623fe6b Mon Sep 17 00:00:00 2001 From: hantonelli Date: Mon, 6 May 2019 11:26:01 +0100 Subject: [PATCH] Update test include an example that uses io.Read interface directly --- example/fileupload/fileupload_test.go | 20 ++++++++++++++++---- handler/graphql_test.go | 24 ++++++++++++++++-------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/example/fileupload/fileupload_test.go b/example/fileupload/fileupload_test.go index 44c8a809dd1..a23e2f2e126 100644 --- a/example/fileupload/fileupload_test.go +++ b/example/fileupload/fileupload_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "io" "io/ioutil" "mime/multipart" "net/http" @@ -216,13 +217,24 @@ func TestFileUpload(t *testing.T) { require.NotNil(t, req[i].File) require.NotNil(t, req[i].File.File) ids = append(ids, req[i].ID) - content, err := ioutil.ReadAll(req[i].File.File) - require.Nil(t, err) - contents = append(contents, string(content)) + + var got []byte + buf := make([]byte, 2) + for { + n, err := req[i].File.File.Read(buf) + got = append(got, buf[:n]...) + if err != nil { + if err == io.EOF { + break + } + require.Fail(t, "unexpected error while reading", err.Error()) + } + } + contents = append(contents, string(got)) resp = append(resp, model.File{ ID: i + 1, Name: req[i].File.Filename, - Content: string(content), + Content: string(got), }) } require.ElementsMatch(t, []int{1, 2}, ids) diff --git a/handler/graphql_test.go b/handler/graphql_test.go index 571e9705f0a..70946ab9491 100644 --- a/handler/graphql_test.go +++ b/handler/graphql_test.go @@ -682,13 +682,17 @@ func TestBytesRead(t *testing.T) { got := make([]byte, 0, 11) buf := make([]byte, 1) for { - r, e := r.Read(buf) - if r == 0 { - if e != nil || e == io.EOF { + n, err := r.Read(buf) + if n < 0 { + require.Fail(t, "unexpected bytes read size") + } + got = append(got, buf[:n]...) + if err != nil { + if err == io.EOF { break } + require.Fail(t, "unexpected error while reading", err.Error()) } - got = append(got, buf...) } require.Equal(t, "0123456789", string(got)) }) @@ -702,13 +706,17 @@ func TestBytesRead(t *testing.T) { got := make([]byte, 0, 11) buf := make([]byte, 1) for { - r, e := r.Read(buf) - if r == 0 { - if e != nil || e == io.EOF { + n, err := r.Read(buf) + if n < 0 { + require.Fail(t, "unexpected bytes read size") + } + got = append(got, buf[:n]...) + if err != nil { + if err == io.EOF { break } + require.Fail(t, "unexpected error while reading", err.Error()) } - got = append(got, buf...) } require.Equal(t, "0193456789", string(got)) })