gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/net/http2/h2c/h2c_test.go (about)

     1  // Copyright 2018 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 h2c
     6  
     7  import (
     8  	"bufio"
     9  	"bytes"
    10  	"context"
    11  	"fmt"
    12  	"io/ioutil"
    13  	"log"
    14  	"net"
    15  	"testing"
    16  
    17  	http "gitee.com/ks-custle/core-gm/gmhttp"
    18  	"gitee.com/ks-custle/core-gm/gmhttp/httptest"
    19  	tls "gitee.com/ks-custle/core-gm/gmtls"
    20  	"gitee.com/ks-custle/core-gm/net/http2"
    21  )
    22  
    23  func TestSettingsAckSwallowWriter(t *testing.T) {
    24  	var buf bytes.Buffer
    25  	swallower := newSettingsAckSwallowWriter(bufio.NewWriter(&buf))
    26  	fw := http2.NewFramer(swallower, nil)
    27  	fw.WriteSettings(http2.Setting{http2.SettingMaxFrameSize, 2})
    28  	fw.WriteSettingsAck()
    29  	fw.WriteData(1, true, []byte{})
    30  	swallower.Flush()
    31  
    32  	fr := http2.NewFramer(nil, bufio.NewReader(&buf))
    33  
    34  	f, err := fr.ReadFrame()
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	if f.Header().Type != http2.FrameSettings {
    39  		t.Fatalf("Expected first frame to be SETTINGS. Got: %v", f.Header().Type)
    40  	}
    41  
    42  	f, err = fr.ReadFrame()
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	if f.Header().Type != http2.FrameData {
    47  		t.Fatalf("Expected first frame to be DATA. Got: %v", f.Header().Type)
    48  	}
    49  }
    50  
    51  func ExampleNewHandler() {
    52  	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    53  		fmt.Fprint(w, "Hello world")
    54  	})
    55  	h2s := &http2.Server{
    56  		// ...
    57  	}
    58  	h1s := &http.Server{
    59  		Addr:    ":8080",
    60  		Handler: NewHandler(handler, h2s),
    61  	}
    62  	log.Fatal(h1s.ListenAndServe())
    63  }
    64  
    65  func TestContext(t *testing.T) {
    66  	baseCtx := context.WithValue(context.Background(), "testkey", "testvalue")
    67  
    68  	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    69  		if r.ProtoMajor != 2 {
    70  			t.Errorf("Request wasn't handled by h2c.  Got ProtoMajor=%v", r.ProtoMajor)
    71  		}
    72  		if r.Context().Value("testkey") != "testvalue" {
    73  			t.Errorf("Request doesn't have expected base context: %v", r.Context())
    74  		}
    75  		fmt.Fprint(w, "Hello world")
    76  	})
    77  
    78  	h2s := &http2.Server{}
    79  	h1s := httptest.NewUnstartedServer(NewHandler(handler, h2s))
    80  	h1s.Config.BaseContext = func(_ net.Listener) context.Context {
    81  		return baseCtx
    82  	}
    83  	h1s.Start()
    84  	defer h1s.Close()
    85  
    86  	client := &http.Client{
    87  		Transport: &http2.Transport{
    88  			AllowHTTP: true,
    89  			DialTLS: func(network, addr string, _ *tls.Config) (net.Conn, error) {
    90  				return net.Dial(network, addr)
    91  			},
    92  		},
    93  	}
    94  
    95  	resp, err := client.Get(h1s.URL)
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  	_, err = ioutil.ReadAll(resp.Body)
   100  	if err != nil {
   101  		t.Fatal(err)
   102  	}
   103  	if err := resp.Body.Close(); err != nil {
   104  		t.Fatal(err)
   105  	}
   106  }