Merge pull request #225 from cloudflare/file-uri-fix

Use decoded URI path partial for file paths
This commit is contained in:
Łukasz Mierzwa
2018-01-27 15:15:48 -08:00
committed by GitHub
2 changed files with 26 additions and 10 deletions

View File

@@ -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
}

View File

@@ -73,19 +73,26 @@ 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 {
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) {
@@ -129,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))
}
@@ -153,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)
}