|
| 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 TestClientListObjectsUsesBucketScopedEndpoint(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 != "examplebucket-1250000000.cos.ap-guangzhou.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 got := r.URL.Query().Get("max-keys"); got != "100" { |
| 30 | + t.Fatalf("unexpected max-keys: %s", got) |
| 31 | + } |
| 32 | + if got := r.URL.Query().Get("marker"); got != "" { |
| 33 | + t.Fatalf("unexpected marker: %s", got) |
| 34 | + } |
| 35 | + if r.Header.Get("x-cos-security-token") != "TOKENEXAMPLE" { |
| 36 | + t.Fatalf("unexpected token header: %q", r.Header.Get("x-cos-security-token")) |
| 37 | + } |
| 38 | + authHeader := r.Header.Get("Authorization") |
| 39 | + if authHeader == "" { |
| 40 | + t.Fatal("missing authorization header") |
| 41 | + } |
| 42 | + if !strings.Contains(authHeader, "q-url-param-list=max-keys") { |
| 43 | + t.Fatalf("unexpected authorization header: %s", authHeader) |
| 44 | + } |
| 45 | + return &http.Response{ |
| 46 | + StatusCode: http.StatusOK, |
| 47 | + Header: make(http.Header), |
| 48 | + Body: io.NopCloser(strings.NewReader(` |
| 49 | +<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> |
| 50 | + <Name>examplebucket-1250000000</Name> |
| 51 | + <MaxKeys>100</MaxKeys> |
| 52 | + <IsTruncated>false</IsTruncated> |
| 53 | + <Contents> |
| 54 | + <Key>alpha.txt</Key> |
| 55 | + <Size>12</Size> |
| 56 | + </Contents> |
| 57 | +</ListBucketResult>`)), |
| 58 | + Request: r, |
| 59 | + }, nil |
| 60 | + }), |
| 61 | + }), |
| 62 | + WithRetryPolicy(api.RetryPolicy{MaxAttempts: 1}), |
| 63 | + WithClock(func() time.Time { return time.Date(2026, 4, 19, 12, 0, 0, 0, time.UTC) }), |
| 64 | + ) |
| 65 | + |
| 66 | + resp, err := client.ListObjects(context.Background(), "examplebucket-1250000000", "ap-guangzhou", "", 100) |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("ListObjects() error = %v", err) |
| 69 | + } |
| 70 | + if resp.Name != "examplebucket-1250000000" || len(resp.Objects) != 1 || resp.Objects[0].Key != "alpha.txt" || resp.Objects[0].Size != 12 { |
| 71 | + t.Fatalf("unexpected response: %+v", resp) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestDriverCountBucketObjectsPaginatesMarker(t *testing.T) { |
| 76 | + client := NewClient( |
| 77 | + auth.New("AKIDEXAMPLE", "SECRETKEYEXAMPLE", ""), |
| 78 | + WithHTTPClient(&http.Client{ |
| 79 | + Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { |
| 80 | + if r.URL.Host != "examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com" { |
| 81 | + t.Fatalf("unexpected host: %s", r.URL.Host) |
| 82 | + } |
| 83 | + switch r.URL.RawQuery { |
| 84 | + case "max-keys=1000": |
| 85 | + return &http.Response{ |
| 86 | + StatusCode: http.StatusOK, |
| 87 | + Header: make(http.Header), |
| 88 | + Body: io.NopCloser(strings.NewReader(` |
| 89 | +<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> |
| 90 | + <Name>examplebucket-1250000000</Name> |
| 91 | + <MaxKeys>1000</MaxKeys> |
| 92 | + <IsTruncated>true</IsTruncated> |
| 93 | + <Contents><Key>a.txt</Key><Size>1</Size></Contents> |
| 94 | + <Contents><Key>b.txt</Key><Size>2</Size></Contents> |
| 95 | +</ListBucketResult>`)), |
| 96 | + Request: r, |
| 97 | + }, nil |
| 98 | + case "marker=b.txt&max-keys=1000", "max-keys=1000&marker=b.txt": |
| 99 | + return &http.Response{ |
| 100 | + StatusCode: http.StatusOK, |
| 101 | + Header: make(http.Header), |
| 102 | + Body: io.NopCloser(strings.NewReader(` |
| 103 | +<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> |
| 104 | + <Name>examplebucket-1250000000</Name> |
| 105 | + <MaxKeys>1000</MaxKeys> |
| 106 | + <IsTruncated>false</IsTruncated> |
| 107 | + <Contents><Key>c.txt</Key><Size>3</Size></Contents> |
| 108 | +</ListBucketResult>`)), |
| 109 | + Request: r, |
| 110 | + }, nil |
| 111 | + default: |
| 112 | + t.Fatalf("unexpected query: %s", r.URL.RawQuery) |
| 113 | + return nil, nil |
| 114 | + } |
| 115 | + }), |
| 116 | + }), |
| 117 | + WithRetryPolicy(api.RetryPolicy{MaxAttempts: 1}), |
| 118 | + WithClock(func() time.Time { return time.Date(2026, 4, 19, 12, 0, 0, 0, time.UTC) }), |
| 119 | + ) |
| 120 | + |
| 121 | + driver := &Driver{ |
| 122 | + Credential: auth.New("AKIDEXAMPLE", "SECRETKEYEXAMPLE", ""), |
| 123 | + Client: client, |
| 124 | + } |
| 125 | + |
| 126 | + count, err := driver.countBucketObjects(context.Background(), "examplebucket-1250000000", "ap-guangzhou", nil) |
| 127 | + if err != nil { |
| 128 | + t.Fatalf("countBucketObjects() error = %v", err) |
| 129 | + } |
| 130 | + if count != 3 { |
| 131 | + t.Fatalf("unexpected object count: %d", count) |
| 132 | + } |
| 133 | +} |
0 commit comments