Skip to content

Commit

Permalink
Update test include an example that uses io.Read interface directly
Browse files Browse the repository at this point in the history
  • Loading branch information
hantonelli committed May 6, 2019
1 parent d9dca64 commit aeccbce
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
20 changes: 16 additions & 4 deletions example/fileupload/fileupload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 16 additions & 8 deletions handler/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand All @@ -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))
})
Expand Down

0 comments on commit aeccbce

Please sign in to comment.