]> Cypherpunks repositories - gostls13.git/commitdiff
net/url: add examples to URL methods
authorGuilherme Garnier <guilherme.garnier@gmail.com>
Wed, 30 Aug 2017 23:06:23 +0000 (20:06 -0300)
committerJoe Tsai <thebrokentoaster@gmail.com>
Fri, 1 Sep 2017 23:45:29 +0000 (23:45 +0000)
Change-Id: Ia557771272a8ef3602f0281b8c89d2cfe146c8b3
Reviewed-on: https://go-review.googlesource.com/60470
Reviewed-by: Adrian Hesketh <adrianhesketh@hushmail.com>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/net/url/example_test.go

index d352c55b6fa14246867667c3c9e6630c41b51d91..d8eb6dcd20c7b15d5393b1a8fd03fb1a6e9c92b3 100644 (file)
@@ -81,6 +81,16 @@ func ExampleParseQuery() {
        // {"x":["1"], "y":["2", "3"], "z":[""]}
 }
 
+func ExampleURL_EscapedPath() {
+       u, err := url.Parse("http://example.com/path with spaces")
+       if err != nil {
+               log.Fatal(err)
+       }
+       fmt.Println(u.EscapedPath())
+       // Output:
+       // /path%20with%20spaces
+}
+
 func ExampleURL_Hostname() {
        u, err := url.Parse("https://example.org:8000/path")
        if err != nil {
@@ -97,6 +107,104 @@ func ExampleURL_Hostname() {
        // 2001:0db8:85a3:0000:0000:8a2e:0370:7334
 }
 
+func ExampleURL_IsAbs() {
+       u := url.URL{Host: "example.com", Path: "foo"}
+       fmt.Println(u.IsAbs())
+       u.Scheme = "http"
+       fmt.Println(u.IsAbs())
+       // Output:
+       // false
+       // true
+}
+
+func ExampleURL_MarshalBinary() {
+       u, _ := url.Parse("https://example.org")
+       b, err := u.MarshalBinary()
+       if err != nil {
+               log.Fatal(err)
+       }
+       fmt.Printf("%s\n", b)
+       // Output:
+       // https://example.org
+}
+
+func ExampleURL_Parse() {
+       u, err := url.Parse("https://example.org")
+       if err != nil {
+               log.Fatal(err)
+       }
+       rel, err := u.Parse("/foo")
+       if err != nil {
+               log.Fatal(err)
+       }
+       fmt.Println(rel)
+       _, err = u.Parse(":foo")
+       if _, ok := err.(*url.Error); !ok {
+               log.Fatal(err)
+       }
+       // Output:
+       // https://example.org/foo
+}
+
+func ExampleURL_Port() {
+       u, err := url.Parse("https://example.org")
+       if err != nil {
+               log.Fatal(err)
+       }
+       fmt.Println(u.Port())
+       u, err = url.Parse("https://example.org:8080")
+       if err != nil {
+               log.Fatal(err)
+       }
+       fmt.Println(u.Port())
+       // Output:
+       //
+       // 8080
+}
+
+func ExampleURL_Query() {
+       u, err := url.Parse("https://example.org/?a=1&a=2&b=&=3&&&&")
+       if err != nil {
+               log.Fatal(err)
+       }
+       q := u.Query()
+       fmt.Println(q["a"])
+       fmt.Println(q.Get("b"))
+       fmt.Println(q.Get(""))
+       // Output:
+       // [1 2]
+       //
+       // 3
+}
+
+func ExampleURL_String() {
+       u := &url.URL{
+               Scheme:   "https",
+               User:     url.UserPassword("me", "pass"),
+               Host:     "example.com",
+               Path:     "foo/bar",
+               RawQuery: "x=1&y=2",
+               Fragment: "anchor",
+       }
+       fmt.Println(u.String())
+       u.Opaque = "opaque"
+       fmt.Println(u.String())
+       // Output:
+       // https://me:pass@example.com/foo/bar?x=1&y=2#anchor
+       // https:opaque?x=1&y=2#anchor
+}
+
+func ExampleURL_UnmarshalBinary() {
+       u := &url.URL{}
+       err := u.UnmarshalBinary([]byte("https://example.org/foo"))
+       if err != nil {
+               log.Fatal(err)
+       }
+       fmt.Printf("%s\n", u)
+       // Output:
+       // https://example.org/foo
+}
+
 func ExampleURL_RequestURI() {
        u, err := url.Parse("https://example.org/path?foo=bar")
        if err != nil {