You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.7 KiB
51 lines
1.7 KiB
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{}
|
|
req, _ := http.NewRequest("GET", client.BaseURL.BucketURL.String(), nil)
|
|
client.doAPI(context.Background(), req, nil, true)
|
|
}
|