-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net/http/httputil: add package level examples
Change-Id: I5be18b68602a7ebb740cf05924236ce17e386e55 Reviewed-on: https://go-review.googlesource.com/17295 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
- Loading branch information
Showing
1 changed file
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright 2015 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package httputil_test | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/http/httputil" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
func ExampleDumpRequest() { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
dump, err := httputil.DumpRequest(r, true) | ||
if err != nil { | ||
http.Error(w, fmt.Sprint(err), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
fmt.Fprintf(w, "%q", dump) | ||
})) | ||
defer ts.Close() | ||
|
||
const body = "Go is a general-purpose language designed with systems programming in mind." | ||
req, err := http.NewRequest("POST", ts.URL, strings.NewReader(body)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
req.Host = "www.example.org" | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
b, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Printf("%s", b) | ||
|
||
// Output: | ||
// "POST / HTTP/1.1\r\nHost: www.example.org\r\nAccept-Encoding: gzip\r\nUser-Agent: Go-http-client/1.1\r\n\r\nGo is a general-purpose language designed with systems programming in mind." | ||
} | ||
|
||
func ExampleDumpRequestOut() { | ||
const body = "Go is a general-purpose language designed with systems programming in mind." | ||
req, err := http.NewRequest("PUT", "http://www.example.org", strings.NewReader(body)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
dump, err := httputil.DumpRequestOut(req, true) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Printf("%q", dump) | ||
|
||
// Output: | ||
// "PUT / HTTP/1.1\r\nHost: www.example.org\r\nUser-Agent: Go-http-client/1.1\r\nContent-Length: 75\r\nAccept-Encoding: gzip\r\n\r\nGo is a general-purpose language designed with systems programming in mind." | ||
} | ||
|
||
func ExampleDumpResponse() { | ||
const body = "Go is a general-purpose language designed with systems programming in mind." | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Date", "Wed, 19 Jul 1972 19:00:00 GMT") | ||
fmt.Fprintln(w, body) | ||
})) | ||
defer ts.Close() | ||
|
||
resp, err := http.Get(ts.URL) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
dump, err := httputil.DumpResponse(resp, true) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Printf("%q", dump) | ||
|
||
// Output: | ||
// "HTTP/1.1 200 OK\r\nContent-Length: 76\r\nContent-Type: text/plain; charset=utf-8\r\nDate: Wed, 19 Jul 1972 19:00:00 GMT\r\n\r\nGo is a general-purpose language designed with systems programming in mind.\n" | ||
} | ||
|
||
func ExampleReverseProxy() { | ||
backendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintln(w, "this call was relayed by the reverse proxy") | ||
})) | ||
defer backendServer.Close() | ||
|
||
rpURL, err := url.Parse(backendServer.URL) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
frontendProxy := httptest.NewServer(httputil.NewSingleHostReverseProxy(rpURL)) | ||
defer frontendProxy.Close() | ||
|
||
resp, err := http.Get(frontendProxy.URL) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
b, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Printf("%s", b) | ||
|
||
// Output: | ||
// this call was relayed by the reverse proxy | ||
} |