forked from abiosoft/caddy-hmac
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper_test.go
62 lines (54 loc) · 1.44 KB
/
helper_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
package hmac
import (
"net/http"
"testing"
)
func TestExtractHMACAndPath(t *testing.T) {
testCases := []struct {
name string
urlPath string
expectedHMAC string
expectedPath string
expectedQuery string
expectedError string
}{
{
name: "Valid URL",
urlPath: "/someSignature/some/path?foo=bar",
expectedHMAC: "someSignature",
expectedPath: "/some/path",
expectedQuery: "foo=bar",
expectedError: "",
},
{
name: "Invalid URL format",
urlPath: "/invalid-path",
expectedHMAC: "",
expectedPath: "",
expectedQuery: "",
expectedError: "invalid URL format",
},
// Add more test cases as needed
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
req, err := http.NewRequest("GET", "http://example.com"+tc.urlPath, nil)
if err != nil {
t.Fatal(err)
}
hmacSignature, remainingPath, query, err := extractHMACAndPath(req)
if err != nil && err.Error() != tc.expectedError {
t.Fatalf("Expected error: %s, got: %s", tc.expectedError, err.Error())
}
if hmacSignature != tc.expectedHMAC {
t.Errorf("Expected HMAC: %s, got: %s", tc.expectedHMAC, hmacSignature)
}
if remainingPath != tc.expectedPath {
t.Errorf("Expected path: %s, got: %s", tc.expectedPath, remainingPath)
}
if query != tc.expectedQuery {
t.Errorf("Expected query: %s, got: %s", tc.expectedQuery, query)
}
})
}
}