Skip to content
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
11 changes: 11 additions & 0 deletions urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,14 @@ func CreateURL(origin, path string, params map[string]string) *url.URL {
u.RawQuery = query.Encode()
return u
}

// GetDomain extracts the domain name from a given url s.
//
// If s is not a valid url, the empty string is returned.
func GetDomain(s string) string {
u, uerr := url.Parse(s)
if uerr != nil {
return ""
}
return u.Host
}
8 changes: 8 additions & 0 deletions urls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ func TestCreateURL(t *testing.T) {
u := CreateURL(orig, "/hello", params)
must.Eq(t, "http://example.org:8000/hello?key=abc123&offset=3", u.String())
}

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

orig := "http://stage.example.org/foo/bar"
result := GetDomain(orig)
must.Eq(t, "stage.example.org", result)
}
Loading