-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignature_test.go
129 lines (111 loc) · 4.49 KB
/
signature_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// +build !testing
package signature
import (
"crypto/md5"
"fmt"
"net/http"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
//
// buildRequest returns a new request for the given parameters
//
func buildRequest(url string, method string, payload map[string]string) *http.Request {
req, err := http.NewRequest(method, url, nil)
if err != nil {
panic(err)
}
return req
}
//
// dpTestNew provides data for TestNew
//
func dpTestNew() [][]string {
return [][]string{
{"1234", "4321"},
{"", ""},
{" ", " "},
{"test", ""},
{"", "test"},
{"test", "test"},
}
}
func TestNew(t *testing.T) {
req := buildRequest("https://api.myracloud.com/en/rapi/dnsRecords/", "GET", map[string]string{})
for ds, data := range dpTestNew() {
s := New(data[0], data[1], req)
assert.Equal(t, data[0], s.secret, fmt.Sprintf("Expected secret to be %s, got %s (dataset: %d)", data[0], s.secret, ds))
assert.Equal(t, data[1], s.apikey, fmt.Sprintf("Expected apikey to be %s, got %s (dataset: %d)", data[1], s.apikey, ds))
}
}
//
// dpTestAppend provides data for TestAppend
//
func dpTestAppend() []*Signature {
return []*Signature{
New("1234", "4321", buildRequest("https://api.myracloud.com/en/rapi/dnsRecords/", "GET", map[string]string{})),
New("1234", "4321", buildRequest("https://api.myracloud.com/en/rapi/dnsRecords/", "GET", map[string]string{"test": "me", "some": "parameter"})),
New("1234", "4321", buildRequest("https://api.myracloud.com/en/rapi/dnsRecords/", "POST", map[string]string{})),
New("1234", "4321", buildRequest("https://api.myracloud.com/en/rapi/dnsRecords/", "POST", map[string]string{"test": "me", "some": "parameter"})),
}
}
func TestAppend(t *testing.T) {
for ds, sig := range dpTestAppend() {
req, err := sig.Append()
if err != nil {
t.Errorf("Unexpected error for dataset %d: %s", ds, err.Error())
}
authorization := req.Header.Get("Authorization")
assert.NotEmpty(t, authorization, fmt.Sprintf("Missing 'Authorization' Header for dataset %d", ds))
contentType := req.Header.Get("Content-Type")
assert.NotEmpty(t, contentType, fmt.Sprintf("Missing 'Content-Type' Header for dataset %d", ds))
date := req.Header.Get("Date")
assert.NotEmpty(t, date, fmt.Sprintf("Missing 'Date' Header for dataset %d", ds))
_, err = time.Parse(time.RFC3339, date)
assert.NoError(t, err)
}
}
//
// dpTestSigningString provides data for TestSigningString
//
func dpTestSigningString() [][]string {
return [][]string{
{"this is the body", "GET", "/test/me", time.Now().Format(time.RFC3339)},
{"this is the body", "get", "/test/me", time.Now().Format(time.RFC3339)},
{"", "get", "/test/me?test=me", time.Now().Format(time.RFC3339)},
{"{\"test\":\"data\"}", "PoSt", "/test/me", time.Now().Format(time.RFC3339)},
{"", "PUT", "/test/me", time.Now().Format(time.RFC3339)},
{"{}", "PUT", "/test/me", time.Now().Format(time.RFC3339)},
}
}
func TestSigningString(t *testing.T) {
for ds, data := range dpTestSigningString() {
result := SigningString(data[0], data[1], data[2], data[3])
assert.NotEmpty(t, result, fmt.Sprintf("Missing signing string for dataset %d", ds))
assert.Contains(t, result, fmt.Sprintf("%x", md5.Sum([]byte(data[0]))), fmt.Sprintf("Missing body information in signing string for dataset %d", ds))
assert.Contains(t, result, strings.ToUpper(data[1]), fmt.Sprintf("Missing method information in signing string for dataset %d", ds))
assert.Contains(t, result, data[2], fmt.Sprintf("Missing path information in signing string for dataset %d", ds))
assert.Contains(t, result, data[3], fmt.Sprintf("Missing date information in signing string for dataset %d", ds))
}
}
//
// dpTestSignature provides data for TestSignature
//
func dpTestSignature() []*Signature {
return []*Signature{
New("1234", "4321", buildRequest("https://api.myracloud.com/en/rapi/dnsRecords/", "GET", map[string]string{})),
New("1234", "4321", buildRequest("https://api.myracloud.com/en/rapi/dnsRecords/", "GET", map[string]string{"test": "me", "some": "parameter"})),
New("1234", "4321", buildRequest("https://api.myracloud.com/en/rapi/dnsRecords/", "POST", map[string]string{})),
New("1234", "4321", buildRequest("https://api.myracloud.com/en/rapi/dnsRecords/", "POST", map[string]string{"test": "me", "some": "parameter"})),
}
}
func TestSignature(t *testing.T) {
for ds, sig := range dpTestSignature() {
date := time.Now().Format(time.RFC3339)
result, err := sig.Signature(date)
assert.NoError(t, err)
assert.NotEmpty(t, result, "Missing signature result for dataset %d", ds)
}
}