forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathoauth2_test.go
113 lines (93 loc) · 2.01 KB
/
oauth2_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
// Copyright 2018 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package oauth2
import (
"context"
"errors"
"net/http"
"testing"
"github.com/jenkins-x/go-scm/scm"
"gopkg.in/h2non/gock.v1"
)
func TestTransport(t *testing.T) {
defer gock.Off()
gock.New("https://api.github.com").
Get("/user").
MatchHeader("Authorization", "Bearer mF_9.B5f-4.1JqM").
Reply(200)
client := &http.Client{
Transport: &Transport{
Source: StaticTokenSource(
&scm.Token{
Token: "mF_9.B5f-4.1JqM",
},
),
},
}
res, err := client.Get("https://api.github.com/user")
if err != nil {
t.Error(err)
return
}
defer res.Body.Close()
}
func TestTransport_CustomScheme(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Get("/api/v1/user").
MatchHeader("Authorization", "token mF_9.B5f-4.1JqM").
Reply(200)
client := &http.Client{
Transport: &Transport{
Scheme: "token",
Source: StaticTokenSource(
&scm.Token{
Token: "mF_9.B5f-4.1JqM",
},
),
},
}
res, err := client.Get("https://try.gogs.io/api/v1/user")
if err != nil {
t.Error(err)
return
}
defer res.Body.Close()
}
func TestTransport_NoToken(t *testing.T) {
defer gock.Off()
gock.New("https://api.github.com").
Get("/user").
Reply(200)
client := &http.Client{
Transport: &Transport{
Source: ContextTokenSource(),
},
}
res, err := client.Get("https://api.github.com/user")
if err != nil {
t.Error(err)
return
}
defer res.Body.Close()
}
func TestTransport_TokenError(t *testing.T) {
want := errors.New("Cannot retrieve token")
client := &http.Client{
Transport: &Transport{
Source: mockErrorSource{want},
},
}
resp, err := client.Get("https://api.github.com/user")
if err == nil {
defer resp.Body.Close()
t.Errorf("Expect token source error, got nil")
}
}
type mockErrorSource struct {
err error
}
func (s mockErrorSource) Token(_ context.Context) (*scm.Token, error) {
return nil, s.err
}