-
Notifications
You must be signed in to change notification settings - Fork 15
/
signer_test.go
76 lines (58 loc) · 1.62 KB
/
signer_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
package httpsignatures
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSignSha1(t *testing.T) {
r := &http.Request{
Header: http.Header{
"Date": []string{"Thu, 05 Jan 2012 21:31:40 GMT"},
},
}
err := DefaultSha1Signer.SignRequest(TEST_KEY_ID, TEST_KEY, r)
assert.Nil(t, err)
s, err := FromRequest(r)
assert.Nil(t, err)
assert.Equal(t, TEST_KEY_ID, s.KeyID)
assert.Equal(t, DefaultSha1Signer.algorithm, s.Algorithm)
assert.Equal(t, DefaultSha1Signer.headers, s.Headers)
assert.Equal(t,
"RIdBXxLb6gWsu3bZtq3rQWSR1nk=",
s.Signature,
)
}
func TestSignSha256(t *testing.T) {
r := &http.Request{
Header: http.Header{
"Date": []string{"Thu, 05 Jan 2012 21:31:40 GMT"},
},
}
err := DefaultSha256Signer.SignRequest(TEST_KEY_ID, TEST_KEY, r)
assert.Nil(t, err)
s, err := FromRequest(r)
assert.Nil(t, err)
assert.Equal(t, TEST_KEY_ID, s.KeyID)
assert.Equal(t, DefaultSha256Signer.algorithm, s.Algorithm)
assert.Equal(t, DefaultSha256Signer.headers, s.Headers)
assert.Equal(t,
"mIX1nFtRDhvv8HIUSNpE3NQZZ6EIY98ObNkJM+Oq7AU=",
s.Signature,
)
}
func TestSignWithMissingDateHeader(t *testing.T) {
r := &http.Request{Header: http.Header{}}
err := DefaultSha1Signer.AuthRequest(TEST_KEY_ID, TEST_KEY, r)
assert.Nil(t, err)
assert.NotEqual(t, "", r.Header.Get("date"))
}
func TestSignWithMissingHeader(t *testing.T) {
r := &http.Request{
Header: http.Header{
"Date": []string{"Thu, 05 Jan 2012 21:31:40 GMT"},
},
}
s := NewSigner(AlgorithmHmacSha1, "foo")
err := s.SignRequest(TEST_KEY_ID, TEST_KEY, r)
assert.Equal(t, "Missing required header 'foo'", err.Error())
}