-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
transport_test.go
115 lines (104 loc) · 3.08 KB
/
transport_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package oauth1
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestTransport(t *testing.T) {
const (
expectedToken = "access_token"
expectedConsumerKey = "consumer_key"
expectedNonce = "some_nonce"
expectedSignatureMethod = "HMAC-SHA1"
expectedTimestamp = "123456789"
)
server := newMockServer(func(w http.ResponseWriter, req *http.Request) {
params := parseOAuthParamsOrFail(t, req.Header.Get("Authorization"))
assert.Equal(t, expectedToken, params[oauthTokenParam])
assert.Equal(t, expectedConsumerKey, params[oauthConsumerKeyParam])
assert.Equal(t, expectedNonce, params[oauthNonceParam])
assert.Equal(t, expectedSignatureMethod, params[oauthSignatureMethodParam])
assert.Equal(t, expectedTimestamp, params[oauthTimestampParam])
assert.Equal(t, defaultOauthVersion, params[oauthVersionParam])
// oauth_signature will vary, httptest.Server uses a random port
})
defer server.Close()
config := &Config{
ConsumerKey: expectedConsumerKey,
ConsumerSecret: "consumer_secret",
Noncer: &fixedNoncer{expectedNonce},
}
auther := &auther{
config: config,
clock: &fixedClock{time.Unix(123456789, 0)},
}
tr := &Transport{
source: StaticTokenSource(NewToken(expectedToken, "some_secret")),
auther: auther,
}
client := &http.Client{Transport: tr}
req, err := http.NewRequest("GET", server.URL, nil)
assert.Nil(t, err)
_, err = client.Do(req)
assert.Nil(t, err)
}
func TestTransport_defaultBaseTransport(t *testing.T) {
tr := &Transport{
Base: nil,
}
assert.Equal(t, http.DefaultTransport, tr.base())
}
func TestTransport_customBaseTransport(t *testing.T) {
expected := &http.Transport{}
tr := &Transport{
Base: expected,
}
assert.Equal(t, expected, tr.base())
}
func TestTransport_nilSource(t *testing.T) {
tr := &Transport{
source: nil,
auther: &auther{
config: &Config{Noncer: &fixedNoncer{"any_nonce"}},
clock: &fixedClock{time.Unix(123456789, 0)},
},
}
client := &http.Client{Transport: tr}
resp, err := client.Get("http://example.com")
assert.Nil(t, resp)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "oauth1: Transport's source is nil")
}
}
func TestTransport_emptySource(t *testing.T) {
tr := &Transport{
source: StaticTokenSource(nil),
auther: &auther{
config: &Config{Noncer: &fixedNoncer{"any_nonce"}},
clock: &fixedClock{time.Unix(123456789, 0)},
},
}
client := &http.Client{Transport: tr}
resp, err := client.Get("http://example.com")
assert.Nil(t, resp)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "oauth1: Token is nil")
}
}
func TestTransport_nilAuther(t *testing.T) {
tr := &Transport{
source: StaticTokenSource(&Token{}),
auther: nil,
}
client := &http.Client{Transport: tr}
resp, err := client.Get("http://example.com")
assert.Nil(t, resp)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "oauth1: Transport's auther is nil")
}
}
func newMockServer(handler func(w http.ResponseWriter, r *http.Request)) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(handler))
}