Browse Source

Add HandleImageGif func

Volodymyr Tkach 1 year ago
parent
commit
f806fbd0a5
3 changed files with 30 additions and 0 deletions
  1. 1 0
      README.md
  2. 5 0
      utils/http/helpers/helpers.go
  3. 24 0
      utils/http/helpers/helpers_test.go

+ 1 - 0
README.md

@@ -139,6 +139,7 @@ func ClientIP(r *http.Request) string
 func ClientIPs(r *http.Request) []string
 func HandleAppStatus() http.Handler
 func HandleFile(data, contentType string) http.Handler
+func HandleImageGif(data string) http.Handler
 func HandleImageJpeg(data string) http.Handler
 func HandleImagePng(data string) http.Handler
 func HandleTextCss(data string) http.Handler

+ 5 - 0
utils/http/helpers/helpers.go

@@ -21,6 +21,7 @@ import (
 // func ClientIPs(r *http.Request) []string
 // func HandleAppStatus() http.Handler
 // func HandleFile(data, contentType string) http.Handler
+// func HandleImageGif(data string) http.Handler
 // func HandleImageJpeg(data string) http.Handler
 // func HandleImagePng(data string) http.Handler
 // func HandleTextCss(data string) http.Handler
@@ -133,6 +134,10 @@ func HandleFile(data, contentType string) http.Handler {
 	})
 }
 
+func HandleImageGif(data string) http.Handler {
+	return HandleFile(data, "image/gif")
+}
+
 func HandleImageJpeg(data string) http.Handler {
 	return HandleFile(data, "image/jpeg")
 }

+ 24 - 0
utils/http/helpers/helpers_test.go

@@ -167,6 +167,30 @@ var _ = Describe("helpers", func() {
 			})
 		})
 
+		Context("HandleImageGif", func() {
+			BeforeEach(func() {
+				srv = httptest.NewServer(helpers.HandleImageGif("MyContent"))
+				client = srv.Client()
+				resp, err = client.Get(srv.URL + "/")
+				Expect(err).To(Succeed())
+			})
+
+			AfterEach(func() {
+				Expect(resp.Body.Close()).To(Succeed())
+				srv.Close()
+			})
+
+			It("handle image gif", func() {
+				Expect(resp.StatusCode).To(Equal(http.StatusOK))
+				Expect(resp.Header.Get("Content-Type")).To(Equal("image/gif"))
+
+				body, err := io.ReadAll(resp.Body)
+				Expect(err).To(Succeed())
+
+				Expect(string(body)).To(Equal("MyContent"))
+			})
+		})
+
 		Context("HandleImageJpeg", func() {
 			BeforeEach(func() {
 				srv = httptest.NewServer(helpers.HandleImageJpeg("MyContent"))