Skip to content

Commit

Permalink
Correctly append slash to HTTP store paths as per #112
Browse files Browse the repository at this point in the history
  • Loading branch information
folbricht committed Aug 3, 2019
1 parent d9ad4b1 commit 2c00bf2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
5 changes: 2 additions & 3 deletions remotehttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ func NewRemoteHTTPStoreBase(location *url.URL, opt StoreOptions) (*RemoteHTTPBas
return nil, fmt.Errorf("unsupported scheme %s, expected http or https", location.Scheme)
}
// Make sure we have a trailing / on the path
u := *location
if !strings.HasSuffix(u.Path, "/") {
u.Path = u.Path + "/"
if !strings.HasSuffix(location.Path, "/") {
location.Path = location.Path + "/"
}

// Build a TLS client config
Expand Down
42 changes: 42 additions & 0 deletions remotehttp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package desync

import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
)

func TestHTTPStoreURL(t *testing.T) {
var requestURI string
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestURI = r.RequestURI
}))
defer ts.Close()
u, _ := url.Parse(ts.URL)

chunkID := ChunkID{1, 2, 3, 4}
tests := map[string]struct {
storePath string
serverPath string
}{
"no path": {"", "/0102/0102030400000000000000000000000000000000000000000000000000000000.cacnk"},
"slash only": {"/", "/0102/0102030400000000000000000000000000000000000000000000000000000000.cacnk"},
"no trailing slash": {"/path", "/path/0102/0102030400000000000000000000000000000000000000000000000000000000.cacnk"},
"with trailing slash": {"/path/", "/path/0102/0102030400000000000000000000000000000000000000000000000000000000.cacnk"},
"long path": {"/path1/path2", "/path1/path2/0102/0102030400000000000000000000000000000000000000000000000000000000.cacnk"},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
u.Path = test.storePath
s, err := NewRemoteHTTPStore(u, StoreOptions{})
if err != nil {
t.Fatal(err)
}
s.GetChunk(chunkID)
if requestURI != test.serverPath {
t.Fatalf("got request uri '%s', want '%s'", requestURI, test.serverPath)
}
})
}
}

0 comments on commit 2c00bf2

Please sign in to comment.