canonicalize_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import "testing"
  6. func TestCanonicalize(t *testing.T) {
  7. tests := []struct {
  8. Input []string
  9. Output []string
  10. }{
  11. // #0
  12. {
  13. Input: []string{"http://127.0.0.1/"},
  14. Output: []string{"http://127.0.0.1"},
  15. },
  16. // #1
  17. {
  18. Input: []string{"http://127.0.0.1:9200/", "gopher://golang.org/", "http://127.0.0.1:9201"},
  19. Output: []string{"http://127.0.0.1:9200", "http://127.0.0.1:9201"},
  20. },
  21. // #2
  22. {
  23. Input: []string{"http://user:secret@127.0.0.1/path?query=1#fragment"},
  24. Output: []string{"http://user:secret@127.0.0.1/path"},
  25. },
  26. // #3
  27. {
  28. Input: []string{"https://somewhere.on.mars:9999/path?query=1#fragment"},
  29. Output: []string{"https://somewhere.on.mars:9999/path"},
  30. },
  31. // #4
  32. {
  33. Input: []string{"https://prod1:9999/one?query=1#fragment", "https://prod2:9998/two?query=1#fragment"},
  34. Output: []string{"https://prod1:9999/one", "https://prod2:9998/two"},
  35. },
  36. // #5
  37. {
  38. Input: []string{"http://127.0.0.1/one/"},
  39. Output: []string{"http://127.0.0.1/one"},
  40. },
  41. // #6
  42. {
  43. Input: []string{"http://127.0.0.1/one///"},
  44. Output: []string{"http://127.0.0.1/one"},
  45. },
  46. // #7: Invalid URL
  47. {
  48. Input: []string{"127.0.0.1/"},
  49. Output: []string{},
  50. },
  51. // #8: Invalid URL
  52. {
  53. Input: []string{"127.0.0.1:9200"},
  54. Output: []string{},
  55. },
  56. }
  57. for i, test := range tests {
  58. got := canonicalize(test.Input...)
  59. if want, have := len(test.Output), len(got); want != have {
  60. t.Fatalf("#%d: expected %d elements; got: %d", i, want, have)
  61. }
  62. for i := 0; i < len(got); i++ {
  63. if want, have := test.Output[i], got[i]; want != have {
  64. t.Errorf("#%d: expected %q; got: %q", i, want, have)
  65. }
  66. }
  67. }
  68. }