urls_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2015 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. "net/url"
  17. "reflect"
  18. "testing"
  19. )
  20. func TestValidateURLsValueBad(t *testing.T) {
  21. tests := []string{
  22. // bad IP specification
  23. ":2379",
  24. "127.0:8080",
  25. "123:456",
  26. // bad port specification
  27. "127.0.0.1:foo",
  28. "127.0.0.1:",
  29. // unix sockets not supported
  30. "unix://",
  31. "unix://tmp/etcd.sock",
  32. // bad strings
  33. "somewhere",
  34. "234#$",
  35. "file://foo/bar",
  36. "http://hello/asdf",
  37. "http://10.1.1.1",
  38. }
  39. for i, in := range tests {
  40. u := URLsValue{}
  41. if err := u.Set(in); err == nil {
  42. t.Errorf(`#%d: unexpected nil error for in=%q`, i, in)
  43. }
  44. }
  45. }
  46. func TestNewURLsValue(t *testing.T) {
  47. tests := []struct {
  48. s string
  49. exp []url.URL
  50. }{
  51. {s: "https://1.2.3.4:8080", exp: []url.URL{{Scheme: "https", Host: "1.2.3.4:8080"}}},
  52. {s: "http://10.1.1.1:80", exp: []url.URL{{Scheme: "http", Host: "10.1.1.1:80"}}},
  53. {s: "http://localhost:80", exp: []url.URL{{Scheme: "http", Host: "localhost:80"}}},
  54. {s: "http://:80", exp: []url.URL{{Scheme: "http", Host: ":80"}}},
  55. {
  56. s: "http://localhost:1,https://localhost:2",
  57. exp: []url.URL{
  58. {Scheme: "http", Host: "localhost:1"},
  59. {Scheme: "https", Host: "localhost:2"},
  60. },
  61. },
  62. }
  63. for i := range tests {
  64. uu := []url.URL(*NewURLsValue(tests[i].s))
  65. if !reflect.DeepEqual(tests[i].exp, uu) {
  66. t.Fatalf("#%d: expected %+v, got %+v", i, tests[i].exp, uu)
  67. }
  68. }
  69. }