websocket_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Copyright 2016 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 portforward
  14. import (
  15. "net/http"
  16. "reflect"
  17. "testing"
  18. )
  19. func TestV4Options(t *testing.T) {
  20. tests := map[string]struct {
  21. url string
  22. websocket bool
  23. expectedOpts *V4Options
  24. expectedError string
  25. }{
  26. "non-ws request": {
  27. url: "http://example.com",
  28. expectedOpts: &V4Options{},
  29. },
  30. "missing port": {
  31. url: "http://example.com",
  32. websocket: true,
  33. expectedError: `query parameter "port" is required`,
  34. },
  35. "unable to parse port": {
  36. url: "http://example.com?port=abc",
  37. websocket: true,
  38. expectedError: `unable to parse "abc" as a port: strconv.ParseUint: parsing "abc": invalid syntax`,
  39. },
  40. "negative port": {
  41. url: "http://example.com?port=-1",
  42. websocket: true,
  43. expectedError: `unable to parse "-1" as a port: strconv.ParseUint: parsing "-1": invalid syntax`,
  44. },
  45. "one port": {
  46. url: "http://example.com?port=80",
  47. websocket: true,
  48. expectedOpts: &V4Options{
  49. Ports: []int32{80},
  50. },
  51. },
  52. "multiple ports": {
  53. url: "http://example.com?port=80,90,100",
  54. websocket: true,
  55. expectedOpts: &V4Options{
  56. Ports: []int32{80, 90, 100},
  57. },
  58. },
  59. "multiple port": {
  60. url: "http://example.com?port=80&port=90",
  61. websocket: true,
  62. expectedOpts: &V4Options{
  63. Ports: []int32{80, 90},
  64. },
  65. },
  66. }
  67. for name, test := range tests {
  68. req, err := http.NewRequest(http.MethodGet, test.url, nil)
  69. if err != nil {
  70. t.Errorf("%s: invalid url %q err=%q", name, test.url, err)
  71. continue
  72. }
  73. if test.websocket {
  74. req.Header.Set("Connection", "Upgrade")
  75. req.Header.Set("Upgrade", "websocket")
  76. }
  77. opts, err := NewV4Options(req)
  78. if len(test.expectedError) > 0 {
  79. if err == nil {
  80. t.Errorf("%s: expected err=%q, but it was nil", name, test.expectedError)
  81. }
  82. if e, a := test.expectedError, err.Error(); e != a {
  83. t.Errorf("%s: expected err=%q, got %q", name, e, a)
  84. }
  85. continue
  86. }
  87. if err != nil {
  88. t.Errorf("%s: unexpected error %v", name, err)
  89. continue
  90. }
  91. if !reflect.DeepEqual(test.expectedOpts, opts) {
  92. t.Errorf("%s: expected options %#v, got %#v", name, test.expectedOpts, err)
  93. }
  94. }
  95. }