Debian Codegraph @

canonicalize_test.go

raw
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
5 package elastic
6
7 import "testing"
8
9 func TestCanonicalize(t *testing.T) {
10 tests := []struct {
11 Input []string
12 Output []string
13 }{
14 // #0
15 {
16 Input: []string{"http://127.0.0.1/"},
17 Output: []string{"http://127.0.0.1"},
18 },
19 // #1
20 {
21 Input: []string{"http://127.0.0.1:9200/", "gopher://golang.org/", "http://127.0.0.1:9201"},
22 Output: []string{"http://127.0.0.1:9200", "http://127.0.0.1:9201"},
23 },
24 // #2
25 {
26 Input: []string{"http://user:[email protected]/path?query=1#fragment"},
27 Output: []string{"http://user:[email protected]/path"},
28 },
29 // #3
30 {
31 Input: []string{"https://somewhere.on.mars:9999/path?query=1#fragment"},
32 Output: []string{"https://somewhere.on.mars:9999/path"},
33 },
34 // #4
35 {
36 Input: []string{"https://prod1:9999/one?query=1#fragment", "https://prod2:9998/two?query=1#fragment"},
37 Output: []string{"https://prod1:9999/one", "https://prod2:9998/two"},
38 },
39 // #5
40 {
41 Input: []string{"http://127.0.0.1/one/"},
42 Output: []string{"http://127.0.0.1/one"},
43 },
44 // #6
45 {
46 Input: []string{"http://127.0.0.1/one///"},
47 Output: []string{"http://127.0.0.1/one"},
48 },
49 // #7: Invalid URL
50 {
51 Input: []string{"127.0.0.1/"},
52 Output: []string{},
53 },
54 // #8: Invalid URL
55 {
56 Input: []string{"127.0.0.1:9200"},
57 Output: []string{},
58 },
59 }
60
61 for i, test := range tests {
62 got := canonicalize(test.Input...)
63 if want, have := len(test.Output), len(got); want != have {
64 t.Fatalf("#%d: expected %d elements; got: %d", i, want, have)
65 }
66 for i := 0; i < len(got); i++ {
67 if want, have := test.Output[i], got[i]; want != have {
68 t.Errorf("#%d: expected %q; got: %q", i, want, have)
69 }
70 }
71 }
72 }