generated from sensu/check-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
net_test.go
84 lines (70 loc) · 1.76 KB
/
net_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
package main
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func TestReadHTTPGoodServer(t *testing.T) {
goodServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if _, err := w.Write(testDataCSV); err != nil {
panic(err)
}
}))
url, err := url.Parse(goodServer.URL)
if err != nil {
t.Fatal(err)
}
data, err := readHTTP(url, Config{})
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data.data, testDataCSV) {
fmt.Println(string(data.data))
t.Error("got bad data from test server")
}
}
func TestReadHTTPBadServer(t *testing.T) {
badServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
http.Error(w, "oh noooooo", http.StatusInternalServerError)
}))
url, err := url.Parse(badServer.URL)
if err != nil {
t.Fatal(err)
}
if _, err := readHTTP(url, Config{}); err == nil {
t.Fatal("expected non-nil error")
}
}
func TestReadHTTPAuthServer(t *testing.T) {
authServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
username, password, ok := req.BasicAuth()
if !ok {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
if username != "username" {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
if password != "password" {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
if _, err := w.Write(testDataCSV); err != nil {
panic(err)
}
}))
url, err := url.Parse(authServer.URL)
if err != nil {
t.Fatal(err)
}
if _, err := readHTTP(url, Config{}); err == nil {
t.Fatal("expected non-nil error")
}
if _, err := readHTTP(url, Config{AdminUser: "username", AdminPass: "password"}); err != nil {
t.Error(err)
}
}