Debian Codegraph @

src/net/url/example_test.go

raw
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 (
8 "encoding/json"
9 "fmt"
10 "log"
11 "net/url"
12 "strings"
13 )
14
15 func ExamplePathEscape() {
16 path := url.PathEscape("my/cool+blog&about,stuff")
17 fmt.Println(path)
18
19 // Output:
20 // my%2Fcool+blog&about%2Cstuff
21 }
22
23 func ExamplePathUnescape() {
24 escapedPath := "my%2Fcool+blog&about%2Cstuff"
25 path, err := url.PathUnescape(escapedPath)
26 if err != nil {
27 log.Fatal(err)
28 }
29 fmt.Println(path)
30
31 // Output:
32 // my/cool+blog&about,stuff
33 }
34
35 func ExampleQueryEscape() {
36 query := url.QueryEscape("my/cool+blog&about,stuff")
37 fmt.Println(query)
38
39 // Output:
40 // my%2Fcool%2Bblog%26about%2Cstuff
41 }
42
43 func ExampleQueryUnescape() {
44 escapedQuery := "my%2Fcool%2Bblog%26about%2Cstuff"
45 query, err := url.QueryUnescape(escapedQuery)
46 if err != nil {
47 log.Fatal(err)
48 }
49 fmt.Println(query)
50
51 // Output:
52 // my/cool+blog&about,stuff
53 }
54
55 func ExampleValues() {
56 v := url.Values{}
57 v.Set("name", "Ava")
58 v.Add("friend", "Jess")
59 v.Add("friend", "Sarah")
60 v.Add("friend", "Zoe")
61 // v.Encode() == "name=Ava&friend=Jess&friend=Sarah&friend=Zoe"
62 fmt.Println(v.Get("name"))
63 fmt.Println(v.Get("friend"))
64 fmt.Println(v["friend"])
65 // Output:
66 // Ava
67 // Jess
68 // [Jess Sarah Zoe]
69 }
70
71 func ExampleValues_Add() {
72 v := url.Values{}
73 v.Add("cat sounds", "meow")
74 v.Add("cat sounds", "mew")
75 v.Add("cat sounds", "mau")
76 fmt.Println(v["cat sounds"])
77
78 // Output:
79 // [meow mew mau]
80 }
81
82 func ExampleValues_Del() {
83 v := url.Values{}
84 v.Add("cat sounds", "meow")
85 v.Add("cat sounds", "mew")
86 v.Add("cat sounds", "mau")
87 fmt.Println(v["cat sounds"])
88
89 v.Del("cat sounds")
90 fmt.Println(v["cat sounds"])
91
92 // Output:
93 // [meow mew mau]
94 // []
95 }
96
97 func ExampleValues_Encode() {
98 v := url.Values{}
99 v.Add("cat sounds", "meow")
100 v.Add("cat sounds", "mew/")
101 v.Add("cat sounds", "mau$")
102 fmt.Println(v.Encode())
103
104 // Output:
105 // cat+sounds=meow&cat+sounds=mew%2F&cat+sounds=mau%24
106 }
107
108 func ExampleValues_Get() {
109 v := url.Values{}
110 v.Add("cat sounds", "meow")
111 v.Add("cat sounds", "mew")
112 v.Add("cat sounds", "mau")
113 fmt.Printf("%q\n", v.Get("cat sounds"))
114 fmt.Printf("%q\n", v.Get("dog sounds"))
115
116 // Output:
117 // "meow"
118 // ""
119 }
120
121 func ExampleValues_Has() {
122 v := url.Values{}
123 v.Add("cat sounds", "meow")
124 v.Add("cat sounds", "mew")
125 v.Add("cat sounds", "mau")
126 fmt.Println(v.Has("cat sounds"))
127 fmt.Println(v.Has("dog sounds"))
128
129 // Output:
130 // true
131 // false
132 }
133
134 func ExampleValues_Set() {
135 v := url.Values{}
136 v.Add("cat sounds", "meow")
137 v.Add("cat sounds", "mew")
138 v.Add("cat sounds", "mau")
139 fmt.Println(v["cat sounds"])
140
141 v.Set("cat sounds", "meow")
142 fmt.Println(v["cat sounds"])
143
144 // Output:
145 // [meow mew mau]
146 // [meow]
147 }
148
149 func ExampleURL() {
150 u, err := url.Parse("http://bing.com/search?q=dotnet")
151 if err != nil {
152 log.Fatal(err)
153 }
154 u.Scheme = "https"
155 u.Host = "google.com"
156 q := u.Query()
157 q.Set("q", "golang")
158 u.RawQuery = q.Encode()
159 fmt.Println(u)
160 // Output: https://google.com/search?q=golang
161 }
162
163 func ExampleURL_roundtrip() {
164 // Parse + String preserve the original encoding.
165 u, err := url.Parse("https://example.com/foo%2fbar")
166 if err != nil {
167 log.Fatal(err)
168 }
169 fmt.Println(u.Path)
170 fmt.Println(u.RawPath)
171 fmt.Println(u.String())
172 // Output:
173 // /foo/bar
174 // /foo%2fbar
175 // https://example.com/foo%2fbar
176 }
177
178 func ExampleURL_ResolveReference() {
179 u, err := url.Parse("../../..//search?q=dotnet")
180 if err != nil {
181 log.Fatal(err)
182 }
183 base, err := url.Parse("http://example.com/directory/")
184 if err != nil {
185 log.Fatal(err)
186 }
187 fmt.Println(base.ResolveReference(u))
188 // Output:
189 // http://example.com/search?q=dotnet
190 }
191
192 func ExampleParseQuery() {
193 m, err := url.ParseQuery(`x=1&y=2&y=3`)
194 if err != nil {
195 log.Fatal(err)
196 }
197 fmt.Println(toJSON(m))
198 // Output:
199 // {"x":["1"], "y":["2", "3"]}
200 }
201
202 func ExampleURL_EscapedPath() {
203 u, err := url.Parse("http://example.com/x/y%2Fz")
204 if err != nil {
205 log.Fatal(err)
206 }
207 fmt.Println("Path:", u.Path)
208 fmt.Println("RawPath:", u.RawPath)
209 fmt.Println("EscapedPath:", u.EscapedPath())
210 // Output:
211 // Path: /x/y/z
212 // RawPath: /x/y%2Fz
213 // EscapedPath: /x/y%2Fz
214 }
215
216 func ExampleURL_EscapedFragment() {
217 u, err := url.Parse("http://example.com/#x/y%2Fz")
218 if err != nil {
219 log.Fatal(err)
220 }
221 fmt.Println("Fragment:", u.Fragment)
222 fmt.Println("RawFragment:", u.RawFragment)
223 fmt.Println("EscapedFragment:", u.EscapedFragment())
224 // Output:
225 // Fragment: x/y/z
226 // RawFragment: x/y%2Fz
227 // EscapedFragment: x/y%2Fz
228 }
229
230 func ExampleURL_Hostname() {
231 u, err := url.Parse("https://example.org:8000/path")
232 if err != nil {
233 log.Fatal(err)
234 }
235 fmt.Println(u.Hostname())
236 u, err = url.Parse("https://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:17000")
237 if err != nil {
238 log.Fatal(err)
239 }
240 fmt.Println(u.Hostname())
241 // Output:
242 // example.org
243 // 2001:0db8:85a3:0000:0000:8a2e:0370:7334
244 }
245
246 func ExampleURL_IsAbs() {
247 u := url.URL{Host: "example.com", Path: "foo"}
248 fmt.Println(u.IsAbs())
249 u.Scheme = "http"
250 fmt.Println(u.IsAbs())
251 // Output:
252 // false
253 // true
254 }
255
256 func ExampleURL_JoinPath() {
257 u, err := url.Parse("https://example.com/foo/bar")
258 if err != nil {
259 log.Fatal(err)
260 }
261
262 fmt.Println(u.JoinPath("baz", "qux"))
263
264 // Output:
265 // https://example.com/foo/bar/baz/qux
266 }
267
268 func ExampleURL_MarshalBinary() {
269 u, _ := url.Parse("https://example.org")
270 b, err := u.MarshalBinary()
271 if err != nil {
272 log.Fatal(err)
273 }
274 fmt.Printf("%s\n", b)
275 // Output:
276 // https://example.org
277 }
278
279 func ExampleURL_Parse() {
280 u, err := url.Parse("https://example.org")
281 if err != nil {
282 log.Fatal(err)
283 }
284 rel, err := u.Parse("/foo")
285 if err != nil {
286 log.Fatal(err)
287 }
288 fmt.Println(rel)
289 _, err = u.Parse(":foo")
290 if _, ok := err.(*url.Error); !ok {
291 log.Fatal(err)
292 }
293 // Output:
294 // https://example.org/foo
295 }
296
297 func ExampleURL_Port() {
298 u, err := url.Parse("https://example.org")
299 if err != nil {
300 log.Fatal(err)
301 }
302 fmt.Println(u.Port())
303 u, err = url.Parse("https://example.org:8080")
304 if err != nil {
305 log.Fatal(err)
306 }
307 fmt.Println(u.Port())
308 // Output:
309 //
310 // 8080
311 }
312
313 func ExampleURL_Query() {
314 u, err := url.Parse("https://example.org/?a=1&a=2&b=&=3&&&&")
315 if err != nil {
316 log.Fatal(err)
317 }
318 q := u.Query()
319 fmt.Println(q["a"])
320 fmt.Println(q.Get("b"))
321 fmt.Println(q.Get(""))
322 // Output:
323 // [1 2]
324 //
325 // 3
326 }
327
328 func ExampleURL_String() {
329 u := &url.URL{
330 Scheme: "https",
331 User: url.UserPassword("me", "pass"),
332 Host: "example.com",
333 Path: "foo/bar",
334 RawQuery: "x=1&y=2",
335 Fragment: "anchor",
336 }
337 fmt.Println(u.String())
338 u.Opaque = "opaque"
339 fmt.Println(u.String())
340 // Output:
341 // https://me:[email protected]/foo/bar?x=1&y=2#anchor
342 // https:opaque?x=1&y=2#anchor
343 }
344
345 func ExampleURL_UnmarshalBinary() {
346 u := &url.URL{}
347 err := u.UnmarshalBinary([]byte("https://example.org/foo"))
348 if err != nil {
349 log.Fatal(err)
350 }
351 fmt.Printf("%s\n", u)
352 // Output:
353 // https://example.org/foo
354 }
355
356 func ExampleURL_Redacted() {
357 u := &url.URL{
358 Scheme: "https",
359 User: url.UserPassword("user", "password"),
360 Host: "example.com",
361 Path: "foo/bar",
362 }
363 fmt.Println(u.Redacted())
364 u.User = url.UserPassword("me", "newerPassword")
365 fmt.Println(u.Redacted())
366 // Output:
367 // https://user:[email protected]/foo/bar
368 // https://me:[email protected]/foo/bar
369 }
370
371 func ExampleURL_RequestURI() {
372 u, err := url.Parse("https://example.org/path?foo=bar")
373 if err != nil {
374 log.Fatal(err)
375 }
376 fmt.Println(u.RequestURI())
377 // Output: /path?foo=bar
378 }
379
380 func toJSON(m any) string {
381 js, err := json.Marshal(m)
382 if err != nil {
383 log.Fatal(err)
384 }
385 return strings.ReplaceAll(string(js), ",", ", ")
386 }