-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_status_test.go
68 lines (63 loc) · 1.75 KB
/
api_status_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
package encodingcom
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestAPIStatus(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte(`{"status":"Encoding Queue Processing Delays","status_code":"queue_slow","incident":"Our encoding queue is processing slower than normal. Check back for updates."}`))
}))
defer server.Close()
resp, err := APIStatus(server.URL)
if err != nil {
t.Fatal(err)
}
expectedResp := APIStatusResponse{
Status: "Encoding Queue Processing Delays",
StatusCode: "queue_slow",
Incident: "Our encoding queue is processing slower than normal. Check back for updates.",
}
if !reflect.DeepEqual(*resp, expectedResp) {
t.Errorf("wrong response returned\nwant %#v\ngot %#v", expectedResp, *resp)
}
}
func TestAPIStatusFailToConnect(t *testing.T) {
_, err := APIStatus("http://192.0.2.13:8080")
if err == nil {
t.Fatal("unexpected <nil> error")
}
}
func TestAPIStatusInvalidResponse(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte(`{not a valid json}`))
}))
defer server.Close()
_, err := APIStatus(server.URL)
if err == nil {
t.Fatal("unexpected <nil> error")
}
}
func TestAPIStatusOK(t *testing.T) {
var tests = []struct {
input string
want bool
}{
{"ok", true},
{"encoding_delay", false},
{"api_out", false},
{"maintenance", false},
{"pc_queue_slow", false},
}
for _, test := range tests {
test := test
t.Run(test.input, func(t *testing.T) {
status := APIStatusResponse{StatusCode: test.input}
got := status.OK()
if got != test.want {
t.Errorf("wrong status returned\nwant %v\ngot %v", test.want, got)
}
})
}
}