image_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package image_test
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "net/http"
  6. "net/http/httptest"
  7. "strings"
  8. "testing"
  9. "github.com/labring/aiproxy/core/common/image"
  10. "github.com/smartystreets/goconvey/convey"
  11. )
  12. func TestIsImageURL(t *testing.T) {
  13. convey.Convey("IsImageURL", t, func() {
  14. convey.Convey("should return true for image content type", func() {
  15. convey.So(image.IsImageURL("image/jpeg"), convey.ShouldBeTrue)
  16. convey.So(image.IsImageURL("image/png"), convey.ShouldBeTrue)
  17. })
  18. convey.Convey("should return false for non-image content type", func() {
  19. convey.So(image.IsImageURL("text/plain"), convey.ShouldBeFalse)
  20. convey.So(image.IsImageURL("application/json"), convey.ShouldBeFalse)
  21. })
  22. })
  23. }
  24. func TestTrimImageContentType(t *testing.T) {
  25. convey.Convey("TrimImageContentType", t, func() {
  26. convey.Convey("should trim content type", func() {
  27. convey.So(
  28. image.TrimImageContentType("image/jpeg; charset=utf-8"),
  29. convey.ShouldEqual,
  30. "image/jpeg",
  31. )
  32. convey.So(image.TrimImageContentType("image/png"), convey.ShouldEqual, "image/png")
  33. })
  34. })
  35. }
  36. func TestGetImageSizeFromBase64(t *testing.T) {
  37. convey.Convey("GetImageSizeFromBase64", t, func() {
  38. // 1x1 pixel red dot png
  39. base64Img := "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
  40. convey.Convey("should get size from base64", func() {
  41. w, h, err := image.GetImageSizeFromBase64(base64Img)
  42. convey.So(err, convey.ShouldBeNil)
  43. convey.So(w, convey.ShouldEqual, 1)
  44. convey.So(h, convey.ShouldEqual, 1)
  45. })
  46. convey.Convey("should return error for invalid base64", func() {
  47. _, _, err := image.GetImageSizeFromBase64("invalid")
  48. convey.So(err, convey.ShouldNotBeNil)
  49. })
  50. })
  51. }
  52. func TestGetImageFromURL(t *testing.T) {
  53. convey.Convey("GetImageFromURL", t, func() {
  54. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  55. switch r.URL.Path {
  56. case "/image.png":
  57. w.Header().Set("Content-Type", "image/png")
  58. // 1x1 pixel red dot png
  59. data, _ := base64.StdEncoding.DecodeString(
  60. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
  61. )
  62. _, _ = w.Write(data)
  63. case "/text":
  64. w.Header().Set("Content-Type", "text/plain")
  65. _, _ = w.Write([]byte("hello"))
  66. default:
  67. w.WriteHeader(http.StatusNotFound)
  68. }
  69. }))
  70. defer ts.Close()
  71. convey.Convey("should get image from URL", func() {
  72. mime, data, err := image.GetImageFromURL(context.Background(), ts.URL+"/image.png")
  73. convey.So(err, convey.ShouldBeNil)
  74. convey.So(mime, convey.ShouldEqual, "image/png")
  75. convey.So(
  76. data,
  77. convey.ShouldEqual,
  78. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
  79. )
  80. })
  81. convey.Convey("should return error for non-image URL", func() {
  82. _, _, err := image.GetImageFromURL(context.Background(), ts.URL+"/text")
  83. convey.So(err, convey.ShouldNotBeNil)
  84. })
  85. convey.Convey("should return error for 404", func() {
  86. _, _, err := image.GetImageFromURL(context.Background(), ts.URL+"/404")
  87. convey.So(err, convey.ShouldNotBeNil)
  88. })
  89. convey.Convey("should handle data URL", func() {
  90. dataURL := "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
  91. mime, data, err := image.GetImageFromURL(context.Background(), dataURL)
  92. convey.So(err, convey.ShouldBeNil)
  93. convey.So(mime, convey.ShouldEqual, "image/png")
  94. convey.So(
  95. data,
  96. convey.ShouldEqual,
  97. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
  98. )
  99. })
  100. })
  101. }
  102. func TestGetImageSizeFromURL(t *testing.T) {
  103. convey.Convey("GetImageSizeFromURL", t, func() {
  104. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  105. if r.URL.Path == "/image.png" {
  106. w.Header().Set("Content-Type", "image/png")
  107. // 1x1 pixel red dot png
  108. data, _ := base64.StdEncoding.DecodeString(
  109. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
  110. )
  111. _, _ = w.Write(data)
  112. } else {
  113. w.WriteHeader(http.StatusNotFound)
  114. }
  115. }))
  116. defer ts.Close()
  117. convey.Convey("should get image size from URL", func() {
  118. w, h, err := image.GetImageSizeFromURL(ts.URL + "/image.png")
  119. convey.So(err, convey.ShouldBeNil)
  120. convey.So(w, convey.ShouldEqual, 1)
  121. convey.So(h, convey.ShouldEqual, 1)
  122. })
  123. convey.Convey("should return error for 404", func() {
  124. _, _, err := image.GetImageSizeFromURL(ts.URL + "/404")
  125. convey.So(err, convey.ShouldNotBeNil)
  126. })
  127. })
  128. }
  129. func TestGetImageSize(t *testing.T) {
  130. convey.Convey("GetImageSize", t, func() {
  131. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  132. if r.URL.Path == "/image.png" {
  133. w.Header().Set("Content-Type", "image/png")
  134. // 1x1 pixel red dot png
  135. data, _ := base64.StdEncoding.DecodeString(
  136. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
  137. )
  138. _, _ = w.Write(data)
  139. }
  140. }))
  141. defer ts.Close()
  142. convey.Convey("should get size from data URL", func() {
  143. dataURL := "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
  144. w, h, err := image.GetImageSize(dataURL)
  145. convey.So(err, convey.ShouldBeNil)
  146. convey.So(w, convey.ShouldEqual, 1)
  147. convey.So(h, convey.ShouldEqual, 1)
  148. })
  149. convey.Convey("should get size from HTTP URL", func() {
  150. w, h, err := image.GetImageSize(ts.URL + "/image.png")
  151. convey.So(err, convey.ShouldBeNil)
  152. convey.So(w, convey.ShouldEqual, 1)
  153. convey.So(h, convey.ShouldEqual, 1)
  154. })
  155. })
  156. }
  157. func TestSVG(t *testing.T) {
  158. convey.Convey("SVG Decode", t, func() {
  159. svgContent := `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
  160. <svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
  161. <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
  162. </svg>`
  163. // Helper to simulate reading from response body or file
  164. reader := strings.NewReader(svgContent)
  165. convey.Convey("should decode SVG config", func() {
  166. // Reset reader
  167. _, err := reader.Seek(0, 0)
  168. convey.So(err, convey.ShouldBeNil)
  169. config, err := image.DecodeConfig(reader)
  170. convey.So(err, convey.ShouldBeNil)
  171. convey.So(config.Width, convey.ShouldEqual, 100)
  172. convey.So(config.Height, convey.ShouldEqual, 100)
  173. })
  174. })
  175. }