Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

48 rader
1.8 KiB

  1. package service
  2. import (
  3. "net/http"
  4. "testing"
  5. "github.com/QuantumNous/new-api/types"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestSmartDetectMimeTypeUsesTrustedHeaderBeforeFilenameAndURL(t *testing.T) {
  9. resp := &http.Response{Header: http.Header{
  10. "Content-Type": []string{"image/png; charset=binary"},
  11. "Content-Disposition": []string{`attachment; filename="report.pdf"`},
  12. }}
  13. require.Equal(t, "image/png", smartDetectMimeType(resp, "https://example.com/video.mp4", []byte("not-an-image")))
  14. }
  15. func TestSmartDetectMimeTypeFallsBackToFilenameAndURL(t *testing.T) {
  16. filenameResp := &http.Response{Header: http.Header{
  17. "Content-Type": []string{"application/octet-stream"},
  18. "Content-Disposition": []string{`attachment; filename="photo.JPEG"`},
  19. }}
  20. urlResp := &http.Response{Header: http.Header{"Content-Type": []string{"application/octet-stream"}}}
  21. require.Equal(t, "image/jpeg", smartDetectMimeType(filenameResp, "https://example.com/archive.bin", nil))
  22. require.Equal(t, "application/pdf", smartDetectMimeType(urlResp, "https://example.com/report.PDF?download=1", nil))
  23. }
  24. func TestLoadFromBase64HonorsExplicitMimeTypeAndRejectsInvalidData(t *testing.T) {
  25. cached, err := loadFromBase64("data:text/plain;base64,aGVsbG8=", "application/custom")
  26. require.NoError(t, err)
  27. require.Equal(t, "application/custom", cached.MimeType)
  28. require.EqualValues(t, 5, cached.Size)
  29. cached.Close()
  30. _, err = loadFromBase64("not-base64", "")
  31. require.Error(t, err)
  32. }
  33. func TestDetectFileType(t *testing.T) {
  34. require.Equal(t, types.FileTypeImage, DetectFileType("image/webp"))
  35. require.Equal(t, types.FileTypeAudio, DetectFileType("audio/mpeg"))
  36. require.Equal(t, types.FileTypeVideo, DetectFileType("video/mp4"))
  37. require.Equal(t, types.FileTypeFile, DetectFileType("application/pdf"))
  38. }