forked from tencentyun/cos-go-sdk-v5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_test.go
54 lines (46 loc) · 1.7 KB
/
auth_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 cos
import (
"context"
"net/http"
"testing"
"time"
)
func TestNewAuthorization(t *testing.T) {
expectAuthorization := `q-sign-algorithm=sha1&q-ak=QmFzZTY0IGlzIGEgZ2VuZXJp&q-sign-time=1480932292;1481012292&q-key-time=1480932292;1481012292&q-header-list=host;x-cos-content-sha1;x-cos-stroage-class&q-url-param-list=&q-signature=ce4ac0ecbcdb30538b3fee0a97cc6389694ce53a`
secretID := "QmFzZTY0IGlzIGEgZ2VuZXJp"
secretKey := "AKIDZfbOA78asKUYBcXFrJD0a1ICvR98JM"
host := "testbucket-125000000.cos.ap-guangzhou.myqcloud.com"
uri := "http://testbucket-125000000.cos.ap-guangzhou.myqcloud.com/testfile2"
startTime := time.Unix(int64(1480932292), 0)
endTime := time.Unix(int64(1481012292), 0)
req, _ := http.NewRequest("PUT", uri, nil)
req.Header.Add("Host", host)
req.Header.Add("x-cos-content-sha1", "db8ac1c259eb89d4a131b253bacfca5f319d54f2")
req.Header.Add("x-cos-stroage-class", "nearline")
authTime := &AuthTime{
SignStartTime: startTime,
SignEndTime: endTime,
KeyStartTime: startTime,
KeyEndTime: endTime,
}
auth := newAuthorization(secretID, secretKey, req, authTime)
if auth != expectAuthorization {
t.Errorf("NewAuthorization returned \n%#v, want \n%#v", auth, expectAuthorization)
}
}
func TestAuthorizationTransport(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
if auth == "" {
t.Error("AuthorizationTransport didn't add Authorization header")
}
})
client.client.Transport = &AuthorizationTransport{
SecretID: "test",
SecretKey: "test",
}
req, _ := http.NewRequest("GET", client.BaseURL.BucketURL.String(), nil)
client.doAPI(context.Background(), req, nil, true)
}