-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
url_test.go
137 lines (115 loc) · 4.48 KB
/
url_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// +build !integration
package elasticsearch
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUrlEncode(t *testing.T) {
params := map[string]string{
"q": "agent:appserver1",
}
url := urlEncode("", params)
if url != "q=agent%3Aappserver1" {
t.Errorf("Fail to encode params: %s", url)
}
params = map[string]string{
"wife": "sarah",
"husband": "joe",
}
url = urlEncode("", params)
if url != "husband=joe&wife=sarah" {
t.Errorf("Fail to encode params: %s", url)
}
}
func TestMakePath(t *testing.T) {
path, err := makePath("twitter", "tweet", "1")
if err != nil {
t.Errorf("Fail to create path: %s", err)
}
if path != "/twitter/tweet/1" {
t.Errorf("Wrong path created: %s", path)
}
path, err = makePath("twitter", "", "_refresh")
if err != nil {
t.Errorf("Fail to create path: %s", err)
}
if path != "/twitter/_refresh" {
t.Errorf("Wrong path created: %s", path)
}
path, err = makePath("", "", "_bulk")
if err != nil {
t.Errorf("Fail to create path: %s", err)
}
if path != "/_bulk" {
t.Errorf("Wrong path created: %s", path)
}
path, err = makePath("twitter", "", "")
if err != nil {
t.Errorf("Fail to create path: %s", err)
}
if path != "/twitter" {
t.Errorf("Wrong path created: %s", path)
}
}
func TestGetUrl(t *testing.T) {
// List of inputs / outputs that must match after fetching url
// Setting a path without a scheme is not allowed. Example: 192.168.1.1:9200/hello
inputOutput := map[string]string{
// shema + hostname
"": "http://localhost:9200",
"http://localhost": "http://localhost:9200",
"http://localhost:80": "http://localhost:80",
"http://localhost:80/": "http://localhost:80/",
"http://localhost/": "http://localhost:9200/",
// no schema + hostname
"33f3600fd5c1bb599af557c36a4efb08.host": "http://33f3600fd5c1bb599af557c36a4efb08.host:9200",
"33f3600fd5c1bb599af557c36a4efb08.host:12345": "http://33f3600fd5c1bb599af557c36a4efb08.host:12345",
"localhost": "http://localhost:9200",
"localhost:80": "http://localhost:80",
"localhost:80/": "http://localhost:80/",
"localhost/": "http://localhost:9200/",
"localhost/mypath": "http://localhost:9200/mypath",
// shema + ipv4
"http://192.168.1.1:80": "http://192.168.1.1:80",
"https://192.168.1.1:80/hello": "https://192.168.1.1:80/hello",
"http://192.168.1.1": "http://192.168.1.1:9200",
"http://192.168.1.1/hello": "http://192.168.1.1:9200/hello",
// no schema + ipv4
"192.168.1.1": "http://192.168.1.1:9200",
"192.168.1.1:80": "http://192.168.1.1:80",
"192.168.1.1/hello": "http://192.168.1.1:9200/hello",
"192.168.1.1:80/hello": "http://192.168.1.1:80/hello",
// schema + ipv6
"http://[2001:db8::1]:80": "http://[2001:db8::1]:80",
"http://[2001:db8::1]": "http://[2001:db8::1]:9200",
"https://[2001:db8::1]:9200": "https://[2001:db8::1]:9200",
"http://FE80:0000:0000:0000:0202:B3FF:FE1E:8329": "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:9200",
"http://[2001:db8::1]:80/hello": "http://[2001:db8::1]:80/hello",
"http://[2001:db8::1]/hello": "http://[2001:db8::1]:9200/hello",
"https://[2001:db8::1]:9200/hello": "https://[2001:db8::1]:9200/hello",
"http://FE80:0000:0000:0000:0202:B3FF:FE1E:8329/hello": "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:9200/hello",
// no schema + ipv6
"2001:db8::1": "http://[2001:db8::1]:9200",
"[2001:db8::1]:80": "http://[2001:db8::1]:80",
"[2001:db8::1]": "http://[2001:db8::1]:9200",
"2001:db8::1/hello": "http://[2001:db8::1]:9200/hello",
"[2001:db8::1]:80/hello": "http://[2001:db8::1]:80/hello",
"[2001:db8::1]/hello": "http://[2001:db8::1]:9200/hello",
}
for input, output := range inputOutput {
urlNew, err := MakeURL("", "", input)
assert.Nil(t, err)
assert.Equal(t, output, urlNew, fmt.Sprintf("input: %v", input))
}
inputOutputWithDefaults := map[string]string{
"http://localhost": "http://localhost:9200/hello",
"192.156.4.5": "https://192.156.4.5:9200/hello",
"http://username:password@es.found.io:9324": "http://username:password@es.found.io:9324/hello",
}
for input, output := range inputOutputWithDefaults {
urlNew, err := MakeURL("https", "/hello", input)
assert.Nil(t, err)
assert.Equal(t, output, urlNew)
}
}