From 34b4000be964095232df9a04e53596f61bb921c5 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 5 Dec 2017 15:06:17 -0500 Subject: [PATCH] allow discovery without trailing slash --- registry/client.go | 9 +++++---- registry/test/mock_registry.go | 4 +++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/registry/client.go b/registry/client.go index 06e54a27f8..b4cd7989f3 100644 --- a/registry/client.go +++ b/registry/client.go @@ -66,7 +66,11 @@ func NewClient(services *disco.Disco, creds auth.CredentialsSource, client *http // Discover qeuries the host, and returns the url for the registry. func (c *Client) Discover(host svchost.Hostname) *url.URL { - return c.services.DiscoverServiceURL(host, serviceID) + service := c.services.DiscoverServiceURL(host, serviceID) + if !strings.HasSuffix(service.Path, "/") { + service.Path += "/" + } + return service } // Versions queries the registry for a module, and returns the available versions. @@ -80,9 +84,6 @@ func (c *Client) Versions(module *regsrc.Module) (*response.ModuleVersions, erro if service == nil { return nil, fmt.Errorf("host %s does not provide Terraform modules", host) } - if !strings.HasSuffix(service.Path, "/") { - service.Path += "/" - } p, err := url.Parse(path.Join(module.Module(), "versions")) if err != nil { diff --git a/registry/test/mock_registry.go b/registry/test/mock_registry.go index 844f569c6d..c1fabbc25b 100644 --- a/registry/test/mock_registry.go +++ b/registry/test/mock_registry.go @@ -23,7 +23,9 @@ import ( // localhost.localdomain, and example.com to the test server. func Disco(s *httptest.Server) *disco.Disco { services := map[string]interface{}{ - "modules.v1": fmt.Sprintf("%s/v1/modules/", s.URL), + // Note that both with and without trailing slashes are supported behaviours + // TODO: add specific tests to enumerate both possibilities. + "modules.v1": fmt.Sprintf("%s/v1/modules", s.URL), } d := disco.NewDisco()