Source file src/net/url/example_test.go

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

View as plain text