diff options
author | Brad Fitzpatrick <bradfitz@golang.org> | 2014-11-12 14:27:27 -0800 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2014-11-12 14:27:27 -0800 |
commit | dfa6e491915706addc5cee4178a1921f8dd2dacd (patch) | |
tree | 4b44ac4d8aa7f0bb93cecff36dd7fb1b47fc8983 | |
parent | 6469d5fad1f6f5cf70a6a4bc8bc6b7717244ec90 (diff) | |
download | go-dfa6e491915706addc5cee4178a1921f8dd2dacd.tar.gz |
net/url: add example of using URL.Opaque with http.Request
Per private thread soliciting help. I realized part of this is
documented in several places, but we lacked a unifying
example.
LGTM=rsc
R=golang-codereviews
CC=adg, golang-codereviews, iant, rsc
https://codereview.appspot.com/171620043
-rw-r--r-- | src/net/url/example_test.go | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/net/url/example_test.go b/src/net/url/example_test.go index 56c5dc696..e55c1aa16 100644 --- a/src/net/url/example_test.go +++ b/src/net/url/example_test.go @@ -7,7 +7,10 @@ package url_test import ( "fmt" "log" + "net/http" + "net/http/httputil" "net/url" + "strings" ) func ExampleValues() { @@ -39,3 +42,30 @@ func ExampleURL() { fmt.Println(u) // Output: https://google.com/search?q=golang } + +func ExampleURL_opaque() { + // Sending a literal '%' in an HTTP request's Path + req := &http.Request{ + Method: "GET", + Host: "example.com", // takes precendence over URL.Host + URL: &url.URL{ + Host: "ignored", + Scheme: "https", + Opaque: "/%2f/", + }, + Header: http.Header{ + "User-Agent": {"godoc-example/0.1"}, + }, + } + out, err := httputil.DumpRequestOut(req, true) + if err != nil { + log.Fatal(err) + } + fmt.Println(strings.Replace(string(out), "\r", "", -1)) + // Output: + // GET /%2f/ HTTP/1.1 + // Host: example.com + // User-Agent: godoc-example/0.1 + // Accept-Encoding: gzip + // +} |