github.com/Andyfoo/golang/x/net@v0.0.0-20190901054642-57c1bf301704/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 "fmt" 11 "log" 12 "net/http" 13 "testing" 14 15 "github.com/Andyfoo/golang/x/net/http2" 16 ) 17 18 func TestSettingsAckSwallowWriter(t *testing.T) { 19 var buf bytes.Buffer 20 swallower := newSettingsAckSwallowWriter(bufio.NewWriter(&buf)) 21 fw := http2.NewFramer(swallower, nil) 22 fw.WriteSettings(http2.Setting{http2.SettingMaxFrameSize, 2}) 23 fw.WriteSettingsAck() 24 fw.WriteData(1, true, []byte{}) 25 swallower.Flush() 26 27 fr := http2.NewFramer(nil, bufio.NewReader(&buf)) 28 29 f, err := fr.ReadFrame() 30 if err != nil { 31 t.Fatal(err) 32 } 33 if f.Header().Type != http2.FrameSettings { 34 t.Fatalf("Expected first frame to be SETTINGS. Got: %v", f.Header().Type) 35 } 36 37 f, err = fr.ReadFrame() 38 if err != nil { 39 t.Fatal(err) 40 } 41 if f.Header().Type != http2.FrameData { 42 t.Fatalf("Expected first frame to be DATA. Got: %v", f.Header().Type) 43 } 44 } 45 46 func ExampleNewHandler() { 47 handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 48 fmt.Fprint(w, "Hello world") 49 }) 50 h2s := &http2.Server{ 51 // ... 52 } 53 h1s := &http.Server{ 54 Addr: ":8080", 55 Handler: NewHandler(handler, h2s), 56 } 57 log.Fatal(h1s.ListenAndServe()) 58 }