h2c_test.go 1.3 KB

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