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
|
package s3
import (
"context"
"net/http"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
)
type mockHeadObject struct {
expires string
}
func (m *mockHeadObject) Do(r *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Header: http.Header{
"Expires": {m.expires},
},
Body: http.NoBody,
}, nil
}
func TestInvalidExpires(t *testing.T) {
expires := "2023-11-01"
svc := New(Options{
HTTPClient: &mockHeadObject{expires},
Region: "us-east-1",
})
out, err := svc.HeadObject(context.Background(), &HeadObjectInput{
Bucket: aws.String("bucket"),
Key: aws.String("key"),
})
if err != nil {
t.Fatal(err)
}
if out.Expires != nil {
t.Errorf("out.Expires should be nil, is %s", *out.Expires)
}
if aws.ToString(out.ExpiresString) != expires {
t.Errorf("out.ExpiresString should be %s, is %s", expires, *out.ExpiresString)
}
}
func TestValidExpires(t *testing.T) {
exs := "Mon, 02 Jan 2006 15:04:05 GMT"
ext, err := time.Parse(exs, exs)
if err != nil {
t.Fatal(err)
}
svc := New(Options{
HTTPClient: &mockHeadObject{exs},
Region: "us-east-1",
})
out, err := svc.HeadObject(context.Background(), &HeadObjectInput{
Bucket: aws.String("bucket"),
Key: aws.String("key"),
})
if err != nil {
t.Fatal(err)
}
if aws.ToTime(out.Expires) != ext {
t.Errorf("out.Expires should be %s, is %s", ext, *out.Expires)
}
if aws.ToString(out.ExpiresString) != exs {
t.Errorf("out.ExpiresString should be %s, is %s", exs, *out.ExpiresString)
}
}
|