-
Notifications
You must be signed in to change notification settings - Fork 3
/
expvars_test.go
99 lines (76 loc) · 2.15 KB
/
expvars_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
package main
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/antonholmquist/jason"
"github.com/stretchr/testify/assert"
)
var (
mux *http.ServeMux
server *httptest.Server
)
func tearUp() {
mux = http.NewServeMux()
server = httptest.NewServer(mux)
}
func tearDown() {
server.Close()
}
func ParseTestURL(t *testing.T, rawurl string) *url.URL {
url, err := url.Parse(rawurl)
assert.Nil(t, err)
return url
}
func TestFetcher_Fetch_Timeout(t *testing.T) {
tearUp()
defer tearDown()
mux.HandleFunc("/debug/vars", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(20 * time.Millisecond)
fmt.Fprint(w, "OK")
})
f := NewFetcher()
f.(*fetcher).client.Timeout = 10 * time.Microsecond
_, err := f.Fetch(*ParseTestURL(t, server.URL+"/debug/vars"))
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "(Client.Timeout exceeded while awaiting headers)")
}
func TestFetcher_Fetch_BadStatusCode(t *testing.T) {
tearUp()
defer tearDown()
mux.HandleFunc("/debug/vars", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadGateway)
fmt.Fprint(w, "ERROR")
})
f := NewFetcher()
_, err := f.Fetch(*ParseTestURL(t, server.URL+"/debug/vars"))
assert.Equal(t, errors.New("Could not fetch expvars from "+server.URL+"/debug/vars"), err)
}
func TestFetcher_Fetch_BadResponse(t *testing.T) {
tearUp()
defer tearDown()
mux.HandleFunc("/debug/vars", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "{")
})
f := NewFetcher()
_, err := f.Fetch(*ParseTestURL(t, server.URL+"/debug/vars"))
assert.Equal(t, errors.New("unexpected EOF"), err)
}
func TestFetcher_Fetch_Success(t *testing.T) {
tearUp()
defer tearDown()
expvarsResponse := `{"gauge": {"metric": 800}, "process": {"text": "text 1"}, "memstats": {"alloc": 123}}`
o, err := jason.NewObjectFromBytes([]byte(expvarsResponse))
assert.NoError(t, err)
mux.HandleFunc("/debug/vars", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, expvarsResponse)
})
f := NewFetcher()
vars, err := f.Fetch(*ParseTestURL(t, server.URL+"/debug/vars"))
assert.Nil(t, err)
assert.Equal(t, &Expvars{o}, vars)
}