-
Notifications
You must be signed in to change notification settings - Fork 3
/
applemaps_test.go
105 lines (93 loc) · 2.81 KB
/
applemaps_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
package applemaps
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const (
accessToken_UnauthorizedResponse string = `{"error":{"message":"Not Authorized","details":[]}}`
accessToken_SuccessResponse string = `{"accessToken":"thisis.thejwt.token","expiresInSeconds":1800}`
)
func TestGetAccessToken_Unauthorized(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=utf8")
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(accessToken_UnauthorizedResponse))
}))
defer testServer.Close()
// manually instantiating the client here to test the unexported getAccessToken() method in isolation
mapsClient := &client{
testServer.Client(),
AccessToken{
Token: "jwt",
Expiration: 1800,
},
"",
time.Now(),
apiBase,
}
_, err := mapsClient.getAccessToken()
assert.Error(t, err)
assert.Empty(t, mapsClient.authToken)
}
func TestGetAccessToken_NoRefresh(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
defer testServer.Close()
// manually instantiating the client here to test the unexported getAccessToken() method in isolation
nextRenewal := time.Now().Add(30 * time.Second)
mapsClient := &client{
testServer.Client(),
AccessToken{
Token: "the.old.jwt",
Expiration: 1800,
},
"authToken",
nextRenewal,
testServer.URL,
}
_, err := mapsClient.getAccessToken()
assert.NoError(t, err)
assert.Equal(t, "the.old.jwt", mapsClient.accessToken.Token)
assert.Equal(t, nextRenewal, mapsClient.nextRenewal)
}
func TestGetAccessToken_Refresh(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=utf8")
w.WriteHeader(http.StatusOK)
w.Write([]byte(accessToken_SuccessResponse))
}))
defer testServer.Close()
// manually instantiating the client here to test the unexported getAccessToken() method in isolation
nextRenewal := time.Now().Add(5 * time.Second)
mapsClient := &client{
testServer.Client(),
AccessToken{
Token: "the.old.jwt",
Expiration: 1800,
},
"authToken",
nextRenewal,
testServer.URL,
}
tok, err := mapsClient.getAccessToken()
assert.NoError(t, err)
assert.Equal(t, "thisis.thejwt.token", tok)
assert.Equal(t, "thisis.thejwt.token", mapsClient.accessToken.Token)
assert.NotEqual(t, nextRenewal, mapsClient.nextRenewal)
}
func TestSetAuthToken(t *testing.T) {
mapsClient := &client{
http.DefaultClient,
AccessToken{
Token: "jwt",
Expiration: 1800,
},
"old-token",
time.Now(),
"url",
}
mapsClient.SetAuthToken("new-token")
assert.Equal(t, "new-token", mapsClient.authToken)
}