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