upgrade_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. Copyright 2015 The Kubernetes Authors.
  16. Licensed under the Apache License, Version 2.0 (the "License");
  17. you may not use this file except in compliance with the License.
  18. You may obtain a copy of the License at
  19. http://www.apache.org/licenses/LICENSE-2.0
  20. Unless required by applicable law or agreed to in writing, software
  21. distributed under the License is distributed on an "AS IS" BASIS,
  22. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. See the License for the specific language governing permissions and
  24. limitations under the License.
  25. */
  26. package spdy
  27. import (
  28. "net/http"
  29. "net/http/httptest"
  30. "testing"
  31. )
  32. func TestUpgradeResponse(t *testing.T) {
  33. testCases := []struct {
  34. connectionHeader string
  35. upgradeHeader string
  36. shouldError bool
  37. }{
  38. {
  39. connectionHeader: "",
  40. upgradeHeader: "",
  41. shouldError: true,
  42. },
  43. {
  44. connectionHeader: "Upgrade",
  45. upgradeHeader: "",
  46. shouldError: true,
  47. },
  48. {
  49. connectionHeader: "",
  50. upgradeHeader: "SPDY/3.1",
  51. shouldError: true,
  52. },
  53. {
  54. connectionHeader: "Upgrade",
  55. upgradeHeader: "SPDY/3.1",
  56. shouldError: false,
  57. },
  58. }
  59. for i, testCase := range testCases {
  60. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  61. upgrader := NewResponseUpgrader()
  62. conn := upgrader.UpgradeResponse(w, req, nil)
  63. haveErr := conn == nil
  64. if e, a := testCase.shouldError, haveErr; e != a {
  65. t.Fatalf("%d: expected shouldErr=%t, got %t", i, testCase.shouldError, haveErr)
  66. }
  67. if haveErr {
  68. return
  69. }
  70. if conn == nil {
  71. t.Fatalf("%d: unexpected nil conn", i)
  72. }
  73. defer conn.Close()
  74. }))
  75. defer server.Close()
  76. req, err := http.NewRequest("GET", server.URL, nil)
  77. if err != nil {
  78. t.Fatalf("%d: error creating request: %s", i, err)
  79. }
  80. req.Header.Set("Connection", testCase.connectionHeader)
  81. req.Header.Set("Upgrade", testCase.upgradeHeader)
  82. client := &http.Client{}
  83. resp, err := client.Do(req)
  84. if err != nil {
  85. t.Fatalf("%d: unexpected non-nil err from client.Do: %s", i, err)
  86. }
  87. if testCase.shouldError {
  88. continue
  89. }
  90. if resp.StatusCode != http.StatusSwitchingProtocols {
  91. t.Fatalf("%d: expected status 101 switching protocols, got %d", i, resp.StatusCode)
  92. }
  93. }
  94. }