httpstream_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package httpstream
  14. import (
  15. "net/http"
  16. "reflect"
  17. "testing"
  18. "k8s.io/kubernetes/pkg/api"
  19. )
  20. type responseWriter struct {
  21. header http.Header
  22. statusCode *int
  23. }
  24. func newResponseWriter() *responseWriter {
  25. return &responseWriter{
  26. header: make(http.Header),
  27. }
  28. }
  29. func (r *responseWriter) Header() http.Header {
  30. return r.header
  31. }
  32. func (r *responseWriter) WriteHeader(code int) {
  33. r.statusCode = &code
  34. }
  35. func (r *responseWriter) Write([]byte) (int, error) {
  36. return 0, nil
  37. }
  38. func TestHandshake(t *testing.T) {
  39. tests := map[string]struct {
  40. clientProtocols []string
  41. serverProtocols []string
  42. expectedProtocol string
  43. expectError bool
  44. }{
  45. "no client protocols": {
  46. clientProtocols: []string{},
  47. serverProtocols: []string{"a", "b"},
  48. expectedProtocol: "",
  49. },
  50. "no common protocol": {
  51. clientProtocols: []string{"c"},
  52. serverProtocols: []string{"a", "b"},
  53. expectedProtocol: "",
  54. expectError: true,
  55. },
  56. "common protocol": {
  57. clientProtocols: []string{"b"},
  58. serverProtocols: []string{"a", "b"},
  59. expectedProtocol: "b",
  60. },
  61. }
  62. for name, test := range tests {
  63. req, err := http.NewRequest("GET", "http://www.example.com/", nil)
  64. if err != nil {
  65. t.Fatalf("%s: error creating request: %v", name, err)
  66. }
  67. for _, p := range test.clientProtocols {
  68. req.Header.Add(HeaderProtocolVersion, p)
  69. }
  70. w := newResponseWriter()
  71. negotiated, err := Handshake(req, w, test.serverProtocols)
  72. // verify negotiated protocol
  73. if e, a := test.expectedProtocol, negotiated; e != a {
  74. t.Errorf("%s: protocol: expected %q, got %q", name, e, a)
  75. }
  76. if test.expectError {
  77. if err == nil {
  78. t.Errorf("%s: expected error but did not get one", name)
  79. }
  80. if w.statusCode == nil {
  81. t.Errorf("%s: expected w.statusCode to be set", name)
  82. } else if e, a := http.StatusForbidden, *w.statusCode; e != a {
  83. t.Errorf("%s: w.statusCode: expected %d, got %d", name, e, a)
  84. }
  85. if e, a := test.serverProtocols, w.Header()[HeaderAcceptedProtocolVersions]; !reflect.DeepEqual(e, a) {
  86. t.Errorf("%s: accepted server protocols: expected %v, got %v", name, e, a)
  87. }
  88. continue
  89. }
  90. if !test.expectError && err != nil {
  91. t.Errorf("%s: unexpected error: %v", name, err)
  92. continue
  93. }
  94. if w.statusCode != nil {
  95. t.Errorf("%s: unexpected non-nil w.statusCode: %d", name, w.statusCode)
  96. }
  97. if len(test.expectedProtocol) == 0 {
  98. if len(w.Header()[HeaderProtocolVersion]) > 0 {
  99. t.Errorf("%s: unexpected protocol version response header: %s", name, w.Header()[HeaderProtocolVersion])
  100. }
  101. continue
  102. }
  103. // verify response headers
  104. if e, a := []string{test.expectedProtocol}, w.Header()[HeaderProtocolVersion]; !api.Semantic.DeepEqual(e, a) {
  105. t.Errorf("%s: protocol response header: expected %v, got %v", name, e, a)
  106. }
  107. }
  108. }