github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/net/url/example_test.go (about)

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