forked from igungor/go-putio
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclient_test.go
66 lines (55 loc) · 1.32 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
package putio
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
var (
mux *http.ServeMux
server *httptest.Server
client *Client
)
func setup() {
mux = http.NewServeMux()
server = httptest.NewServer(mux)
client = NewClient(nil)
url, _ := url.Parse(server.URL)
client.BaseURL = url
}
func teardown() {
server.Close()
}
func testMethod(t *testing.T, r *http.Request, want string) {
if want != r.Method {
t.Errorf("got: %v, want: %v", r.Method, want)
}
}
func testHeader(t *testing.T, r *http.Request, key, value string) { // nolint
if r.Header.Get(key) != value {
t.Errorf("missing header. want: %q: %q", key, value)
}
}
func TestNewClient(t *testing.T) {
cl := NewClient(nil)
if cl.BaseURL.String() != defaultBaseURL {
t.Errorf("got: %v, want: %v", cl.BaseURL.String(), defaultBaseURL)
}
}
func TestNewRequest_badURL(t *testing.T) {
cl := NewClient(nil)
_, err := cl.NewRequest(context.Background(), http.MethodGet, ":", nil)
if err == nil {
t.Errorf("bad URL accepted")
}
}
func TestNewRequest_customUserAgent(t *testing.T) {
userAgent := "test"
cl := NewClient(nil)
cl.UserAgent = userAgent
req, _ := cl.NewRequest(context.Background(), http.MethodGet, "/test", nil)
if got := req.Header.Get("User-Agent"); got != userAgent {
t.Errorf("got: %v, want: %v", got, userAgent)
}
}