package s3
import (
"context"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
// Put and Get are the forge's own two verbs: the only place this
// package holds bytes. Asserted against a server that answers like a
// bucket, so the signature and the headers are exercised rather than
// assumed.
func TestPutSendsTheBytesAndGetReadsThemBack(t *testing.T) {
var got struct {
method, ctype, body, path string
length int64
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodPut {
raw, _ := io.ReadAll(req.Body)
got.method, got.ctype, got.body = req.Method, req.Header.Get("Content-Type"), string(raw)
got.path, got.length = req.URL.Path, req.ContentLength
w.WriteHeader(http.StatusOK)
return
}
w.Header().Set("Content-Type", "image/png")
_, _ = io.WriteString(w, "PNGBYTES")
}))
defer srv.Close()
b := Bucket{Endpoint: srv.URL, Name: "forge", Region: "auto",
AccessKey: "k", Secret: "s"}
now := time.Now().UTC()
if err := b.Put(context.Background(), srv.Client(),
"avatars/quarry", "image/png", []byte("PNGBYTES"), now); err != nil {
t.Fatalf("FAIL: put: %v", err)
}
if got.method != http.MethodPut || got.body != "PNGBYTES" {
t.Errorf("FAIL: the bucket saw %s %q", got.method, got.body)
}
if got.ctype != "image/png" {
t.Errorf("FAIL: content type = %q", got.ctype)
}
// Signed with content-length, so a bucket that enforces it accepts.
if got.length != int64(len("PNGBYTES")) {
t.Errorf("FAIL: content length = %d", got.length)
}
if !strings.Contains(got.path, "avatars/quarry") {
t.Errorf("FAIL: key not in the path: %q", got.path)
}
raw, ctype, err := b.Get(context.Background(), srv.Client(), "avatars/quarry", now)
if err != nil {
t.Fatalf("FAIL: get: %v", err)
}
if string(raw) != "PNGBYTES" || ctype != "image/png" {
t.Errorf("FAIL: got %q %q", raw, ctype)
}
}
// A bucket that answers with something enormous must not make the
// forge allocate without bound.
func TestGetRefusesAnObjectLargerThanTheBound(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "image/png")
// One byte past the bound, streamed rather than allocated here.
for n := int64(0); n <= maxForgeObject; n += 1 << 16 {
_, _ = w.Write(make([]byte, 1<<16))
}
}))
defer srv.Close()
b := Bucket{Endpoint: srv.URL, Name: "forge", Region: "auto", AccessKey: "k", Secret: "s"}
if _, _, err := b.Get(context.Background(), srv.Client(), "avatars/big", time.Now().UTC()); err == nil {
t.Error("FAIL: an oversized object was read whole")
}
}
// A non-2xx is an error naming the key, not an empty success.
func TestGetReportsARefusal(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusForbidden)
}))
defer srv.Close()
b := Bucket{Endpoint: srv.URL, Name: "forge", Region: "auto", AccessKey: "k", Secret: "s"}
_, _, err := b.Get(context.Background(), srv.Client(), "avatars/quarry", time.Now().UTC())
if err == nil || !strings.Contains(err.Error(), "avatars/quarry") {
t.Errorf("FAIL: refusal reported as %v", err)
}
}