-
Notifications
You must be signed in to change notification settings - Fork 98
/
appsTransport_test.go
142 lines (122 loc) · 3.43 KB
/
appsTransport_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package ghinstallation
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
jwt "github.com/golang-jwt/jwt/v4"
"github.com/google/go-cmp/cmp"
)
func TestNewAppsTransportKeyFromFile(t *testing.T) {
tmpfile, err := os.CreateTemp("", "example")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name()) // clean up
if _, err := tmpfile.Write(key); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
_, err = NewAppsTransportKeyFromFile(&http.Transport{}, appID, tmpfile.Name())
if err != nil {
t.Fatal("unexpected error:", err)
}
}
type RoundTrip struct {
rt func(*http.Request) (*http.Response, error)
}
func (r RoundTrip) RoundTrip(req *http.Request) (*http.Response, error) {
return r.rt(req)
}
func TestAppsTransport(t *testing.T) {
customHeader := "my-header"
check := RoundTrip{
rt: func(req *http.Request) (*http.Response, error) {
h, ok := req.Header["Accept"]
if !ok {
t.Error("Header Accept not set")
}
want := []string{customHeader, acceptHeader}
if diff := cmp.Diff(want, h); diff != "" {
t.Errorf("HTTP Accept headers want->got: %s", diff)
}
return nil, nil
},
}
tr, err := NewAppsTransport(check, appID, key)
if err != nil {
t.Fatalf("error creating transport: %v", err)
}
if tr.appID != appID {
t.Errorf("appID want->got: %d->%d", appID, tr.appID)
}
req := httptest.NewRequest(http.MethodGet, "http://example.com", new(bytes.Buffer))
req.Header.Add("Accept", customHeader)
if _, err := tr.RoundTrip(req); err != nil {
t.Fatalf("error calling RoundTrip: %v", err)
}
}
func TestJWTExpiry(t *testing.T) {
key, err := jwt.ParseRSAPrivateKeyFromPEM(key)
if err != nil {
t.Fatal(err)
}
customHeader := "my-header"
check := RoundTrip{
rt: func(req *http.Request) (*http.Response, error) {
token := strings.Fields(req.Header.Get("Authorization"))[1]
tok, err := jwt.ParseWithClaims(token, &jwt.RegisteredClaims{}, func(t *jwt.Token) (interface{}, error) {
if t.Header["alg"] != "RS256" {
return nil, fmt.Errorf("unexpected signing method: %v, expected: %v", t.Header["alg"], "RS256")
}
return &key.PublicKey, nil
})
if err != nil {
t.Fatalf("jwt parse: %v", err)
}
c := tok.Claims.(*jwt.RegisteredClaims)
if c.ExpiresAt.IsZero() {
t.Fatalf("missing exp claim")
}
return nil, nil
},
}
tr := NewAppsTransportFromPrivateKey(check, appID, key)
req := httptest.NewRequest(http.MethodGet, "http://example.com", new(bytes.Buffer))
req.Header.Add("Accept", customHeader)
if _, err := tr.RoundTrip(req); err != nil {
t.Fatalf("error calling RoundTrip: %v", err)
}
}
func TestCustomSigner(t *testing.T) {
check := RoundTrip{
rt: func(req *http.Request) (*http.Response, error) {
h, ok := req.Header["Authorization"]
if !ok {
t.Error("Header Accept not set")
}
want := []string{"Bearer hunter2"}
if diff := cmp.Diff(want, h); diff != "" {
t.Errorf("HTTP Accept headers want->got: %s", diff)
}
return nil, nil
},
}
tr, err := NewAppsTransportWithOptions(check, appID, WithSigner(&noopSigner{}))
if err != nil {
t.Fatalf("NewAppsTransportWithOptions: %v", err)
}
req := httptest.NewRequest(http.MethodGet, "http://example.com", new(bytes.Buffer))
if _, err := tr.RoundTrip(req); err != nil {
t.Fatalf("error calling RoundTrip: %v", err)
}
}
type noopSigner struct{}
func (noopSigner) Sign(jwt.Claims) (string, error) {
return "hunter2", nil
}