...

Source file src/net/http/httputil/example_test.go

Documentation: net/http/httputil

     1  // Copyright 2015 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 httputil_test
     6  
     7  import (
     8  	"fmt"
     9  	"io"
    10  	"log"
    11  	"net/http"
    12  	"net/http/httptest"
    13  	"net/http/httputil"
    14  	"net/url"
    15  	"strings"
    16  )
    17  
    18  func ExampleDumpRequest() {
    19  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    20  		dump, err := httputil.DumpRequest(r, true)
    21  		if err != nil {
    22  			http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
    23  			return
    24  		}
    25  
    26  		fmt.Fprintf(w, "%q", dump)
    27  	}))
    28  	defer ts.Close()
    29  
    30  	const body = "Go is a general-purpose language designed with systems programming in mind."
    31  	req, err := http.NewRequest("POST", ts.URL, strings.NewReader(body))
    32  	if err != nil {
    33  		log.Fatal(err)
    34  	}
    35  	req.Host = "www.example.org"
    36  	resp, err := http.DefaultClient.Do(req)
    37  	if err != nil {
    38  		log.Fatal(err)
    39  	}
    40  	defer resp.Body.Close()
    41  
    42  	b, err := io.ReadAll(resp.Body)
    43  	if err != nil {
    44  		log.Fatal(err)
    45  	}
    46  
    47  	fmt.Printf("%s", b)
    48  
    49  	// Output:
    50  	// "POST / HTTP/1.1\r\nHost: www.example.org\r\nAccept-Encoding: gzip\r\nContent-Length: 75\r\nUser-Agent: Go-http-client/1.1\r\n\r\nGo is a general-purpose language designed with systems programming in mind."
    51  }
    52  
    53  func ExampleDumpRequestOut() {
    54  	const body = "Go is a general-purpose language designed with systems programming in mind."
    55  	req, err := http.NewRequest("PUT", "http://www.example.org", strings.NewReader(body))
    56  	if err != nil {
    57  		log.Fatal(err)
    58  	}
    59  
    60  	dump, err := httputil.DumpRequestOut(req, true)
    61  	if err != nil {
    62  		log.Fatal(err)
    63  	}
    64  
    65  	fmt.Printf("%q", dump)
    66  
    67  	// Output:
    68  	// "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."
    69  }
    70  
    71  func ExampleDumpResponse() {
    72  	const body = "Go is a general-purpose language designed with systems programming in mind."
    73  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    74  		w.Header().Set("Date", "Wed, 19 Jul 1972 19:00:00 GMT")
    75  		fmt.Fprintln(w, body)
    76  	}))
    77  	defer ts.Close()
    78  
    79  	resp, err := http.Get(ts.URL)
    80  	if err != nil {
    81  		log.Fatal(err)
    82  	}
    83  	defer resp.Body.Close()
    84  
    85  	dump, err := httputil.DumpResponse(resp, true)
    86  	if err != nil {
    87  		log.Fatal(err)
    88  	}
    89  
    90  	fmt.Printf("%q", dump)
    91  
    92  	// Output:
    93  	// "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"
    94  }
    95  
    96  func ExampleReverseProxy() {
    97  	backendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    98  		fmt.Fprintln(w, "this call was relayed by the reverse proxy")
    99  	}))
   100  	defer backendServer.Close()
   101  
   102  	rpURL, err := url.Parse(backendServer.URL)
   103  	if err != nil {
   104  		log.Fatal(err)
   105  	}
   106  	frontendProxy := httptest.NewServer(&httputil.ReverseProxy{
   107  		Rewrite: func(r *httputil.ProxyRequest) {
   108  			r.SetXForwarded()
   109  			r.SetURL(rpURL)
   110  		},
   111  	})
   112  	defer frontendProxy.Close()
   113  
   114  	resp, err := http.Get(frontendProxy.URL)
   115  	if err != nil {
   116  		log.Fatal(err)
   117  	}
   118  
   119  	b, err := io.ReadAll(resp.Body)
   120  	if err != nil {
   121  		log.Fatal(err)
   122  	}
   123  
   124  	fmt.Printf("%s", b)
   125  
   126  	// Output:
   127  	// this call was relayed by the reverse proxy
   128  }
   129  

View as plain text