gitee.com/zhaochuninhefei/gmgo@v0.0.31-0.20240209061119-069254a02979/gmhttp/httptest/example_test.go (about)

     1  // Copyright 2013 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 httptest_test
     6  
     7  import (
     8  	"fmt"
     9  	"io"
    10  	"log"
    11  
    12  	http "gitee.com/zhaochuninhefei/gmgo/gmhttp"
    13  	"gitee.com/zhaochuninhefei/gmgo/gmhttp/httptest"
    14  	"gitee.com/zhaochuninhefei/zcgolog/zclog"
    15  )
    16  
    17  func ExampleResponseRecorder() {
    18  	handler := func(w http.ResponseWriter, r *http.Request) {
    19  		_, _ = io.WriteString(w, "<html><body>Hello World!</body></html>")
    20  	}
    21  
    22  	//goland:noinspection HttpUrlsUsage
    23  	req := httptest.NewRequest("GET", "http://example.com/foo", nil)
    24  	w := httptest.NewRecorder()
    25  	handler(w, req)
    26  
    27  	resp := w.Result()
    28  	body, _ := io.ReadAll(resp.Body)
    29  
    30  	fmt.Println(resp.StatusCode)
    31  	fmt.Println(resp.Header.Get("Content-Type"))
    32  	fmt.Println(string(body))
    33  
    34  	// Output:
    35  	// 200
    36  	// text/html; charset=utf-8
    37  	// <html><body>Hello World!</body></html>
    38  }
    39  
    40  func ExampleServer() {
    41  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    42  		_, _ = fmt.Fprintln(w, "Hello, client")
    43  	}))
    44  	defer ts.Close()
    45  
    46  	res, err := http.Get(ts.URL)
    47  	if err != nil {
    48  		log.Fatal(err)
    49  	}
    50  	greeting, err := io.ReadAll(res.Body)
    51  	_ = res.Body.Close()
    52  	if err != nil {
    53  		log.Fatal(err)
    54  	}
    55  
    56  	fmt.Printf("%s", greeting)
    57  	// Output: Hello, client
    58  }
    59  
    60  func ExampleServer_hTTP2() {
    61  	zcgologConfig := &zclog.Config{
    62  		LogLevelGlobal: zclog.LOG_LEVEL_INFO,
    63  	}
    64  	zclog.InitLogger(zcgologConfig)
    65  	ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    66  		_, _ = fmt.Fprintf(w, "Hello, %s", r.Proto)
    67  	}))
    68  	ts.EnableHTTP2 = true
    69  	ts.StartTLS()
    70  	defer ts.Close()
    71  
    72  	res, err := ts.Client().Get(ts.URL)
    73  	if err != nil {
    74  		log.Fatal(err)
    75  	}
    76  	greeting, err := io.ReadAll(res.Body)
    77  	_ = res.Body.Close()
    78  	if err != nil {
    79  		log.Fatal(err)
    80  	}
    81  	fmt.Printf("%s", greeting)
    82  
    83  	// Output: Hello, HTTP/2.0
    84  }
    85  
    86  func ExampleNewTLSServer() {
    87  	zcgologConfig := &zclog.Config{
    88  		LogLevelGlobal: zclog.LOG_LEVEL_INFO,
    89  	}
    90  	zclog.InitLogger(zcgologConfig)
    91  	ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    92  		_, _ = fmt.Fprintln(w, "Hello, client")
    93  	}))
    94  	defer ts.Close()
    95  
    96  	client := ts.Client()
    97  	res, err := client.Get(ts.URL)
    98  	if err != nil {
    99  		log.Fatal(err)
   100  	}
   101  
   102  	greeting, err := io.ReadAll(res.Body)
   103  	_ = res.Body.Close()
   104  	if err != nil {
   105  		log.Fatal(err)
   106  	}
   107  
   108  	fmt.Printf("%s", greeting)
   109  	// Output: Hello, client
   110  }