-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
92 lines (76 loc) · 1.87 KB
/
client_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
package teamweek
import (
"net/http"
"net/url"
"testing"
)
func TestInvalidURL(t *testing.T) {
client = NewClient(nil)
err := client.get("/%s/error", nil)
if err == nil {
t.Errorf("Expected 'invalid URL escape' error")
}
}
func TestHandleHttpError(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Bad Request", 400)
})
err := client.get("/", nil)
if err == nil {
t.Errorf("Expected 'Bad Request' error")
}
}
func TestInvalidNewRequest(t *testing.T) {
client = NewClient(nil)
client.BaseURL = &url.URL{Host: "%s"}
err := client.get("/", nil)
if err == nil {
t.Error("Expected error to be returned.")
}
if err, ok := err.(*url.Error); !ok {
t.Errorf("Expected a URL error; got %#v.", err)
}
}
func TestHttpClientError(t *testing.T) {
client = NewClient(nil)
client.BaseURL = &url.URL{}
err := client.get("/", nil)
if err == nil {
t.Error("Expected error to be returned.")
}
if err, ok := err.(*url.Error); !ok {
t.Errorf("Expected a URL error; got %#v.", err)
}
}
func TestServiceInternalError(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Internal Server Error", 500)
})
if err := client.get("/", nil); err == nil {
t.Errorf("Expected 'Internal Server Error'")
}
}
func TestUnauthorizedError(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Unauthorized", 401)
})
if err := client.get("/", nil); err == nil {
t.Errorf("Expected 'Unauthorized'")
}
}
func TestUnexpectedStatus(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Accepted", 442)
})
if err := client.get("/", nil); err == nil {
t.Errorf("Unexpected status code error")
}
}