diff --git a/client.go b/client.go index 7915447..13d2d43 100644 --- a/client.go +++ b/client.go @@ -399,7 +399,7 @@ func readResponse(resp *ftp.Response) (io.ReadCloser, error) { // // See https://github.com/moovfinancial/paygate/issues/494 if n == 0 && err == nil { - return nil, nil + return io.NopCloser(&buf), nil } if err != nil { return nil, fmt.Errorf("n=%d error=%v", n, err) diff --git a/client_test.go b/client_test.go index a1614bf..5ed6039 100644 --- a/client_test.go +++ b/client_test.go @@ -6,8 +6,10 @@ package go_ftp_test import ( "bytes" + "fmt" "io" "io/fs" + "math/rand" "os" "strings" "testing" @@ -96,6 +98,41 @@ func TestClient(t *testing.T) { require.ElementsMatch(t, filenames, []string{"/archive/old.txt", "/archive/empty2.txt"}) }) + t.Run("list and read", func(t *testing.T) { + filenames, err := client.ListFiles("/with-empty") + require.NoError(t, err) + + // randomize filename order + rand.Shuffle(len(filenames), func(i, j int) { + filenames[i], filenames[j] = filenames[j], filenames[i] + }) + require.ElementsMatch(t, filenames, []string{ + "/with-empty/EMPTY1.txt", "/with-empty/empty_file2.txt", + "/with-empty/data.txt", "/with-empty/data2.txt", + }) + + // read each file and get back expected contents + var contents []string + for i := range filenames { + var file *go_ftp.File + if i/2 == 0 { + file, err = client.Open(filenames[i]) + } else { + file, err = client.Reader(filenames[i]) + } + require.NoError(t, err, fmt.Sprintf("filenames[%d]", i)) + require.NotNil(t, file, fmt.Sprintf("filenames[%d]", i)) + require.NotNil(t, file.Contents, fmt.Sprintf("filenames[%d]", i)) + + bs, err := io.ReadAll(file.Contents) + require.NoError(t, err) + + contents = append(contents, string(bs)) + } + + require.ElementsMatch(t, contents, []string{"", "", "also data\n", "has data\n"}) + }) + t.Run("walk", func(t *testing.T) { var found []string err := client.Walk(".", func(path string, d fs.DirEntry, err error) error { @@ -106,6 +143,8 @@ func TestClient(t *testing.T) { require.ElementsMatch(t, found, []string{ "first.txt", "second.txt", "empty.txt", "archive/old.txt", "archive/empty2.txt", + "with-empty/EMPTY1.txt", "with-empty/empty_file2.txt", + "with-empty/data.txt", "with-empty/data2.txt", }) }) diff --git a/testdata/ftp-server/with-empty/EMPTY1.txt b/testdata/ftp-server/with-empty/EMPTY1.txt new file mode 100644 index 0000000..e69de29 diff --git a/testdata/ftp-server/with-empty/data.txt b/testdata/ftp-server/with-empty/data.txt new file mode 100644 index 0000000..d0e0b0b --- /dev/null +++ b/testdata/ftp-server/with-empty/data.txt @@ -0,0 +1 @@ +has data diff --git a/testdata/ftp-server/with-empty/data2.txt b/testdata/ftp-server/with-empty/data2.txt new file mode 100644 index 0000000..0a064b8 --- /dev/null +++ b/testdata/ftp-server/with-empty/data2.txt @@ -0,0 +1 @@ +also data diff --git a/testdata/ftp-server/with-empty/empty_file2.txt b/testdata/ftp-server/with-empty/empty_file2.txt new file mode 100644 index 0000000..e69de29