xtablo-source/backend/internal/files/store_unit_test.go
2026-05-15 19:57:46 +02:00

53 lines
1.5 KiB
Go

package files
import (
"bytes"
"io"
"net/http"
"strings"
"testing"
)
// TestUpload_ContentTypeSniff_PNG verifies that Upload sniffs "image/png" from
// a buffer that starts with the PNG magic bytes.
func TestUpload_ContentTypeSniff_PNG(t *testing.T) {
pngHeader := []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}
data := make([]byte, 600)
copy(data, pngHeader)
got := http.DetectContentType(data)
if got != "image/png" {
t.Errorf("DetectContentType(PNG) = %q; want %q", got, "image/png")
}
}
// TestUpload_ContentTypeSniff_SmallFile verifies that a file smaller than
// 512 bytes still yields a valid MIME type (no ErrUnexpectedEOF panic path).
func TestUpload_ContentTypeSniff_SmallFile(t *testing.T) {
data := []byte("hello world")
got := http.DetectContentType(data)
if got == "" {
t.Error("DetectContentType returned empty string for small file")
}
if !strings.HasPrefix(got, "text/") {
t.Errorf("DetectContentType(small text file) = %q; want text/* prefix", got)
}
}
// TestUpload_FullBodyPreserved verifies that io.ReadAll captures every byte
// from the input reader — no truncation at 512 bytes.
func TestUpload_FullBodyPreserved(t *testing.T) {
original := make([]byte, 1024)
for i := range original {
original[i] = byte(i % 256)
}
buf, err := io.ReadAll(bytes.NewReader(original))
if err != nil {
t.Fatalf("readAll: %v", err)
}
if !bytes.Equal(buf, original) {
t.Errorf("buffer length %d; want %d — bytes were dropped", len(buf), len(original))
}
}