-
Notifications
You must be signed in to change notification settings - Fork 7
/
path_formatter_test.go
45 lines (35 loc) · 1.1 KB
/
path_formatter_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
package surl
import (
"errors"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPathFormatter(t *testing.T) {
f := pathFormatter{}
// unsigned url with existing path
u := &url.URL{Path: "/foo/bar"}
expiry := time.Date(2081, time.February, 24, 4, 0, 0, 0, time.UTC)
encoded := stdIntEncoding(10).Encode(expiry.Unix())
f.addExpiry(u, encoded)
assert.Equal(t, "3507595200/foo/bar", u.Path)
f.addSignature(u, "abcdef")
assert.Equal(t, "/abcdef.3507595200/foo/bar", u.Path)
sig, err := f.extractSignature(u)
require.NoError(t, err)
assert.Equal(t, "abcdef", string(sig))
assert.Equal(t, "3507595200/foo/bar", u.Path)
got, err := f.extractExpiry(u)
require.NoError(t, err)
assert.Equal(t, encoded, got)
assert.Equal(t, "/foo/bar", u.Path)
}
func TestPathFormatter_Errors(t *testing.T) {
signer := New([]byte("abc123"), WithPathFormatter())
t.Run("invalid signature", func(t *testing.T) {
err := signer.Verify("http://abc.com/MICKEYMOUSE.123/foo/bar")
assert.Truef(t, errors.Is(err, ErrInvalidSignature), "got error: %w", err)
})
}