|
| 1 | +package cos |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/404tk/cloudtoolkit/pkg/providers/tencent/api" |
| 12 | + "github.com/404tk/cloudtoolkit/pkg/providers/tencent/auth" |
| 13 | +) |
| 14 | + |
| 15 | +func TestDriverGetBucketsMapsServiceResponse(t *testing.T) { |
| 16 | + client := NewClient( |
| 17 | + auth.New("AKIDEXAMPLE", "SECRETKEYEXAMPLE", "TOKENEXAMPLE"), |
| 18 | + WithHTTPClient(&http.Client{ |
| 19 | + Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { |
| 20 | + if r.Method != http.MethodGet { |
| 21 | + t.Fatalf("unexpected method: %s", r.Method) |
| 22 | + } |
| 23 | + if r.URL.Host != "service.cos.myqcloud.com" { |
| 24 | + t.Fatalf("unexpected host: %s", r.URL.Host) |
| 25 | + } |
| 26 | + if r.URL.Path != "/" { |
| 27 | + t.Fatalf("unexpected path: %s", r.URL.Path) |
| 28 | + } |
| 29 | + if r.Header.Get("x-cos-security-token") != "TOKENEXAMPLE" { |
| 30 | + t.Fatalf("unexpected token header: %q", r.Header.Get("x-cos-security-token")) |
| 31 | + } |
| 32 | + authHeader := r.Header.Get("Authorization") |
| 33 | + if authHeader == "" { |
| 34 | + t.Fatal("missing authorization header") |
| 35 | + } |
| 36 | + if !strings.Contains(authHeader, "q-header-list=host;x-cos-security-token") { |
| 37 | + t.Fatalf("unexpected authorization header: %s", authHeader) |
| 38 | + } |
| 39 | + return &http.Response{ |
| 40 | + StatusCode: http.StatusOK, |
| 41 | + Header: make(http.Header), |
| 42 | + Body: io.NopCloser(strings.NewReader(`<ListAllMyBucketsResult> |
| 43 | + <Owner> |
| 44 | + <ID>xbaccxx</ID> |
| 45 | + <DisplayName>100000760461</DisplayName> |
| 46 | + </Owner> |
| 47 | + <Buckets> |
| 48 | + <Bucket> |
| 49 | + <Name>huadong-1253846586</Name> |
| 50 | + <Location>ap-shanghai</Location> |
| 51 | + <CreationDate>2017-06-16T13:08:28Z</CreationDate> |
| 52 | + </Bucket> |
| 53 | + <Bucket> |
| 54 | + <Name>huanan-1253846586</Name> |
| 55 | + <Location>ap-guangzhou</Location> |
| 56 | + <CreationDate>2017-06-10T09:00:07Z</CreationDate> |
| 57 | + </Bucket> |
| 58 | + </Buckets> |
| 59 | +</ListAllMyBucketsResult>`)), |
| 60 | + Request: r, |
| 61 | + }, nil |
| 62 | + }), |
| 63 | + }), |
| 64 | + WithRetryPolicy(api.RetryPolicy{MaxAttempts: 1}), |
| 65 | + WithClock(func() time.Time { return time.Date(2026, 4, 19, 12, 0, 0, 0, time.UTC) }), |
| 66 | + ) |
| 67 | + |
| 68 | + driver := &Driver{ |
| 69 | + Credential: auth.New("AKIDEXAMPLE", "SECRETKEYEXAMPLE", "TOKENEXAMPLE"), |
| 70 | + Client: client, |
| 71 | + } |
| 72 | + |
| 73 | + got, err := driver.GetBuckets(context.Background()) |
| 74 | + if err != nil { |
| 75 | + t.Fatalf("GetBuckets() error = %v", err) |
| 76 | + } |
| 77 | + if len(got) != 2 { |
| 78 | + t.Fatalf("unexpected bucket count: %d", len(got)) |
| 79 | + } |
| 80 | + if got[0].BucketName != "huadong-1253846586" || got[0].Region != "ap-shanghai" { |
| 81 | + t.Fatalf("unexpected first bucket: %+v", got[0]) |
| 82 | + } |
| 83 | + if got[1].BucketName != "huanan-1253846586" || got[1].Region != "ap-guangzhou" { |
| 84 | + t.Fatalf("unexpected second bucket: %+v", got[1]) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestDriverGetBucketsReturnsServiceError(t *testing.T) { |
| 89 | + client := NewClient( |
| 90 | + auth.New("AKIDEXAMPLE", "SECRETKEYEXAMPLE", ""), |
| 91 | + WithHTTPClient(&http.Client{ |
| 92 | + Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { |
| 93 | + return &http.Response{ |
| 94 | + StatusCode: http.StatusForbidden, |
| 95 | + Header: http.Header{ |
| 96 | + "X-Cos-Request-Id": []string{"req-1"}, |
| 97 | + }, |
| 98 | + Body: io.NopCloser(strings.NewReader(`<Error><Code>AccessDenied</Code><Message>denied</Message></Error>`)), |
| 99 | + Request: r, |
| 100 | + }, nil |
| 101 | + }), |
| 102 | + }), |
| 103 | + WithRetryPolicy(api.RetryPolicy{MaxAttempts: 1}), |
| 104 | + WithClock(func() time.Time { return time.Date(2026, 4, 19, 12, 0, 0, 0, time.UTC) }), |
| 105 | + ) |
| 106 | + |
| 107 | + driver := &Driver{ |
| 108 | + Credential: auth.New("AKIDEXAMPLE", "SECRETKEYEXAMPLE", ""), |
| 109 | + Client: client, |
| 110 | + } |
| 111 | + |
| 112 | + _, err := driver.GetBuckets(context.Background()) |
| 113 | + if err == nil { |
| 114 | + t.Fatal("expected service error") |
| 115 | + } |
| 116 | + if !strings.Contains(err.Error(), "AccessDenied") || !strings.Contains(err.Error(), "request_id=req-1") { |
| 117 | + t.Fatalf("unexpected error: %v", err) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +type roundTripFunc func(*http.Request) (*http.Response, error) |
| 122 | + |
| 123 | +func (fn roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { |
| 124 | + return fn(r) |
| 125 | +} |
0 commit comments