-
Notifications
You must be signed in to change notification settings - Fork 715
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf0dd92
commit 71460bd
Showing
9 changed files
with
112 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package sanitize | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
// URL returns a function that sanitizes a URL string. It lets underspecified | ||
// strings to be converted to usable URLs via some default arguments. | ||
func URL(scheme string, port int, path string) func(string) string { | ||
if scheme == "" { | ||
scheme = "http://" | ||
} | ||
return func(s string) string { | ||
if s == "" { | ||
return s // can't do much here | ||
} | ||
if !strings.HasPrefix(s, "http") { | ||
s = scheme + s | ||
} | ||
u, err := url.Parse(s) | ||
if err != nil { | ||
log.Printf("%q: %v", s, err) | ||
return s // oh well | ||
} | ||
if port > 0 { | ||
if _, _, err = net.SplitHostPort(u.Host); err != nil { | ||
u.Host += fmt.Sprintf(":%d", port) | ||
} | ||
} | ||
if path != "" && u.Path != path { | ||
u.Path = path | ||
} | ||
return u.String() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package sanitize_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/weaveworks/scope/common/sanitize" | ||
) | ||
|
||
func TestSanitizeURL(t *testing.T) { | ||
for _, input := range []struct { | ||
scheme string | ||
port int | ||
path string | ||
input string | ||
want string | ||
}{ | ||
{"", 0, "", "", ""}, | ||
{"", 0, "", "foo", "http://foo"}, | ||
{"", 80, "", "foo", "http://foo:80"}, | ||
{"", 0, "some/path", "foo", "http://foo/some/path"}, | ||
{"", 0, "/some/path", "foo", "http://foo/some/path"}, | ||
{"https://", 0, "", "foo", "https://foo"}, | ||
{"https://", 80, "", "foo", "https://foo:80"}, | ||
{"https://", 0, "some/path", "foo", "https://foo/some/path"}, | ||
{"https://", 0, "", "http://foo", "http://foo"}, // specified scheme beats default... | ||
{"", 9999, "", "foo:80", "http://foo:80"}, // specified port beats default... | ||
{"", 0, "/bar", "foo/baz", "http://foo/bar"}, // ...but default path beats specified! | ||
} { | ||
if want, have := input.want, sanitize.URL(input.scheme, input.port, input.path)(input.input); want != have { | ||
t.Errorf("sanitize.URL(%q, %d, %q)(%q): want %q, have %q", input.scheme, input.port, input.path, input.input, want, have) | ||
continue | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters