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