registry_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package imagetype_new
  2. import (
  3. "testing"
  4. "github.com/imgproxy/imgproxy/v3/bufreader"
  5. "github.com/stretchr/testify/require"
  6. )
  7. func TestRegisterType(t *testing.T) {
  8. // Create a separate registry for testing to avoid conflicts with global registry
  9. testRegistry := &Registry{}
  10. // Register a custom type
  11. customDesc := &TypeDesc{
  12. String: "custom",
  13. Ext: ".custom",
  14. Mime: "image/custom",
  15. IsVector: false,
  16. SupportsAlpha: true,
  17. SupportsColourProfile: true,
  18. }
  19. customType := testRegistry.RegisterType(customDesc)
  20. // Verify the type is now registered
  21. result := testRegistry.GetTypeDesc(customType)
  22. require.NotNil(t, result)
  23. require.Equal(t, customDesc.String, result.String)
  24. require.Equal(t, customDesc.Ext, result.Ext)
  25. require.Equal(t, customDesc.Mime, result.Mime)
  26. require.Equal(t, customDesc.IsVector, result.IsVector)
  27. require.Equal(t, customDesc.SupportsAlpha, result.SupportsAlpha)
  28. require.Equal(t, customDesc.SupportsColourProfile, result.SupportsColourProfile)
  29. }
  30. func TestTypeProperties(t *testing.T) {
  31. // Test that Type methods use TypeDesc fields correctly
  32. tests := []struct {
  33. name string
  34. typ Type
  35. expectVector bool
  36. expectAlpha bool
  37. expectColourProfile bool
  38. expectQuality bool
  39. expectAnimationLoad bool
  40. expectAnimationSave bool
  41. expectThumbnail bool
  42. }{
  43. {
  44. name: "JPEG",
  45. typ: JPEG,
  46. expectVector: false,
  47. expectAlpha: false,
  48. expectColourProfile: true,
  49. expectQuality: true,
  50. expectAnimationLoad: false,
  51. expectAnimationSave: false,
  52. expectThumbnail: false,
  53. },
  54. {
  55. name: "PNG",
  56. typ: PNG,
  57. expectVector: false,
  58. expectAlpha: true,
  59. expectColourProfile: true,
  60. expectQuality: false,
  61. expectAnimationLoad: false,
  62. expectAnimationSave: false,
  63. expectThumbnail: false,
  64. },
  65. {
  66. name: "WEBP",
  67. typ: WEBP,
  68. expectVector: false,
  69. expectAlpha: true,
  70. expectColourProfile: true,
  71. expectQuality: true,
  72. expectAnimationLoad: true,
  73. expectAnimationSave: true,
  74. expectThumbnail: false,
  75. },
  76. {
  77. name: "SVG",
  78. typ: SVG,
  79. expectVector: true,
  80. expectAlpha: true,
  81. expectColourProfile: false,
  82. expectQuality: false,
  83. expectAnimationLoad: false,
  84. expectAnimationSave: false,
  85. expectThumbnail: false,
  86. },
  87. {
  88. name: "GIF",
  89. typ: GIF,
  90. expectVector: false,
  91. expectAlpha: true,
  92. expectColourProfile: false,
  93. expectQuality: false,
  94. expectAnimationLoad: true,
  95. expectAnimationSave: true,
  96. expectThumbnail: false,
  97. },
  98. {
  99. name: "HEIC",
  100. typ: HEIC,
  101. expectVector: false,
  102. expectAlpha: true,
  103. expectColourProfile: true,
  104. expectQuality: true,
  105. expectAnimationLoad: false,
  106. expectAnimationSave: false,
  107. expectThumbnail: true,
  108. },
  109. {
  110. name: "AVIF",
  111. typ: AVIF,
  112. expectVector: false,
  113. expectAlpha: true,
  114. expectColourProfile: true,
  115. expectQuality: true,
  116. expectAnimationLoad: false,
  117. expectAnimationSave: false,
  118. expectThumbnail: true,
  119. },
  120. }
  121. for _, tt := range tests {
  122. t.Run(tt.name, func(t *testing.T) {
  123. require.Equal(t, tt.expectVector, tt.typ.IsVector())
  124. require.Equal(t, tt.expectAlpha, tt.typ.SupportsAlpha())
  125. require.Equal(t, tt.expectColourProfile, tt.typ.SupportsColourProfile())
  126. require.Equal(t, tt.expectQuality, tt.typ.SupportsQuality())
  127. require.Equal(t, tt.expectAnimationLoad, tt.typ.SupportsAnimationLoad())
  128. require.Equal(t, tt.expectAnimationSave, tt.typ.SupportsAnimationSave())
  129. require.Equal(t, tt.expectThumbnail, tt.typ.SupportsThumbnail())
  130. })
  131. }
  132. }
  133. func TestRegisterDetector(t *testing.T) {
  134. // Create a test registry to avoid interfering with global state
  135. testRegistry := &Registry{}
  136. // Create a test detector function
  137. testDetector := func(r bufreader.ReadPeeker) (Type, error) {
  138. b, err := r.Peek(2)
  139. if err != nil {
  140. return Unknown, err
  141. }
  142. if len(b) >= 2 && b[0] == 0xFF && b[1] == 0xD8 {
  143. return JPEG, nil
  144. }
  145. return Unknown, newUnknownFormatError()
  146. }
  147. // Register the detector using the method
  148. testRegistry.RegisterDetector(testDetector)
  149. // Verify the detector is registered
  150. require.Len(t, testRegistry.detectors, 1)
  151. require.NotNil(t, testRegistry.detectors[0])
  152. }
  153. func TestRegisterMagicBytes(t *testing.T) {
  154. // Create a test registry to avoid interfering with global state
  155. testRegistry := &Registry{}
  156. require.Empty(t, testRegistry.detectors)
  157. // Register magic bytes for JPEG using the method
  158. jpegMagic := []byte{0xFF, 0xD8}
  159. testRegistry.RegisterMagicBytes(JPEG, jpegMagic)
  160. // Verify the magic bytes are registered
  161. require.Len(t, testRegistry.detectors, 1)
  162. }