Updated tests and documentation for URI.Parse and URI.Update* regarding uris without scheme

This commit is contained in:
Aliaksandr Valialkin
2017-01-09 10:50:56 +02:00
parent 82894ea1e8
commit 0a7f0a797c
2 changed files with 24 additions and 7 deletions
+9
View File
@@ -213,6 +213,11 @@ func (u *URI) SetHostBytes(host []byte) {
}
// Parse initializes URI from the given host and uri.
//
// host may be nil. In this case uri must contain fully qualified uri,
// i.e. with scheme and host. http is assumed if scheme is omitted.
//
// uri may contain e.g. RequestURI without scheme and host if host is non-empty.
func (u *URI) Parse(host, uri []byte) {
u.parse(host, uri, nil)
}
@@ -369,6 +374,8 @@ func (u *URI) LastPathSegment() []byte {
//
// * Absolute, i.e. http://foobar.com/aaa/bb?cc . In this case the original
// uri is replaced by newURI.
// * Absolute without scheme, i.e. //foobar.com/aaa/bb?cc. In this case
// the original scheme is preserved.
// * Missing host, i.e. /aaa/bb?cc . In this case only RequestURI part
// of the original uri is replaced.
// * Relative path, i.e. xx?yy=abc . In this case the original RequestURI
@@ -383,6 +390,8 @@ func (u *URI) Update(newURI string) {
//
// * Absolute, i.e. http://foobar.com/aaa/bb?cc . In this case the original
// uri is replaced by newURI.
// * Absolute without scheme, i.e. //foobar.com/aaa/bb?cc. In this case
// the original scheme is preserved.
// * Missing host, i.e. /aaa/bb?cc . In this case only RequestURI part
// of the original uri is replaced.
// * Relative path, i.e. xx?yy=abc . In this case the original RequestURI
+15 -7
View File
@@ -115,6 +115,7 @@ func TestURIUpdate(t *testing.T) {
// uri without scheme
testURIUpdate(t, "https://foo.bar/baz", "//aaa.bbb/cc?dd", "https://aaa.bbb/cc?dd")
testURIUpdate(t, "http://foo.bar/baz", "//aaa.bbb/cc?dd", "http://aaa.bbb/cc?dd")
}
func testURIUpdate(t *testing.T, base, update, result string) {
@@ -228,18 +229,25 @@ func testURIFullURI(t *testing.T, scheme, host, path, hash string, args *Args, e
}
func TestURIParseNilHost(t *testing.T) {
testURIParseScheme(t, "http://google.com/foo?bar#baz", "http")
testURIParseScheme(t, "HTtP://google.com/", "http")
testURIParseScheme(t, "://google.com/", "http")
testURIParseScheme(t, "fTP://aaa.com", "ftp")
testURIParseScheme(t, "httPS://aaa.com", "https")
testURIParseScheme(t, "http://google.com/foo?bar#baz", "http", "google.com", "/foo?bar#baz")
testURIParseScheme(t, "HTtP://google.com/", "http", "google.com", "/")
testURIParseScheme(t, "://google.com/xyz", "http", "google.com", "/xyz")
testURIParseScheme(t, "//google.com/foobar", "http", "google.com", "/foobar")
testURIParseScheme(t, "fTP://aaa.com", "ftp", "aaa.com", "/")
testURIParseScheme(t, "httPS://aaa.com", "https", "aaa.com", "/")
}
func testURIParseScheme(t *testing.T, uri, expectedScheme string) {
func testURIParseScheme(t *testing.T, uri, expectedScheme, expectedHost, expectedRequestURI string) {
var u URI
u.Parse(nil, []byte(uri))
if string(u.Scheme()) != expectedScheme {
t.Fatalf("Unexpected scheme %q. Expected %q for uri %q", u.Scheme(), expectedScheme, uri)
t.Fatalf("Unexpected scheme %q. Expecting %q for uri %q", u.Scheme(), expectedScheme, uri)
}
if string(u.Host()) != expectedHost {
t.Fatalf("Unexepcted host %q. Expecting %q for uri %q", u.Host(), expectedHost, uri)
}
if string(u.RequestURI()) != expectedRequestURI {
t.Fatalf("Unexepcted requestURI %q. Expecting %q for uri %q", u.RequestURI(), expectedRequestURI, uri)
}
}