-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclient_test.go
74 lines (60 loc) · 1.41 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
74
package nordigen
import (
"context"
"net/http"
"os"
"sync"
"testing"
"time"
)
var (
sharedClient *Client
initOnce sync.Once
)
func initTestClient(t *testing.T) *Client {
id, idExists := os.LookupEnv("NORDIGEN_SECRET_ID")
key, keyExists := os.LookupEnv("NORDIGEN_SECRET_KEY")
if !idExists || !keyExists {
t.Skip("NORDIGEN_SECRET_ID and NORDIGEN_SECRET_KEY not set")
}
initOnce.Do(func() {
c := &Client{
c: &http.Client{Timeout: 60 * time.Second},
secretId: id,
secretKey: key,
m: &sync.RWMutex{},
}
c.c.Transport = Transport{rt: http.DefaultTransport, cli: c}
// Initialize the first token
err := c.newToken(context.Background())
if err != nil {
t.Fatalf("newToken: %s", err)
}
sharedClient = c
})
return sharedClient
}
func TestAccessRefresh(t *testing.T) {
c := initTestClient(t)
// Expire token immediately
c.token.AccessExpires = 0
ctx, cancel := context.WithCancel(context.Background())
go c.tokenHandler(ctx)
_, err := c.ListRequisitions()
if err != nil {
t.Fatalf("ListRequisitions: %s", err)
}
cancel() // Stop handler again
}
func TestRefreshRefresh(t *testing.T) {
c := initTestClient(t)
// Expire token immediately
c.token.RefreshExpires = 0
ctx, cancel := context.WithCancel(context.Background())
go c.tokenHandler(ctx)
_, err := c.ListRequisitions()
if err != nil {
t.Fatalf("ListRequisitions: %s", err)
}
cancel() // Stop handler again
}