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

  1. package cos
  2. import (
  3. "context"
  4. "net/http"
  5. "testing"
  6. "time"
  7. )
  8. func TestNewAuthorization(t *testing.T) {
  9. 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`
  10. secretID := "QmFzZTY0IGlzIGEgZ2VuZXJp"
  11. secretKey := "AKIDZfbOA78asKUYBcXFrJD0a1ICvR98JM"
  12. host := "testbucket-125000000.cos.ap-guangzhou.myqcloud.com"
  13. uri := "http://testbucket-125000000.cos.ap-guangzhou.myqcloud.com/testfile2"
  14. startTime := time.Unix(int64(1480932292), 0)
  15. endTime := time.Unix(int64(1481012292), 0)
  16. req, _ := http.NewRequest("PUT", uri, nil)
  17. req.Header.Add("Host", host)
  18. req.Header.Add("x-cos-content-sha1", "db8ac1c259eb89d4a131b253bacfca5f319d54f2")
  19. req.Header.Add("x-cos-stroage-class", "nearline")
  20. authTime := &AuthTime{
  21. SignStartTime: startTime,
  22. SignEndTime: endTime,
  23. KeyStartTime: startTime,
  24. KeyEndTime: endTime,
  25. }
  26. auth := newAuthorization(secretID, secretKey, req, authTime)
  27. if auth != expectAuthorization {
  28. t.Errorf("NewAuthorization returned \n%#v, want \n%#v", auth, expectAuthorization)
  29. }
  30. }
  31. func TestAuthorizationTransport(t *testing.T) {
  32. setup()
  33. defer teardown()
  34. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  35. auth := r.Header.Get("Authorization")
  36. if auth == "" {
  37. t.Error("AuthorizationTransport didn't add Authorization header")
  38. }
  39. })
  40. client.client.Transport = &AuthorizationTransport{}
  41. req, _ := http.NewRequest("GET", client.BaseURL.BucketURL.String(), nil)
  42. client.doAPI(context.Background(), req, nil, true)
  43. }