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
5package url_test
6
7import (
8	"encoding/json"
9	"fmt"
10	"log"
11	"net/url"
12	"strings"
13)
14
15func 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
23func 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
35func 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
43func 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
55func 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
71func 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
82func 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
97func 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
108func 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
121func 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
134func 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
149func 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
163func 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
178func 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
192func 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
202func 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
216func 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
230func 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
246func 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
256func ExampleURL_MarshalBinary() {
257	u, _ := url.Parse("https://example.org")
258	b, err := u.MarshalBinary()
259	if err != nil {
260		log.Fatal(err)
261	}
262	fmt.Printf("%s\n", b)
263	// Output:
264	// https://example.org
265}
266
267func ExampleURL_Parse() {
268	u, err := url.Parse("https://example.org")
269	if err != nil {
270		log.Fatal(err)
271	}
272	rel, err := u.Parse("/foo")
273	if err != nil {
274		log.Fatal(err)
275	}
276	fmt.Println(rel)
277	_, err = u.Parse(":foo")
278	if _, ok := err.(*url.Error); !ok {
279		log.Fatal(err)
280	}
281	// Output:
282	// https://example.org/foo
283}
284
285func ExampleURL_Port() {
286	u, err := url.Parse("https://example.org")
287	if err != nil {
288		log.Fatal(err)
289	}
290	fmt.Println(u.Port())
291	u, err = url.Parse("https://example.org:8080")
292	if err != nil {
293		log.Fatal(err)
294	}
295	fmt.Println(u.Port())
296	// Output:
297	//
298	// 8080
299}
300
301func ExampleURL_Query() {
302	u, err := url.Parse("https://example.org/?a=1&a=2&b=&=3&&&&")
303	if err != nil {
304		log.Fatal(err)
305	}
306	q := u.Query()
307	fmt.Println(q["a"])
308	fmt.Println(q.Get("b"))
309	fmt.Println(q.Get(""))
310	// Output:
311	// [1 2]
312	//
313	// 3
314}
315
316func ExampleURL_String() {
317	u := &url.URL{
318		Scheme:   "https",
319		User:     url.UserPassword("me", "pass"),
320		Host:     "example.com",
321		Path:     "foo/bar",
322		RawQuery: "x=1&y=2",
323		Fragment: "anchor",
324	}
325	fmt.Println(u.String())
326	u.Opaque = "opaque"
327	fmt.Println(u.String())
328	// Output:
329	// https://me:[email protected]/foo/bar?x=1&y=2#anchor
330	// https:opaque?x=1&y=2#anchor
331}
332
333func ExampleURL_UnmarshalBinary() {
334	u := &url.URL{}
335	err := u.UnmarshalBinary([]byte("https://example.org/foo"))
336	if err != nil {
337		log.Fatal(err)
338	}
339	fmt.Printf("%s\n", u)
340	// Output:
341	// https://example.org/foo
342}
343
344func ExampleURL_Redacted() {
345	u := &url.URL{
346		Scheme: "https",
347		User:   url.UserPassword("user", "password"),
348		Host:   "example.com",
349		Path:   "foo/bar",
350	}
351	fmt.Println(u.Redacted())
352	u.User = url.UserPassword("me", "newerPassword")
353	fmt.Println(u.Redacted())
354	// Output:
355	// https://user:[email protected]/foo/bar
356	// https://me:[email protected]/foo/bar
357}
358
359func ExampleURL_RequestURI() {
360	u, err := url.Parse("https://example.org/path?foo=bar")
361	if err != nil {
362		log.Fatal(err)
363	}
364	fmt.Println(u.RequestURI())
365	// Output: /path?foo=bar
366}
367
368func toJSON(m any) string {
369	js, err := json.Marshal(m)
370	if err != nil {
371		log.Fatal(err)
372	}
373	return strings.ReplaceAll(string(js), ",", ", ")
374}
375