From b8cf0a83f4135b686259f72e5a2630ae3c0883ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Fri, 26 Jan 2018 23:01:46 -0800 Subject: [PATCH 1/2] Correctly handle all types of file:// values For absolute paths we correctly return full decoded path, but for relative paths we can't simply strip file:// prefix as it might leave query args in place. Build absolute path by joining working dir + hostname part + path part. Add an extra test case for coverage. --- internal/uri/file.go | 9 +++++---- internal/uri/uri_test.go | 5 +++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/internal/uri/file.go b/internal/uri/file.go index aef474773..8d9602cf2 100644 --- a/internal/uri/file.go +++ b/internal/uri/file.go @@ -32,16 +32,17 @@ func (r *FileURIReader) pathFromURI(uri string) (string, error) { return "", err } - // if we have a file URI with relative path we need to expand it into an - // absolute path, url.Parse doesn't support relative file paths + // if we a file URI with an absolute path then return it if strings.HasPrefix(uri, "file:///") { return u.Path, nil } - wd, err := os.Getwd() + // if we have a file URI with relative path we need to expand it into an + // absolute path, url.Parse doesn't support relative file paths + cwd, err := os.Getwd() if err != nil { return "", err } - absolutePath := path.Join(wd, strings.TrimPrefix(uri, "file://")) + absolutePath := path.Join(cwd, u.Host, u.Path) return absolutePath, nil } diff --git a/internal/uri/uri_test.go b/internal/uri/uri_test.go index 0e8dfea7e..5f312551d 100644 --- a/internal/uri/uri_test.go +++ b/internal/uri/uri_test.go @@ -73,6 +73,11 @@ var fileTransportTests = []fileTransportTest{ size: getFileSize("uri.go"), failed: true, }, + fileTransportTest{ + uri: "file://../uri/uri.go", + size: getFileSize("uri.go"), + failed: true, + }, } func readAll(source io.ReadCloser) int64 { From 782b633f8c6ba5e49e57daa9e7b0189a4afc8a09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Sat, 27 Jan 2018 09:36:59 -0800 Subject: [PATCH 2/2] Fix error checking in file read tests --- internal/uri/uri_test.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/internal/uri/uri_test.go b/internal/uri/uri_test.go index 5f312551d..667c1547f 100644 --- a/internal/uri/uri_test.go +++ b/internal/uri/uri_test.go @@ -80,17 +80,19 @@ var fileTransportTests = []fileTransportTest{ }, } -func readAll(source io.ReadCloser) int64 { +func readAll(source io.ReadCloser) (int64, error) { var readSize int64 b := make([]byte, 512) for { got, err := source.Read(b) readSize += int64(got) - if err == io.EOF { - break + if err != nil { + if err == io.EOF { + return readSize, nil + } + return readSize, err } } - return readSize } func TestHTTPReader(t *testing.T) { @@ -134,9 +136,13 @@ func TestHTTPReader(t *testing.T) { } continue } - got := readAll(source) + got, err := readAll(source) source.Close() + if err != nil { + t.Errorf("[%v] Read() failed: %s", testCase, err) + } + if got != int64(len(responseBody)+1) { t.Errorf("[%v] Wrong respone size, got %d, expected %d", testCase, got, len(responseBody)) } @@ -158,9 +164,13 @@ func TestFileReader(t *testing.T) { } continue } - got := readAll(source) + got, err := readAll(source) source.Close() + if err != nil { + t.Errorf("[%v] Read() failed: %s", testCase, err) + } + if got != testCase.size { t.Errorf("[%v] Wrong respone size, got %d, expected %d", testCase, got, testCase.size) }