forked from brianstrauch/spotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts_test.go
54 lines (46 loc) · 1.24 KB
/
accounts_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
package spotify
import (
"net/url"
"regexp"
"strings"
"testing"
)
func TestCreatePKCEVerifierAndChallenge(t *testing.T) {
verifier, challenge, err := CreatePKCEVerifierAndChallenge()
if err != nil {
t.Fatal(err)
}
if !regexp.MustCompile(`^[[:alnum:]_.\-~]{128}$`).MatchString(verifier) {
t.Fatal("Verifier string does not match")
}
// Hash with SHA-256 (64 chars)
// Convert to Base64 (44 chars)
// Remove trailing = (43 chars)
if !regexp.MustCompile(`^[[:alnum:]\-_]{43}$`).MatchString(challenge) {
t.Fatal("Challenge string does not match")
}
}
func TestBuildPKCEAuthURI(t *testing.T) {
var (
clientID = "client"
redirectURI = "http://localhost:1024"
challenge = "challenge"
state = "state"
scope = "user-modify-playback-state"
)
uri := BuildPKCEAuthURI(clientID, redirectURI, challenge, state, scope)
substrings := []string{
"client_id=" + clientID,
"response_type=code",
"redirect_uri=" + url.QueryEscape(redirectURI),
"code_challenge_method=S256",
"code_challenge=" + challenge,
"state=" + state,
"scope=" + url.QueryEscape(scope),
}
for _, substring := range substrings {
if !strings.Contains(uri, substring) {
t.Fatalf("URI %s does not contain substring %s", uri, substring)
}
}
}