-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhttp_test.go
257 lines (199 loc) · 5.84 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package adapters
import (
"context"
"fmt"
"net"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/overmindtech/cli/discovery"
)
const TestHTTPTimeout = 3 * time.Second
type TestHTTPServer struct {
TLSServer *httptest.Server
HTTPServer *httptest.Server
NotFoundPage string // A page that returns a 404
InternalServerErrorPage string // A page that returns a 500
RedirectPage string // A page that returns a 301
SlowPage string // A page that takes longer than the timeout to respond
OKPage string // A page that returns a 200
OKPageNoTLS string // A page that returns a 200 without TLS
Host string
Port string
}
func NewTestServer() (*TestHTTPServer, error) {
sm := http.NewServeMux()
sm.Handle("/404", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
_, err := w.Write([]byte("not found innit"))
if err != nil {
return
}
}))
sm.Handle("/500", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
_, err := w.Write([]byte("yeah nah innit"))
if err != nil {
return
}
}))
sm.Handle("/301", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Location", "https://www.google.com")
w.WriteHeader(301)
}))
sm.Handle("/200", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("ok innit"))
if err != nil {
return
}
}))
sm.Handle("/slow", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(500 * time.Millisecond)
_, err := w.Write([]byte("ok innit"))
if err != nil {
return
}
}))
tlsServer := httptest.NewTLSServer(sm)
httpServer := httptest.NewServer(sm)
host, port, err := net.SplitHostPort(tlsServer.Listener.Addr().String())
if err != nil {
return nil, err
}
return &TestHTTPServer{
TLSServer: tlsServer,
HTTPServer: httpServer,
NotFoundPage: fmt.Sprintf("%v/404", tlsServer.URL),
InternalServerErrorPage: fmt.Sprintf("%v/500", tlsServer.URL),
RedirectPage: fmt.Sprintf("%v/301", tlsServer.URL),
OKPage: fmt.Sprintf("%v/200", tlsServer.URL),
OKPageNoTLS: fmt.Sprintf("%v/200", httpServer.URL),
SlowPage: fmt.Sprintf("%v/slow", tlsServer.URL),
Host: host,
Port: port,
}, nil
}
func (t *TestHTTPServer) Close() {
if t.TLSServer != nil {
t.TLSServer.Close()
}
if t.HTTPServer != nil {
t.HTTPServer.Close()
}
}
func TestHTTPGet(t *testing.T) {
src := HTTPAdapter{}
server, err := NewTestServer()
if err != nil {
t.Fatal(err)
}
defer server.TLSServer.Close()
t.Run("With a specified port and dns name", func(t *testing.T) {
item, err := src.Get(context.Background(), "global", fmt.Sprintf("https://localhost:%v", server.Port), false)
if err != nil {
t.Fatal(err)
}
var dnsFound bool
for _, link := range item.GetLinkedItemQueries() {
switch link.GetQuery().GetType() {
case "dns":
dnsFound = true
if link.GetQuery().GetQuery() != "localhost" {
t.Errorf("expected dns query to be localhost, got %v", link.GetQuery())
}
}
}
if !dnsFound {
t.Error("link to dns not found")
}
discovery.TestValidateItem(t, item)
})
t.Run("With an IP", func(t *testing.T) {
item, err := src.Get(context.Background(), "global", server.OKPage, false)
if err != nil {
t.Fatal(err)
}
var ipFound bool
for _, link := range item.GetLinkedItemQueries() {
switch link.GetQuery().GetType() {
case "ip":
ipFound = true
if link.GetQuery().GetQuery() != "127.0.0.1" {
t.Errorf("expected dns query to be 127.0.0.1, got %v", link.GetQuery())
}
}
}
if !ipFound {
t.Error("link to ip not found")
}
discovery.TestValidateItem(t, item)
})
t.Run("With a 404", func(t *testing.T) {
item, err := src.Get(context.Background(), "global", server.NotFoundPage, false)
if err != nil {
t.Fatal(err)
}
var status interface{}
status, err = item.GetAttributes().Get("status")
if err != nil {
t.Fatal(err)
}
if status != float64(404) {
t.Errorf("expected status to be 404, got: %v", status)
}
discovery.TestValidateItem(t, item)
})
t.Run("With a timeout", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
item, err := src.Get(ctx, "global", server.SlowPage, false)
if err == nil {
t.Errorf("Expected timeout but got %v", item.String())
}
})
t.Run("With a 500 error", func(t *testing.T) {
item, err := src.Get(context.Background(), "global", server.InternalServerErrorPage, false)
if err != nil {
t.Fatal(err)
}
var status interface{}
status, err = item.GetAttributes().Get("status")
if err != nil {
t.Fatal(err)
}
if status != float64(500) {
t.Errorf("expected status to be 500, got: %v", status)
}
discovery.TestValidateItem(t, item)
})
t.Run("With a 301 redirect", func(t *testing.T) {
item, err := src.Get(context.Background(), "global", server.RedirectPage, false)
if err != nil {
t.Fatal(err)
}
var status interface{}
status, err = item.GetAttributes().Get("status")
if err != nil {
t.Fatal(err)
}
if status != float64(301) {
t.Errorf("expected status to be 301, got: %v", status)
}
if len(item.GetLinkedItemQueries()) == 0 {
t.Error("expected a linked item to redirected location, got none")
}
discovery.TestValidateItem(t, item)
})
t.Run("With no TLS", func(t *testing.T) {
item, err := src.Get(context.Background(), "global", server.OKPageNoTLS, false)
if err != nil {
t.Fatal(err)
}
_, err = item.GetAttributes().Get("tls")
if err == nil {
t.Error("Expected to not find TLS info")
}
discovery.TestValidateItem(t, item)
})
}