Skip to content

Add URL helpers #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions url/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package url

import (
"fmt"
"github.com/gruntwork-io/gruntwork-cli/errors"
"net/url"
"strings"
)

// Create a URL with the given base and path parts. This method will properly URI encode everything and handle leading
// and trailing slashes.
func FormatUrl(baseUrl string, pathParts []string, query url.Values) (string, error) {
parsedUrl, err := url.Parse(stripSlashes(baseUrl))
if err != nil {
return "", errors.WithStackTrace(err)
}

normalizedPathParts := []string{}

for _, pathPart := range pathParts {
normalizedPathPart := stripSlashes(pathPart)
normalizedPathParts = append(normalizedPathParts, normalizedPathPart)
}

if len(normalizedPathParts) > 0 {
parsedUrl.Path = fmt.Sprintf("%s/%s", stripSlashes(parsedUrl.Path), strings.Join(normalizedPathParts, "/"))
}

parsedUrl.RawQuery = mergeQuery(parsedUrl.Query(), query).Encode()

return parsedUrl.String(), nil
}

// Merge the two query params together. The new query will override the original.
func mergeQuery(originalQuery url.Values, newQuery url.Values) url.Values {
result := map[string][]string{}

for key, values := range originalQuery {
result[key] = values
}

for key, values := range newQuery {
result[key] = values
}

return result
}

// Remove all leading or trailing slashes in the given string
func stripSlashes(str string) string {
return strings.Trim(str, "/")
}
70 changes: 70 additions & 0 deletions url/url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package url

import (
"github.com/stretchr/testify/assert"
"net/url"
"testing"
)

func TestFormatUrl(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
baseUrl string
parts []string
query url.Values
expected string
}{
{"base-url-only", "http://www.example.com", parts(), query0(), "http://www.example.com"},
{"base-url-with-one-part", "http://www.example.com", parts("foo"), query0(), "http://www.example.com/foo"},
{"base-url-with-multiple-parts", "http://www.example.com", parts("foo", "bar", "baz"), query0(), "http://www.example.com/foo/bar/baz"},
{"base-url-with-trailing-slash-with-multiple-parts", "http://www.example.com/", parts("foo", "bar", "baz"), query0(), "http://www.example.com/foo/bar/baz"},
{"base-url-with-trailing-slash-with-multiple-parts-with-slashes", "http://www.example.com/", parts("/foo", "bar/", "//baz///"), query0(), "http://www.example.com/foo/bar/baz"},
{"base-url-with-path-with-multiple-parts", "http://www.example.com/a/b/c", parts("foo", "bar", "baz"), query0(), "http://www.example.com/a/b/c/foo/bar/baz"},
{"base-url-with-query-string-with-multiple-parts", "http://www.example.com/?a=b&c=d", parts("foo", "bar", "baz"), query0(), "http://www.example.com/foo/bar/baz?a=b&c=d"},
{"base-url-with-multiple-parts-encoding", "http://www.example.com", parts("foo a b c", "?#$@!"), query0(), "http://www.example.com/foo%20a%20b%20c/%3F%23$@%21"},
{"base-url-with-one-query-param", "http://www.example.com", parts(), query1("foo", "bar"), "http://www.example.com?foo=bar"},
{"base-url-with-one-query-param-encoding", "http://www.example.com", parts(), query1("foo a b c", "?#$@!"), "http://www.example.com?foo+a+b+c=%3F%23%24%40%21"},
}

for _, testCase := range testCases {
// Store a copy in scope so all the test cases don't end up running the last item in the loop
testCase := testCase

t.Run(testCase.name, func(t *testing.T) {
actual, err := FormatUrl(testCase.baseUrl, testCase.parts, testCase.query)
assert.NoError(t, err)
assert.Equal(t, testCase.expected, actual)
})
}
}

func parts(p ... string) []string {
return p
}

func query0() url.Values {
return map[string][]string{}
}

func query1(key string, value string) url.Values {
return map[string][]string{
key: {value},
}
}

func query2(key1 string, value1 string, key2 string, value2 string) url.Values {
return map[string][]string{
key1: {value1},
key2: {value2},
}
}

func query3(key1 string, value1 string, key2 string, value2 string, key3 string, value3 string) url.Values {
return map[string][]string{
key1: {value1},
key2: {value2},
key3: {value3},
}
}