go18_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2016 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. // +build go1.8
  5. package http2
  6. import (
  7. "net/http"
  8. "testing"
  9. "time"
  10. )
  11. // Tests that http2.Server.IdleTimeout is initialized from
  12. // http.Server.{Idle,Read}Timeout. http.Server.IdleTimeout was
  13. // added in Go 1.8.
  14. func TestConfigureServerIdleTimeout_Go18(t *testing.T) {
  15. const timeout = 5 * time.Second
  16. const notThisOne = 1 * time.Second
  17. // With a zero http2.Server, verify that it copies IdleTimeout:
  18. {
  19. s1 := &http.Server{
  20. IdleTimeout: timeout,
  21. ReadTimeout: notThisOne,
  22. }
  23. s2 := &Server{}
  24. if err := ConfigureServer(s1, s2); err != nil {
  25. t.Fatal(err)
  26. }
  27. if s2.IdleTimeout != timeout {
  28. t.Errorf("s2.IdleTimeout = %v; want %v", s2.IdleTimeout, timeout)
  29. }
  30. }
  31. // And that it falls back to ReadTimeout:
  32. {
  33. s1 := &http.Server{
  34. ReadTimeout: timeout,
  35. }
  36. s2 := &Server{}
  37. if err := ConfigureServer(s1, s2); err != nil {
  38. t.Fatal(err)
  39. }
  40. if s2.IdleTimeout != timeout {
  41. t.Errorf("s2.IdleTimeout = %v; want %v", s2.IdleTimeout, timeout)
  42. }
  43. }
  44. // Verify that s1's IdleTimeout doesn't overwrite an existing setting:
  45. {
  46. s1 := &http.Server{
  47. IdleTimeout: notThisOne,
  48. }
  49. s2 := &Server{
  50. IdleTimeout: timeout,
  51. }
  52. if err := ConfigureServer(s1, s2); err != nil {
  53. t.Fatal(err)
  54. }
  55. if s2.IdleTimeout != timeout {
  56. t.Errorf("s2.IdleTimeout = %v; want %v", s2.IdleTimeout, timeout)
  57. }
  58. }
  59. }