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