Andrew Gerrand | 3e804f9 | 2012-02-18 11:48:33 +1100 | [diff] [blame] | 1 | // Copyright 2012 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package url_test |
| 6 | |
| 7 | import ( |
Russ Cox | 59dae58 | 2016-10-17 22:50:44 -0400 | [diff] [blame] | 8 | "encoding/json" |
Andrew Gerrand | 3e804f9 | 2012-02-18 11:48:33 +1100 | [diff] [blame] | 9 | "fmt" |
| 10 | "log" |
| 11 | "net/url" |
Brad Fitzpatrick | 38ea0ae | 2014-11-12 14:27:27 -0800 | [diff] [blame] | 12 | "strings" |
Andrew Gerrand | 3e804f9 | 2012-02-18 11:48:33 +1100 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | func ExampleValues() { |
| 16 | v := url.Values{} |
| 17 | v.Set("name", "Ava") |
| 18 | v.Add("friend", "Jess") |
| 19 | v.Add("friend", "Sarah") |
| 20 | v.Add("friend", "Zoe") |
| 21 | // v.Encode() == "name=Ava&friend=Jess&friend=Sarah&friend=Zoe" |
| 22 | fmt.Println(v.Get("name")) |
| 23 | fmt.Println(v.Get("friend")) |
| 24 | fmt.Println(v["friend"]) |
| 25 | // Output: |
| 26 | // Ava |
| 27 | // Jess |
| 28 | // [Jess Sarah Zoe] |
| 29 | } |
| 30 | |
| 31 | func ExampleURL() { |
| 32 | u, err := url.Parse("http://bing.com/search?q=dotnet") |
| 33 | if err != nil { |
| 34 | log.Fatal(err) |
| 35 | } |
| 36 | u.Scheme = "https" |
| 37 | u.Host = "google.com" |
| 38 | q := u.Query() |
| 39 | q.Set("q", "golang") |
| 40 | u.RawQuery = q.Encode() |
| 41 | fmt.Println(u) |
| 42 | // Output: https://google.com/search?q=golang |
| 43 | } |
Brad Fitzpatrick | 38ea0ae | 2014-11-12 14:27:27 -0800 | [diff] [blame] | 44 | |
Russ Cox | 874a605 | 2015-06-19 17:04:56 -0400 | [diff] [blame] | 45 | func ExampleURL_roundtrip() { |
| 46 | // Parse + String preserve the original encoding. |
| 47 | u, err := url.Parse("https://example.com/foo%2fbar") |
| 48 | if err != nil { |
| 49 | log.Fatal(err) |
| 50 | } |
| 51 | fmt.Println(u.Path) |
| 52 | fmt.Println(u.RawPath) |
| 53 | fmt.Println(u.String()) |
| 54 | // Output: |
| 55 | // /foo/bar |
| 56 | // /foo%2fbar |
| 57 | // https://example.com/foo%2fbar |
| 58 | } |
| 59 | |
Carlos C | 6094b88 | 2015-07-09 15:08:39 +0200 | [diff] [blame] | 60 | func ExampleURL_ResolveReference() { |
| 61 | u, err := url.Parse("../../..//search?q=dotnet") |
| 62 | if err != nil { |
| 63 | log.Fatal(err) |
| 64 | } |
| 65 | base, err := url.Parse("http://example.com/directory/") |
| 66 | if err != nil { |
| 67 | log.Fatal(err) |
| 68 | } |
| 69 | fmt.Println(base.ResolveReference(u)) |
| 70 | // Output: |
| 71 | // http://example.com/search?q=dotnet |
| 72 | } |
Russ Cox | 59dae58 | 2016-10-17 22:50:44 -0400 | [diff] [blame] | 73 | |
| 74 | func ExampleParseQuery() { |
| 75 | m, err := url.ParseQuery(`x=1&y=2&y=3;z`) |
| 76 | if err != nil { |
| 77 | log.Fatal(err) |
| 78 | } |
| 79 | fmt.Println(toJSON(m)) |
| 80 | // Output: |
| 81 | // {"x":["1"], "y":["2", "3"], "z":[""]} |
| 82 | } |
| 83 | |
Guilherme Garnier | a6a92b1 | 2017-08-30 20:06:23 -0300 | [diff] [blame] | 84 | func ExampleURL_EscapedPath() { |
| 85 | u, err := url.Parse("http://example.com/path with spaces") |
| 86 | if err != nil { |
| 87 | log.Fatal(err) |
| 88 | } |
| 89 | fmt.Println(u.EscapedPath()) |
| 90 | // Output: |
| 91 | // /path%20with%20spaces |
| 92 | } |
| 93 | |
Kevin Burke | b3b9b5e | 2017-05-07 23:19:31 -0700 | [diff] [blame] | 94 | func ExampleURL_Hostname() { |
| 95 | u, err := url.Parse("https://example.org:8000/path") |
| 96 | if err != nil { |
| 97 | log.Fatal(err) |
| 98 | } |
| 99 | fmt.Println(u.Hostname()) |
| 100 | u, err = url.Parse("https://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:17000") |
| 101 | if err != nil { |
| 102 | log.Fatal(err) |
| 103 | } |
| 104 | fmt.Println(u.Hostname()) |
| 105 | // Output: |
| 106 | // example.org |
| 107 | // 2001:0db8:85a3:0000:0000:8a2e:0370:7334 |
| 108 | } |
| 109 | |
Guilherme Garnier | a6a92b1 | 2017-08-30 20:06:23 -0300 | [diff] [blame] | 110 | func ExampleURL_IsAbs() { |
| 111 | u := url.URL{Host: "example.com", Path: "foo"} |
| 112 | fmt.Println(u.IsAbs()) |
| 113 | u.Scheme = "http" |
| 114 | fmt.Println(u.IsAbs()) |
| 115 | // Output: |
| 116 | // false |
| 117 | // true |
| 118 | } |
| 119 | |
| 120 | func ExampleURL_MarshalBinary() { |
| 121 | u, _ := url.Parse("https://example.org") |
| 122 | b, err := u.MarshalBinary() |
| 123 | if err != nil { |
| 124 | log.Fatal(err) |
| 125 | } |
| 126 | fmt.Printf("%s\n", b) |
| 127 | // Output: |
| 128 | // https://example.org |
| 129 | } |
| 130 | |
| 131 | func ExampleURL_Parse() { |
| 132 | u, err := url.Parse("https://example.org") |
| 133 | if err != nil { |
| 134 | log.Fatal(err) |
| 135 | } |
| 136 | rel, err := u.Parse("/foo") |
| 137 | if err != nil { |
| 138 | log.Fatal(err) |
| 139 | } |
| 140 | fmt.Println(rel) |
| 141 | _, err = u.Parse(":foo") |
| 142 | if _, ok := err.(*url.Error); !ok { |
| 143 | log.Fatal(err) |
| 144 | } |
| 145 | // Output: |
| 146 | // https://example.org/foo |
| 147 | } |
| 148 | |
| 149 | func ExampleURL_Port() { |
| 150 | u, err := url.Parse("https://example.org") |
| 151 | if err != nil { |
| 152 | log.Fatal(err) |
| 153 | } |
| 154 | fmt.Println(u.Port()) |
| 155 | u, err = url.Parse("https://example.org:8080") |
| 156 | if err != nil { |
| 157 | log.Fatal(err) |
| 158 | } |
| 159 | fmt.Println(u.Port()) |
| 160 | // Output: |
| 161 | // |
| 162 | // 8080 |
| 163 | } |
| 164 | |
| 165 | func ExampleURL_Query() { |
| 166 | u, err := url.Parse("https://example.org/?a=1&a=2&b=&=3&&&&") |
| 167 | if err != nil { |
| 168 | log.Fatal(err) |
| 169 | } |
| 170 | q := u.Query() |
| 171 | fmt.Println(q["a"]) |
| 172 | fmt.Println(q.Get("b")) |
| 173 | fmt.Println(q.Get("")) |
| 174 | // Output: |
| 175 | // [1 2] |
| 176 | // |
| 177 | // 3 |
| 178 | } |
| 179 | |
| 180 | func ExampleURL_String() { |
| 181 | u := &url.URL{ |
| 182 | Scheme: "https", |
| 183 | User: url.UserPassword("me", "pass"), |
| 184 | Host: "example.com", |
| 185 | Path: "foo/bar", |
| 186 | RawQuery: "x=1&y=2", |
| 187 | Fragment: "anchor", |
| 188 | } |
| 189 | fmt.Println(u.String()) |
| 190 | u.Opaque = "opaque" |
| 191 | fmt.Println(u.String()) |
| 192 | // Output: |
| 193 | // https://me:pass@example.com/foo/bar?x=1&y=2#anchor |
| 194 | // https:opaque?x=1&y=2#anchor |
| 195 | } |
| 196 | |
| 197 | func ExampleURL_UnmarshalBinary() { |
| 198 | u := &url.URL{} |
| 199 | err := u.UnmarshalBinary([]byte("https://example.org/foo")) |
| 200 | if err != nil { |
| 201 | log.Fatal(err) |
| 202 | } |
| 203 | fmt.Printf("%s\n", u) |
| 204 | // Output: |
| 205 | // https://example.org/foo |
| 206 | } |
| 207 | |
Kevin Burke | b3b9b5e | 2017-05-07 23:19:31 -0700 | [diff] [blame] | 208 | func ExampleURL_RequestURI() { |
| 209 | u, err := url.Parse("https://example.org/path?foo=bar") |
| 210 | if err != nil { |
| 211 | log.Fatal(err) |
| 212 | } |
| 213 | fmt.Println(u.RequestURI()) |
| 214 | // Output: /path?foo=bar |
| 215 | } |
| 216 | |
Russ Cox | 59dae58 | 2016-10-17 22:50:44 -0400 | [diff] [blame] | 217 | func toJSON(m interface{}) string { |
| 218 | js, err := json.Marshal(m) |
| 219 | if err != nil { |
| 220 | log.Fatal(err) |
| 221 | } |
Brad Fitzpatrick | da0d1a4 | 2018-09-26 21:10:21 +0000 | [diff] [blame^] | 222 | return strings.ReplaceAll(string(js), ",", ", ") |
Russ Cox | 59dae58 | 2016-10-17 22:50:44 -0400 | [diff] [blame] | 223 | } |