github.com/ck00004/CobaltStrikeParser-Go@v1.0.14/lib/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 "encoding/json" 9 "fmt" 10 "log" 11 "strings" 12 13 "github.com/ck00004/CobaltStrikeParser-Go/lib/url" 14 ) 15 16 func ExampleValues() { 17 v := url.Values{} 18 v.Set("name", "Ava") 19 v.Add("friend", "Jess") 20 v.Add("friend", "Sarah") 21 v.Add("friend", "Zoe") 22 // v.Encode() == "name=Ava&friend=Jess&friend=Sarah&friend=Zoe" 23 fmt.Println(v.Get("name")) 24 fmt.Println(v.Get("friend")) 25 fmt.Println(v["friend"]) 26 // Output: 27 // Ava 28 // Jess 29 // [Jess Sarah Zoe] 30 } 31 32 func ExampleURL() { 33 u, err := url.Parse("http://bing.com/search?q=dotnet") 34 if err != nil { 35 log.Fatal(err) 36 } 37 u.Scheme = "https" 38 u.Host = "google.com" 39 q := u.Query() 40 q.Set("q", "golang") 41 u.RawQuery = q.Encode() 42 fmt.Println(u) 43 // Output: https://google.com/search?q=golang 44 } 45 46 func ExampleURL_roundtrip() { 47 // Parse + String preserve the original encoding. 48 u, err := url.Parse("https://example.com/foo%2fbar") 49 if err != nil { 50 log.Fatal(err) 51 } 52 fmt.Println(u.Path) 53 fmt.Println(u.RawPath) 54 fmt.Println(u.String()) 55 // Output: 56 // /foo/bar 57 // /foo%2fbar 58 // https://example.com/foo%2fbar 59 } 60 61 func ExampleURL_ResolveReference() { 62 u, err := url.Parse("../../..//search?q=dotnet") 63 if err != nil { 64 log.Fatal(err) 65 } 66 base, err := url.Parse("http://example.com/directory/") 67 if err != nil { 68 log.Fatal(err) 69 } 70 fmt.Println(base.ResolveReference(u)) 71 // Output: 72 // http://example.com/search?q=dotnet 73 } 74 75 func ExampleParseQuery() { 76 m, err := url.ParseQuery(`x=1&y=2&y=3`) 77 if err != nil { 78 log.Fatal(err) 79 } 80 fmt.Println(toJSON(m)) 81 // Output: 82 // {"x":["1"], "y":["2", "3"]} 83 } 84 85 func ExampleURL_EscapedPath() { 86 u, err := url.Parse("http://example.com/x/y%2Fz") 87 if err != nil { 88 log.Fatal(err) 89 } 90 fmt.Println("Path:", u.Path) 91 fmt.Println("RawPath:", u.RawPath) 92 fmt.Println("EscapedPath:", u.EscapedPath()) 93 // Output: 94 // Path: /x/y/z 95 // RawPath: /x/y%2Fz 96 // EscapedPath: /x/y%2Fz 97 } 98 99 func ExampleURL_EscapedFragment() { 100 u, err := url.Parse("http://example.com/#x/y%2Fz") 101 if err != nil { 102 log.Fatal(err) 103 } 104 fmt.Println("Fragment:", u.Fragment) 105 fmt.Println("RawFragment:", u.RawFragment) 106 fmt.Println("EscapedFragment:", u.EscapedFragment()) 107 // Output: 108 // Fragment: x/y/z 109 // RawFragment: x/y%2Fz 110 // EscapedFragment: x/y%2Fz 111 } 112 113 func ExampleURL_Hostname() { 114 u, err := url.Parse("https://example.org:8000/path") 115 if err != nil { 116 log.Fatal(err) 117 } 118 fmt.Println(u.Hostname()) 119 u, err = url.Parse("https://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:17000") 120 if err != nil { 121 log.Fatal(err) 122 } 123 fmt.Println(u.Hostname()) 124 // Output: 125 // example.org 126 // 2001:0db8:85a3:0000:0000:8a2e:0370:7334 127 } 128 129 func ExampleURL_IsAbs() { 130 u := url.URL{Host: "example.com", Path: "foo"} 131 fmt.Println(u.IsAbs()) 132 u.Scheme = "http" 133 fmt.Println(u.IsAbs()) 134 // Output: 135 // false 136 // true 137 } 138 139 func ExampleURL_MarshalBinary() { 140 u, _ := url.Parse("https://example.org") 141 b, err := u.MarshalBinary() 142 if err != nil { 143 log.Fatal(err) 144 } 145 fmt.Printf("%s\n", b) 146 // Output: 147 // https://example.org 148 } 149 150 func ExampleURL_Parse() { 151 u, err := url.Parse("https://example.org") 152 if err != nil { 153 log.Fatal(err) 154 } 155 rel, err := u.Parse("/foo") 156 if err != nil { 157 log.Fatal(err) 158 } 159 fmt.Println(rel) 160 _, err = u.Parse(":foo") 161 if _, ok := err.(*url.Error); !ok { 162 log.Fatal(err) 163 } 164 // Output: 165 // https://example.org/foo 166 } 167 168 func ExampleURL_Port() { 169 u, err := url.Parse("https://example.org") 170 if err != nil { 171 log.Fatal(err) 172 } 173 fmt.Println(u.Port()) 174 u, err = url.Parse("https://example.org:8080") 175 if err != nil { 176 log.Fatal(err) 177 } 178 fmt.Println(u.Port()) 179 // Output: 180 // 181 // 8080 182 } 183 184 func ExampleURL_Query() { 185 u, err := url.Parse("https://example.org/?a=1&a=2&b=&=3&&&&") 186 if err != nil { 187 log.Fatal(err) 188 } 189 q := u.Query() 190 fmt.Println(q["a"]) 191 fmt.Println(q.Get("b")) 192 fmt.Println(q.Get("")) 193 // Output: 194 // [1 2] 195 // 196 // 3 197 } 198 199 func ExampleURL_String() { 200 u := &url.URL{ 201 Scheme: "https", 202 User: url.UserPassword("me", "pass"), 203 Host: "example.com", 204 Path: "foo/bar", 205 RawQuery: "x=1&y=2", 206 Fragment: "anchor", 207 } 208 fmt.Println(u.String()) 209 u.Opaque = "opaque" 210 fmt.Println(u.String()) 211 // Output: 212 // https://me:pass@example.com/foo/bar?x=1&y=2#anchor 213 // https:opaque?x=1&y=2#anchor 214 } 215 216 func ExampleURL_UnmarshalBinary() { 217 u := &url.URL{} 218 err := u.UnmarshalBinary([]byte("https://example.org/foo")) 219 if err != nil { 220 log.Fatal(err) 221 } 222 fmt.Printf("%s\n", u) 223 // Output: 224 // https://example.org/foo 225 } 226 227 func ExampleURL_Redacted() { 228 u := &url.URL{ 229 Scheme: "https", 230 User: url.UserPassword("user", "password"), 231 Host: "example.com", 232 Path: "foo/bar", 233 } 234 fmt.Println(u.Redacted()) 235 u.User = url.UserPassword("me", "newerPassword") 236 fmt.Println(u.Redacted()) 237 // Output: 238 // https://user:xxxxx@example.com/foo/bar 239 // https://me:xxxxx@example.com/foo/bar 240 } 241 242 func ExampleURL_RequestURI() { 243 u, err := url.Parse("https://example.org/path?foo=bar") 244 if err != nil { 245 log.Fatal(err) 246 } 247 fmt.Println(u.RequestURI()) 248 // Output: /path?foo=bar 249 } 250 251 func toJSON(m interface{}) string { 252 js, err := json.Marshal(m) 253 if err != nil { 254 log.Fatal(err) 255 } 256 return strings.ReplaceAll(string(js), ",", ", ") 257 }