forked from adlio/trello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
73 lines (62 loc) · 1.53 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
// Copyright © 2016 Aaron Longwell
//
// Use of this source code is governed by an MIT license.
// Details in the LICENSE file.
package trello
import (
"context"
"net/http"
"testing"
)
func TestGetWithBadURL(t *testing.T) {
c := testClient()
target := map[string]interface{}{}
c.BaseURL = "gopher://test"
err := c.Get("members", Defaults(), &target)
if err == nil {
t.Fatal("Get() should fail with a bad URL")
}
}
func TestWithContext(t *testing.T) {
c := testClient()
if c.ctx != context.Background() {
t.Fatal("NewClient() should use context.Background()")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
newC := c.WithContext(ctx)
if newC == c {
t.Fatal("WithContext() should return a new client")
}
if newC.ctx != ctx {
t.Fatal("WithContext() should return a client with the given context")
}
var calls int
mt := &mockTransport{
RoundTripFunc: func(req *http.Request) (*http.Response, error) {
calls++
if req.Context() != ctx {
t.Fatal("Get() should be using the new context")
}
return http.DefaultTransport.RoundTrip(req)
},
}
newC.Client = &http.Client{
Transport: mt,
}
newC.Get("members", nil, nil)
if calls != 1 {
t.Fatal("Get() should have used the mocked transport")
}
}
type mockTransport struct {
RoundTripFunc func(*http.Request) (*http.Response, error)
}
func (t *mockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return t.RoundTripFunc(req)
}
func testClient() *Client {
c := NewClient("user", "pass")
c.testMode = true
return c
}