-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhttp_test.go
184 lines (150 loc) · 4.05 KB
/
http_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package megos
import (
"fmt"
"net/http"
"net/url"
"reflect"
"testing"
)
func TestGetHTTPResponse(t *testing.T) {
setup()
defer teardown()
mux1.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, "Test success")
})
r, err := client.GetHTTPResponse(client.Master[0])
if r == nil {
t.Errorf("Response is nil. Expected valid http.Response object, got nil")
}
if r.StatusCode != 200 {
t.Errorf("Response Statuscode is not 200. Expected 200, got %d", r.StatusCode)
}
if err != nil {
t.Errorf("err is not nil. Expected nil, got %s", err)
}
}
func TestGetHTTPResponse_WithError(t *testing.T) {
setup()
defer teardown()
u, _ := url.Parse("https://not-existing.example.org/")
r, err := client.GetHTTPResponse(u)
if r != nil {
t.Errorf("Response is not nil. Expected nil, got %+v", r)
}
if err == nil {
t.Errorf("err is nil. Expected error, got nil")
}
}
func TestGetBodyOfHTTPResponse(t *testing.T) {
setup()
defer teardown()
body := "Test success"
byteBody := []byte(body)
mux1.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, body)
})
b, err := client.GetBodyOfHTTPResponse(client.Master[0])
if !reflect.DeepEqual(b, byteBody) {
t.Errorf("Response body is not equal. Expected %+v, got %+v", byteBody, b)
}
if err != nil {
t.Errorf("err is not nil. Expected nil, got %s", err)
}
}
func TestGetBodyOfHTTPResponse_WithError(t *testing.T) {
setup()
defer teardown()
expected := []byte{}
u, _ := url.Parse("https://not-existing.example.org/")
b, err := client.GetBodyOfHTTPResponse(u)
if !reflect.DeepEqual(b, expected) {
t.Errorf("Response body is not equal. Expected %+v, got %+v", expected, b)
}
if err == nil {
t.Errorf("err is nil. Expected error, got nil")
}
}
func TestGetHTTPResponseFromCluster_NoMaster(t *testing.T) {
setup()
defer teardown()
testFunc := func(u url.URL) url.URL {
v, _ := url.Parse("https://not-existing.example.org/")
return *v
}
resp, err := client.GetHTTPResponseFromCluster(testFunc)
if resp != nil {
t.Errorf("Response is not nil. Expected nil, got %+v", resp)
}
if err == nil {
t.Error("Error is nil. Expected an error (No master online.).")
}
}
func TestGetHTTPResponseFromCluster(t *testing.T) {
setup()
defer teardown()
mux2.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, "content")
})
i := 0
testFunc := func(u url.URL) url.URL {
i++
// The first node should fail
// With this we can ensure that more than one node is requested in failure case
if i == 1 {
v, _ := url.Parse("http://not-existing.example.org/")
return *v
}
u.Path = "master/state"
return u
}
resp, err := client.GetHTTPResponseFromCluster(testFunc)
if resp == nil {
t.Errorf("Response is nil. Expected valid response, got nil")
}
if resp.StatusCode != 200 {
t.Errorf("Response status code is not 200. Expected 200, got %d", resp.StatusCode)
}
if err != nil {
t.Errorf("Error is not nil. Expected nil, got %s.", err)
}
}
func TestGetHTTPResponseFromLeader_NoLeader(t *testing.T) {
setup()
defer teardown()
testFunc := func(p Pid) url.URL {
return url.URL{}
}
resp, err := client.GetHTTPResponseFromLeader(testFunc)
if resp != nil {
t.Errorf("Response is not nil. Expected nil, got %+v", resp)
}
if err == nil {
t.Error("Error is nil. Expected an error (No leader set.).")
}
}
func TestGetHTTPResponseFromLeader(t *testing.T) {
setup()
defer teardown()
mux1.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, "content")
})
client.Leader = &Pid{}
testFunc := func(p Pid) url.URL {
u, _ := url.Parse(server1.URL)
return *u
}
resp, err := client.GetHTTPResponseFromLeader(testFunc)
if resp == nil {
t.Errorf("Response is nil. Expected valid response, got nil")
}
if resp.StatusCode != 200 {
t.Errorf("Response status code is not 200. Expected 200, got %d", resp.StatusCode)
}
if err != nil {
t.Errorf("Error is not nil. Expected nil, got %s.", err)
}
}