gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/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 "gitee.com/ks-custle/core-gm/gmhttp" 15 "gitee.com/ks-custle/core-gm/gmhttp/httptest" 16 "gitee.com/ks-custle/core-gm/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 func(Body io.ReadCloser) { 42 _ = Body.Close() 43 }(resp.Body) 44 45 b, err := io.ReadAll(resp.Body) 46 if err != nil { 47 log.Fatal(err) 48 } 49 50 fmt.Printf("%s", b) 51 52 // Output: 53 // "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." 54 } 55 56 func ExampleDumpRequestOut() { 57 const body = "Go is a general-purpose language designed with systems programming in mind." 58 //goland:noinspection HttpUrlsUsage 59 req, err := http.NewRequest("PUT", "http://www.example.org", strings.NewReader(body)) 60 if err != nil { 61 log.Fatal(err) 62 } 63 64 dump, err := httputil.DumpRequestOut(req, true) 65 if err != nil { 66 log.Fatal(err) 67 } 68 69 fmt.Printf("%q", dump) 70 71 // Output: 72 // "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." 73 } 74 75 func ExampleDumpResponse() { 76 const body = "Go is a general-purpose language designed with systems programming in mind." 77 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 78 w.Header().Set("Date", "Wed, 19 Jul 1972 19:00:00 GMT") 79 _, _ = fmt.Fprintln(w, body) 80 })) 81 defer ts.Close() 82 83 resp, err := http.Get(ts.URL) 84 if err != nil { 85 log.Fatal(err) 86 } 87 defer func(Body io.ReadCloser) { 88 _ = Body.Close() 89 }(resp.Body) 90 91 dump, err := httputil.DumpResponse(resp, true) 92 if err != nil { 93 log.Fatal(err) 94 } 95 96 fmt.Printf("%q", dump) 97 98 // Output: 99 // "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" 100 } 101 102 func ExampleReverseProxy() { 103 backendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 104 _, _ = fmt.Fprintln(w, "this call was relayed by the reverse proxy") 105 })) 106 defer backendServer.Close() 107 108 rpURL, err := url.Parse(backendServer.URL) 109 if err != nil { 110 log.Fatal(err) 111 } 112 frontendProxy := httptest.NewServer(httputil.NewSingleHostReverseProxy(rpURL)) 113 defer frontendProxy.Close() 114 115 resp, err := http.Get(frontendProxy.URL) 116 if err != nil { 117 log.Fatal(err) 118 } 119 120 b, err := io.ReadAll(resp.Body) 121 if err != nil { 122 log.Fatal(err) 123 } 124 125 fmt.Printf("%s", b) 126 127 // Output: 128 // this call was relayed by the reverse proxy 129 }