unique_urls_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2018 The etcd Authors
  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. package flags
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. func TestNewUniqueURLsWithExceptions(t *testing.T) {
  20. tests := []struct {
  21. s string
  22. exp map[string]struct{}
  23. rs string
  24. exception string
  25. }{
  26. { // non-URL but allowed by exception
  27. s: "*",
  28. exp: map[string]struct{}{"*": {}},
  29. rs: "*",
  30. exception: "*",
  31. },
  32. {
  33. s: "",
  34. exp: map[string]struct{}{},
  35. rs: "",
  36. exception: "*",
  37. },
  38. {
  39. s: "https://1.2.3.4:8080",
  40. exp: map[string]struct{}{"https://1.2.3.4:8080": {}},
  41. rs: "https://1.2.3.4:8080",
  42. exception: "*",
  43. },
  44. {
  45. s: "https://1.2.3.4:8080,https://1.2.3.4:8080",
  46. exp: map[string]struct{}{"https://1.2.3.4:8080": {}},
  47. rs: "https://1.2.3.4:8080",
  48. exception: "*",
  49. },
  50. {
  51. s: "http://10.1.1.1:80",
  52. exp: map[string]struct{}{"http://10.1.1.1:80": {}},
  53. rs: "http://10.1.1.1:80",
  54. exception: "*",
  55. },
  56. {
  57. s: "http://localhost:80",
  58. exp: map[string]struct{}{"http://localhost:80": {}},
  59. rs: "http://localhost:80",
  60. exception: "*",
  61. },
  62. {
  63. s: "http://:80",
  64. exp: map[string]struct{}{"http://:80": {}},
  65. rs: "http://:80",
  66. exception: "*",
  67. },
  68. {
  69. s: "https://localhost:5,https://localhost:3",
  70. exp: map[string]struct{}{"https://localhost:3": {}, "https://localhost:5": {}},
  71. rs: "https://localhost:3,https://localhost:5",
  72. exception: "*",
  73. },
  74. {
  75. s: "http://localhost:5,https://localhost:3",
  76. exp: map[string]struct{}{"https://localhost:3": {}, "http://localhost:5": {}},
  77. rs: "http://localhost:5,https://localhost:3",
  78. exception: "*",
  79. },
  80. }
  81. for i := range tests {
  82. uv := NewUniqueURLsWithExceptions(tests[i].s, tests[i].exception)
  83. if !reflect.DeepEqual(tests[i].exp, uv.Values) {
  84. t.Fatalf("#%d: expected %+v, got %+v", i, tests[i].exp, uv.Values)
  85. }
  86. if uv.String() != tests[i].rs {
  87. t.Fatalf("#%d: expected %q, got %q", i, tests[i].rs, uv.String())
  88. }
  89. }
  90. }