From 416f0a47fa23cb6e3d2b6a8f8dc868b84abd7e6b Mon Sep 17 00:00:00 2001 From: Stephen McMillen <240648+sclm@users.noreply.github.com> Date: Mon, 28 Oct 2019 16:56:09 -0400 Subject: [PATCH] Allow for Hosts to have paths with tokens This changes the config from only passing the host through to allowing for the inclusion of a path with it. --- plugin.go | 3 ++- plugin_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 plugin_test.go diff --git a/plugin.go b/plugin.go index 874fa03..879c795 100644 --- a/plugin.go +++ b/plugin.go @@ -282,7 +282,8 @@ func npmrcContentsUsernamePassword(config Config) string { /// Writes npmrc contents when using a token func npmrcContentsToken(config Config) string { registry, _ := url.Parse(config.Registry) - return fmt.Sprintf("//%s/:_authToken=%s", registry.Host, config.Token) + registry.Scheme = "" // Reset the scheme to empty. This makes it so we will get a protocol relative URL. + return fmt.Sprintf("%s:_authToken=%s", registry.String(), config.Token) } // versionCommand gets the npm version diff --git a/plugin_test.go b/plugin_test.go new file mode 100644 index 0000000..2710b93 --- /dev/null +++ b/plugin_test.go @@ -0,0 +1,24 @@ +package main + +import ( + "testing" +) + +func TestTokenRCContents(t *testing.T) { + conf := Config{ + Registry: "https://npm.someorg.com/", + Token: "token", + } + actual := npmrcContentsToken(conf) + expected := "//npm.someorg.com/:_authToken=token" + if actual != expected { + t.Errorf("Unexpected token config (Got: %s, Expected: %s)", actual, expected) + } + + conf.Registry = "https://npm.someorg.com/with/path/" + actual = npmrcContentsToken(conf) + expected = "//npm.someorg.com/with/path/:_authToken=token" + if actual != expected { + t.Errorf("Unexpected token config (Got: %s, Expected: %s)", actual, expected) + } +}