use path.Join() for building path
Signed-off-by: MATSUMOTO TAKEAKI <takeaki.matsumoto@linecorp.com>
This commit is contained in:
parent
a1cfd267c8
commit
a3eb956464
2 changed files with 50 additions and 6 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
pathutil "path"
|
||||
|
||||
storagedriver "github.com/docker/distribution/registry/storage/driver"
|
||||
storagemiddleware "github.com/docker/distribution/registry/storage/driver/middleware"
|
||||
|
@ -11,9 +12,9 @@ import (
|
|||
|
||||
type redirectStorageMiddleware struct {
|
||||
storagedriver.StorageDriver
|
||||
scheme string
|
||||
host string
|
||||
path string
|
||||
scheme string
|
||||
host string
|
||||
basePath string
|
||||
}
|
||||
|
||||
var _ storagedriver.StorageDriver = &redirectStorageMiddleware{}
|
||||
|
@ -38,12 +39,12 @@ func newRedirectStorageMiddleware(sd storagedriver.StorageDriver, options map[st
|
|||
return nil, fmt.Errorf("no host specified for redirect baseurl")
|
||||
}
|
||||
|
||||
return &redirectStorageMiddleware{StorageDriver: sd, scheme: u.Scheme, host: u.Host, path: u.Path}, nil
|
||||
return &redirectStorageMiddleware{StorageDriver: sd, scheme: u.Scheme, host: u.Host, basePath: u.Path}, nil
|
||||
}
|
||||
|
||||
func (r *redirectStorageMiddleware) URLFor(ctx context.Context, path string, options map[string]interface{}) (string, error) {
|
||||
if r.path != "" {
|
||||
path = r.path + path
|
||||
if r.basePath != "" {
|
||||
path = pathutil.Join(r.basePath, path)
|
||||
}
|
||||
u := &url.URL{Scheme: r.scheme, Host: r.host, Path: path}
|
||||
return u.String(), nil
|
||||
|
|
|
@ -57,3 +57,46 @@ func (s *MiddlewareSuite) TestHTTP(c *check.C) {
|
|||
c.Assert(err, check.Equals, nil)
|
||||
c.Assert(url, check.Equals, "http://example.com/morty/data")
|
||||
}
|
||||
|
||||
func (s *MiddlewareSuite) TestPath(c *check.C) {
|
||||
// basePath: end with no slash
|
||||
options := make(map[string]interface{})
|
||||
options["baseurl"] = "https://example.com/path"
|
||||
middleware, err := newRedirectStorageMiddleware(nil, options)
|
||||
c.Assert(err, check.Equals, nil)
|
||||
|
||||
m, ok := middleware.(*redirectStorageMiddleware)
|
||||
c.Assert(ok, check.Equals, true)
|
||||
c.Assert(m.scheme, check.Equals, "https")
|
||||
c.Assert(m.host, check.Equals, "example.com")
|
||||
c.Assert(m.basePath, check.Equals, "/path")
|
||||
|
||||
// call URLFor() with no leading slash
|
||||
url, err := middleware.URLFor(context.TODO(), "morty/data", nil)
|
||||
c.Assert(err, check.Equals, nil)
|
||||
c.Assert(url, check.Equals, "https://example.com/path/morty/data")
|
||||
// call URLFor() with leading slash
|
||||
url, err = middleware.URLFor(context.TODO(), "/morty/data", nil)
|
||||
c.Assert(err, check.Equals, nil)
|
||||
c.Assert(url, check.Equals, "https://example.com/path/morty/data")
|
||||
|
||||
// basePath: end with slash
|
||||
options["baseurl"] = "https://example.com/path/"
|
||||
middleware, err = newRedirectStorageMiddleware(nil, options)
|
||||
c.Assert(err, check.Equals, nil)
|
||||
|
||||
m, ok = middleware.(*redirectStorageMiddleware)
|
||||
c.Assert(ok, check.Equals, true)
|
||||
c.Assert(m.scheme, check.Equals, "https")
|
||||
c.Assert(m.host, check.Equals, "example.com")
|
||||
c.Assert(m.basePath, check.Equals, "/path/")
|
||||
|
||||
// call URLFor() with no leading slash
|
||||
url, err = middleware.URLFor(context.TODO(), "morty/data", nil)
|
||||
c.Assert(err, check.Equals, nil)
|
||||
c.Assert(url, check.Equals, "https://example.com/path/morty/data")
|
||||
// call URLFor() with leading slash
|
||||
url, err = middleware.URLFor(context.TODO(), "/morty/data", nil)
|
||||
c.Assert(err, check.Equals, nil)
|
||||
c.Assert(url, check.Equals, "https://example.com/path/morty/data")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue