forked from longtao.wu/droboticflow
51 lines
1.7 KiB
Go
51 lines
1.7 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"droboticflow/internal/store"
|
|
|
|
"github.com/baidubce/bce-sdk-go/bce"
|
|
)
|
|
|
|
func TestExactObjectStorageKeyPreservesTrailingSlash(t *testing.T) {
|
|
if got := exactObjectStorageKey(" algo/nested/ "); got != "algo/nested/" {
|
|
t.Fatalf("exactObjectStorageKey preserved key = %q, want %q", got, "algo/nested/")
|
|
}
|
|
}
|
|
|
|
func TestIsBOSNoSuchKeyRecognizesServiceError(t *testing.T) {
|
|
err := bce.NewBceServiceError("NoSuchKey", "The specified key does not exist.", "req-1", http.StatusNotFound)
|
|
if !isBOSNoSuchKey(err) {
|
|
t.Fatalf("expected NoSuchKey to be recognized")
|
|
}
|
|
}
|
|
|
|
func TestRenderObjectStorageWriteErrorTreatsBOSNoSuchKeyAsNotFound(t *testing.T) {
|
|
recorder := httptest.NewRecorder()
|
|
err := bce.NewBceServiceError("NoSuchKey", "The specified key does not exist.", "req-2", http.StatusNotFound)
|
|
|
|
renderObjectStorageWriteError(recorder, err)
|
|
|
|
if recorder.Code != http.StatusNotFound {
|
|
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusNotFound)
|
|
}
|
|
if !strings.Contains(recorder.Body.String(), "NoSuchKey") {
|
|
t.Fatalf("expected response to include NoSuchKey, got %s", recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestObjectStorageJSONPutRejectsOversizedBody(t *testing.T) {
|
|
body := `{"key":"big.txt","content":"` + strings.Repeat("x", int(objectStorageJSONPutMaxBytes)) + `"}`
|
|
handler := NewHandler(store.NewMemoryStore(), nil)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/object-storage/objects", strings.NewReader(body)))
|
|
|
|
if recorder.Code != http.StatusRequestEntityTooLarge {
|
|
t.Fatalf("expected oversized JSON put to return 413, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|