-
Notifications
You must be signed in to change notification settings - Fork 750
Expand file tree
/
Copy pathapi-stat_test.go
More file actions
207 lines (191 loc) · 7.07 KB
/
Copy pathapi-stat_test.go
File metadata and controls
207 lines (191 loc) · 7.07 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* MinIO Go Library for Amazon S3 Compatible Cloud Storage
* Copyright 2026 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package minio
import (
"context"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/minio/minio-go/v7/pkg/credentials"
)
// newTestStatClient returns a Client pointed at an httptest server that
// serves handler; the server is closed via t.Cleanup.
func newTestStatClient(t *testing.T, handler http.HandlerFunc) *Client {
t.Helper()
srv := httptest.NewServer(handler)
t.Cleanup(srv.Close)
clnt, err := New(srv.Listener.Addr().String(), &Options{
Creds: credentials.NewStaticV4("foo", "foo12345", ""),
Region: "us-east-1",
})
if err != nil {
t.Fatal(err)
}
return clnt
}
// Tests that StatObject returns the delete-marker ObjectInfo fields
// (VersionID and IsDeleteMarker — ReplicationReady is deliberately not
// merged into this return) and the MethodNotAllowed error code when a
// versioned HEAD hits a delete marker (HTTP 405).
func TestStatObjectDeleteMarker(t *testing.T) {
clnt := newTestStatClient(t, func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set(amzDeleteMarker, "true")
w.Header().Set(amzVersionID, "test-version-id")
w.Header().Set(minioTgtReplicationReady, "true")
w.WriteHeader(http.StatusMethodNotAllowed)
})
objInfo, err := clnt.StatObject(context.Background(), "bucket-name", "object-name",
StatObjectOptions{VersionID: "test-version-id"})
if err == nil {
t.Fatal("expected error for delete marker, got nil")
}
errResp := ToErrorResponse(err)
if errResp.Code != MethodNotAllowed {
t.Errorf("error code = %q, want %q", errResp.Code, MethodNotAllowed)
}
if errResp.StatusCode != http.StatusMethodNotAllowed {
t.Errorf("error status = %d, want %d", errResp.StatusCode, http.StatusMethodNotAllowed)
}
if errResp.BucketName != "bucket-name" || errResp.Key != "object-name" {
t.Errorf("error bucket/key = %q/%q, want %q/%q",
errResp.BucketName, errResp.Key, "bucket-name", "object-name")
}
if !objInfo.IsDeleteMarker {
t.Error("expected IsDeleteMarker to be true")
}
if objInfo.VersionID != "test-version-id" {
t.Errorf("VersionID = %q, want %q", objInfo.VersionID, "test-version-id")
}
if objInfo.ReplicationReady {
t.Error("expected ReplicationReady to stay false on the delete-marker return")
}
}
// Tests that a 405 response missing either half of the delete-marker
// shape (the x-amz-delete-marker header, or a version-targeted stat)
// falls through to the generic error path with the raw status code.
func TestStatObjectMethodNotAllowedGeneric(t *testing.T) {
const wantCode = "405 Method Not Allowed"
tests := []struct {
name string
deleteMarker bool
versionID string
}{
{"no delete-marker header", false, "test-version-id"},
{"no version id", true, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
clnt := newTestStatClient(t, func(w http.ResponseWriter, _ *http.Request) {
if tt.deleteMarker {
w.Header().Set(amzDeleteMarker, "true")
}
w.Header().Set(amzVersionID, "test-version-id")
w.WriteHeader(http.StatusMethodNotAllowed)
})
objInfo, err := clnt.StatObject(context.Background(), "bucket-name", "object-name",
StatObjectOptions{VersionID: tt.versionID})
if err == nil {
t.Fatal("expected error, got nil")
}
if errResp := ToErrorResponse(err); errResp.Code != wantCode {
t.Errorf("error code = %q, want %q", errResp.Code, wantCode)
}
if objInfo.IsDeleteMarker != tt.deleteMarker {
t.Errorf("IsDeleteMarker = %v, want %v", objInfo.IsDeleteMarker, tt.deleteMarker)
}
if objInfo.VersionID != "test-version-id" {
t.Errorf("VersionID = %q, want %q", objInfo.VersionID, "test-version-id")
}
})
}
}
// Tests that 202 and 204 responses, which executeMethod treats as
// success, are parsed like a 200 instead of being converted into errors.
func TestStatObjectNoContentSuccess(t *testing.T) {
for _, status := range []int{http.StatusAccepted, http.StatusNoContent} {
t.Run(http.StatusText(status), func(t *testing.T) {
clnt := newTestStatClient(t, func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Last-Modified", "Thu, 30 Jul 2026 00:00:00 GMT")
w.Header().Set("ETag", `"deadbeef"`)
w.Header().Set(amzVersionID, "test-version-id")
w.WriteHeader(status)
})
objInfo, err := clnt.StatObject(context.Background(), "bucket-name", "object-name", StatObjectOptions{})
if err != nil {
t.Fatalf("expected nil error for %d, got %v", status, err)
}
if objInfo.ETag != "deadbeef" {
t.Errorf("ETag = %q, want %q", objInfo.ETag, "deadbeef")
}
if objInfo.VersionID != "test-version-id" {
t.Errorf("VersionID = %q, want %q", objInfo.VersionID, "test-version-id")
}
})
}
}
// Tests that StatObject returns a zero ObjectInfo when the request fails
// before any response is received.
func TestStatObjectNoResponse(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}))
addr := srv.Listener.Addr().String()
srv.Close()
clnt, err := New(addr, &Options{
Creds: credentials.NewStaticV4("foo", "foo12345", ""),
Region: "us-east-1",
MaxRetries: 1,
})
if err != nil {
t.Fatal(err)
}
objInfo, err := clnt.StatObject(context.Background(), "bucket-name", "object-name", StatObjectOptions{})
if err == nil {
t.Fatal("expected error for unreachable endpoint, got nil")
}
if !reflect.DeepEqual(objInfo, ObjectInfo{}) {
t.Errorf("expected zero ObjectInfo, got %+v", objInfo)
}
}
// Tests that StatObject surfaces the delete-marker and replication-ready
// headers on a generic error response, e.g. HEAD on an object whose
// latest version is a delete marker (HTTP 404).
func TestStatObjectErrorHeaders(t *testing.T) {
clnt := newTestStatClient(t, func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set(amzDeleteMarker, "true")
w.Header().Set(amzVersionID, "test-version-id")
w.Header().Set(minioTgtReplicationReady, "true")
w.WriteHeader(http.StatusNotFound)
})
objInfo, err := clnt.StatObject(context.Background(), "bucket-name", "object-name", StatObjectOptions{})
if err == nil {
t.Fatal("expected error, got nil")
}
if errResp := ToErrorResponse(err); errResp.Code != NoSuchKey {
t.Errorf("error code = %q, want %q", errResp.Code, NoSuchKey)
}
if !objInfo.IsDeleteMarker {
t.Error("expected IsDeleteMarker to be true")
}
if objInfo.VersionID != "test-version-id" {
t.Errorf("VersionID = %q, want %q", objInfo.VersionID, "test-version-id")
}
if !objInfo.ReplicationReady {
t.Error("expected ReplicationReady to be true")
}
}