package s3
import (
"context"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
"craftdesign.group/quarry/internal/outbound/outboundtest"
)
// A bucket's endpoint is a user's, so the client a caller does not
// supply is the guarded one: a loopback endpoint is not reached unless
// the operator allowed it.
func TestDefaultClientRefusesAnEndpointInsideTheNetwork(t *testing.T) {
var hits atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { hits.Add(1) }))
defer srv.Close()
b := Bucket{Endpoint: srv.URL, Name: "files", Region: "auto", AccessKey: "AK", Secret: "SK"}
outboundtest.Set(t)
if _, _, err := b.Get(context.Background(), nil, "k", time.Now()); err == nil {
t.Errorf("FAIL: a loopback bucket was read with no allowance")
}
if err := b.Probe(context.Background(), nil, time.Now()); err == nil {
t.Errorf("FAIL: a loopback bucket was probed with no allowance")
}
if hits.Load() != 0 {
t.Errorf("FAIL: the endpoint was reached %d times", hits.Load())
}
if !t.Failed() {
t.Log("ok")
}
}