helpers_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. package helpers_test
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "io/fs"
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. . "github.com/onsi/ginkgo"
  11. . "github.com/onsi/gomega"
  12. "github.com/vladimirok5959/golang-utils/utils/http/helpers"
  13. )
  14. var _ = Describe("helpers", func() {
  15. Context("CurlGetStatusError", func() {
  16. It("recognize error", func() {
  17. err := error(&helpers.CurlGetStatusError{Expected: http.StatusOK, Received: http.StatusServiceUnavailable})
  18. Expect(errors.Is(err, helpers.ErrCurlGetStatus)).To(BeTrue())
  19. err = error(&helpers.CurlGetStatusError{Expected: http.StatusOK, Received: http.StatusBadGateway})
  20. Expect(errors.Is(err, helpers.ErrCurlGetStatus)).To(BeTrue())
  21. err = fmt.Errorf("Some error")
  22. Expect(errors.Is(err, helpers.ErrCurlGetStatus)).To(BeFalse())
  23. Expect(errors.Is(fs.ErrNotExist, helpers.ErrCurlGetStatus)).To(BeFalse())
  24. })
  25. It("generate error message", func() {
  26. err := error(&helpers.CurlGetStatusError{Expected: http.StatusOK, Received: http.StatusBadGateway})
  27. Expect(err.Error()).To(Equal("CurlGet: expected 200, received 502"))
  28. })
  29. })
  30. Context("ClientIP", func() {
  31. It("return client IP", func() {
  32. Expect(helpers.ClientIP(&http.Request{
  33. RemoteAddr: "127.0.0.1",
  34. })).To(Equal("127.0.0.1"))
  35. Expect(helpers.ClientIP(&http.Request{
  36. RemoteAddr: "192.168.0.1,127.0.0.1",
  37. })).To(Equal("192.168.0.1"))
  38. Expect(helpers.ClientIP(&http.Request{
  39. RemoteAddr: "192.168.0.1, 127.0.0.1",
  40. })).To(Equal("192.168.0.1"))
  41. Expect(helpers.ClientIP(&http.Request{
  42. RemoteAddr: "192.168.0.50,192.168.0.1,127.0.0.1",
  43. })).To(Equal("192.168.0.50"))
  44. Expect(helpers.ClientIP(&http.Request{
  45. RemoteAddr: "192.168.0.50, 192.168.0.1, 127.0.0.1",
  46. })).To(Equal("192.168.0.50"))
  47. })
  48. })
  49. Context("ClientIPs", func() {
  50. It("return array of client IPs", func() {
  51. Expect(helpers.ClientIPs(&http.Request{
  52. RemoteAddr: "127.0.0.1",
  53. })).To(ConsistOf(
  54. "127.0.0.1",
  55. ))
  56. Expect(helpers.ClientIPs(&http.Request{
  57. RemoteAddr: "192.168.0.1,127.0.0.1",
  58. })).To(ConsistOf(
  59. "192.168.0.1", "127.0.0.1",
  60. ))
  61. Expect(helpers.ClientIPs(&http.Request{
  62. RemoteAddr: "192.168.0.1, 127.0.0.1",
  63. })).To(ConsistOf(
  64. "192.168.0.1", "127.0.0.1",
  65. ))
  66. Expect(helpers.ClientIPs(&http.Request{
  67. RemoteAddr: "192.168.0.50,192.168.0.1,127.0.0.1",
  68. })).To(ConsistOf(
  69. "192.168.0.50", "192.168.0.1", "127.0.0.1",
  70. ))
  71. Expect(helpers.ClientIPs(&http.Request{
  72. RemoteAddr: "192.168.0.50, 192.168.0.1, 127.0.0.1",
  73. })).To(ConsistOf(
  74. "192.168.0.50", "192.168.0.1", "127.0.0.1",
  75. ))
  76. })
  77. })
  78. Context("Handles", func() {
  79. var srv *httptest.Server
  80. var client *http.Client
  81. var resp *http.Response
  82. var err error
  83. Context("HandleAppStatus", func() {
  84. BeforeEach(func() {
  85. srv = httptest.NewServer(helpers.HandleAppStatus())
  86. client = srv.Client()
  87. resp, err = client.Get(srv.URL + "/")
  88. Expect(err).To(Succeed())
  89. })
  90. AfterEach(func() {
  91. Expect(resp.Body.Close()).To(Succeed())
  92. srv.Close()
  93. })
  94. It("handle app status", func() {
  95. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  96. Expect(resp.Header.Get("Content-Type")).To(Equal("application/json"))
  97. body, err := io.ReadAll(resp.Body)
  98. Expect(err).To(Succeed())
  99. Expect(string(body)).To(MatchRegexp(`{"memory":{"alloc":[0-9]+,"num_gc":[0-9]+,"sys":[0-9]+,"total_alloc":[0-9]+},"routines":[0-9]+}`))
  100. })
  101. })
  102. Context("HandleFile", func() {
  103. BeforeEach(func() {
  104. srv = httptest.NewServer(helpers.HandleFile("MyContent", "my/type"))
  105. client = srv.Client()
  106. resp, err = client.Get(srv.URL + "/")
  107. Expect(err).To(Succeed())
  108. })
  109. AfterEach(func() {
  110. Expect(resp.Body.Close()).To(Succeed())
  111. srv.Close()
  112. })
  113. It("handle file", func() {
  114. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  115. Expect(resp.Header.Get("Content-Type")).To(Equal("my/type"))
  116. body, err := io.ReadAll(resp.Body)
  117. Expect(err).To(Succeed())
  118. Expect(string(body)).To(Equal("MyContent"))
  119. })
  120. })
  121. Context("HandleImageJpeg", func() {
  122. BeforeEach(func() {
  123. srv = httptest.NewServer(helpers.HandleImageJpeg("MyContent"))
  124. client = srv.Client()
  125. resp, err = client.Get(srv.URL + "/")
  126. Expect(err).To(Succeed())
  127. })
  128. AfterEach(func() {
  129. Expect(resp.Body.Close()).To(Succeed())
  130. srv.Close()
  131. })
  132. It("handle image jpeg", func() {
  133. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  134. Expect(resp.Header.Get("Content-Type")).To(Equal("image/jpeg"))
  135. body, err := io.ReadAll(resp.Body)
  136. Expect(err).To(Succeed())
  137. Expect(string(body)).To(Equal("MyContent"))
  138. })
  139. })
  140. Context("HandleImagePng", func() {
  141. BeforeEach(func() {
  142. srv = httptest.NewServer(helpers.HandleImagePng("MyContent"))
  143. client = srv.Client()
  144. resp, err = client.Get(srv.URL + "/")
  145. Expect(err).To(Succeed())
  146. })
  147. AfterEach(func() {
  148. Expect(resp.Body.Close()).To(Succeed())
  149. srv.Close()
  150. })
  151. It("handle image png", func() {
  152. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  153. Expect(resp.Header.Get("Content-Type")).To(Equal("image/png"))
  154. body, err := io.ReadAll(resp.Body)
  155. Expect(err).To(Succeed())
  156. Expect(string(body)).To(Equal("MyContent"))
  157. })
  158. })
  159. Context("HandleTextCss", func() {
  160. BeforeEach(func() {
  161. srv = httptest.NewServer(helpers.HandleTextCss("MyContent"))
  162. client = srv.Client()
  163. resp, err = client.Get(srv.URL + "/")
  164. Expect(err).To(Succeed())
  165. })
  166. AfterEach(func() {
  167. Expect(resp.Body.Close()).To(Succeed())
  168. srv.Close()
  169. })
  170. It("handle text css", func() {
  171. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  172. Expect(resp.Header.Get("Content-Type")).To(Equal("text/css"))
  173. body, err := io.ReadAll(resp.Body)
  174. Expect(err).To(Succeed())
  175. Expect(string(body)).To(Equal("MyContent"))
  176. })
  177. })
  178. Context("HandleTextJavaScript", func() {
  179. BeforeEach(func() {
  180. srv = httptest.NewServer(helpers.HandleTextJavaScript("MyContent"))
  181. client = srv.Client()
  182. resp, err = client.Get(srv.URL + "/")
  183. Expect(err).To(Succeed())
  184. })
  185. AfterEach(func() {
  186. Expect(resp.Body.Close()).To(Succeed())
  187. srv.Close()
  188. })
  189. It("handle text javascript", func() {
  190. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  191. Expect(resp.Header.Get("Content-Type")).To(Equal("text/javascript"))
  192. body, err := io.ReadAll(resp.Body)
  193. Expect(err).To(Succeed())
  194. Expect(string(body)).To(Equal("MyContent"))
  195. })
  196. })
  197. Context("HandleTextPlain", func() {
  198. BeforeEach(func() {
  199. srv = httptest.NewServer(helpers.HandleTextPlain("MyContent"))
  200. client = srv.Client()
  201. resp, err = client.Get(srv.URL + "/")
  202. Expect(err).To(Succeed())
  203. })
  204. AfterEach(func() {
  205. Expect(resp.Body.Close()).To(Succeed())
  206. srv.Close()
  207. })
  208. It("handle text plain", func() {
  209. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  210. Expect(resp.Header.Get("Content-Type")).To(Equal("text/plain"))
  211. body, err := io.ReadAll(resp.Body)
  212. Expect(err).To(Succeed())
  213. Expect(string(body)).To(Equal("MyContent"))
  214. })
  215. })
  216. Context("HandleTextXml", func() {
  217. BeforeEach(func() {
  218. srv = httptest.NewServer(helpers.HandleTextXml("MyContent"))
  219. client = srv.Client()
  220. resp, err = client.Get(srv.URL + "/")
  221. Expect(err).To(Succeed())
  222. })
  223. AfterEach(func() {
  224. Expect(resp.Body.Close()).To(Succeed())
  225. srv.Close()
  226. })
  227. It("handle text xml", func() {
  228. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  229. Expect(resp.Header.Get("Content-Type")).To(Equal("text/xml"))
  230. body, err := io.ReadAll(resp.Body)
  231. Expect(err).To(Succeed())
  232. Expect(string(body)).To(Equal("MyContent"))
  233. })
  234. })
  235. Context("RespondAsBadRequest", func() {
  236. BeforeEach(func() {
  237. var handler = func() http.HandlerFunc {
  238. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  239. helpers.RespondAsBadRequest(w, r, fmt.Errorf("MyError"))
  240. })
  241. }
  242. srv = httptest.NewServer(handler())
  243. client = srv.Client()
  244. resp, err = client.Get(srv.URL + "/")
  245. Expect(err).To(Succeed())
  246. })
  247. AfterEach(func() {
  248. Expect(resp.Body.Close()).To(Succeed())
  249. srv.Close()
  250. })
  251. It("handle bad request", func() {
  252. Expect(resp.StatusCode).To(Equal(http.StatusBadRequest))
  253. Expect(resp.Header.Get("Content-Type")).To(Equal("application/json"))
  254. body, err := io.ReadAll(resp.Body)
  255. Expect(err).To(Succeed())
  256. Expect(string(body)).To(MatchRegexp(`{"error":"MyError"}`))
  257. })
  258. })
  259. Context("RespondAsInternalServerError", func() {
  260. BeforeEach(func() {
  261. var handler = func() http.HandlerFunc {
  262. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  263. helpers.RespondAsInternalServerError(w, r)
  264. })
  265. }
  266. srv = httptest.NewServer(handler())
  267. client = srv.Client()
  268. resp, err = client.Get(srv.URL + "/")
  269. Expect(err).To(Succeed())
  270. })
  271. AfterEach(func() {
  272. Expect(resp.Body.Close()).To(Succeed())
  273. srv.Close()
  274. })
  275. It("handle method not allowed", func() {
  276. Expect(resp.StatusCode).To(Equal(http.StatusInternalServerError))
  277. })
  278. })
  279. Context("RespondAsMethodNotAllowed", func() {
  280. BeforeEach(func() {
  281. var handler = func() http.HandlerFunc {
  282. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  283. helpers.RespondAsMethodNotAllowed(w, r)
  284. })
  285. }
  286. srv = httptest.NewServer(handler())
  287. client = srv.Client()
  288. resp, err = client.Get(srv.URL + "/")
  289. Expect(err).To(Succeed())
  290. })
  291. AfterEach(func() {
  292. Expect(resp.Body.Close()).To(Succeed())
  293. srv.Close()
  294. })
  295. It("handle method not allowed", func() {
  296. Expect(resp.StatusCode).To(Equal(http.StatusMethodNotAllowed))
  297. })
  298. })
  299. })
  300. Context("MinifyHtmlCode", func() {
  301. It("minify Html code", func() {
  302. Expect(helpers.MinifyHtmlCode(`
  303. <!doctype html>
  304. <html lang="uk">
  305. <head>
  306. <meta charset="utf-8" />
  307. </head>
  308. <body>
  309. Index
  310. </body>
  311. </html>
  312. `)).To(Equal(`<!doctype html><html lang="uk"><head><meta charset="utf-8" /></head><body>Index</body></html>`))
  313. Expect(helpers.MinifyHtmlCode(`
  314. <div>
  315. <a href="#">Link 1</a>, <a href="#">Link 2</a>
  316. </div>
  317. `)).To(Equal(`<div><a href="#">Link 1</a>, <a href="#">Link 2</a></div>`))
  318. Expect(helpers.MinifyHtmlCode(`
  319. <div>
  320. <b>Contacts:</b> <a href="#">Link 1</a>, <a href="#">Link 2</a>
  321. </div>
  322. `)).To(Equal(`<div><b>Contacts:</b> <a href="#">Link 1</a>, <a href="#">Link 2</a></div>`))
  323. })
  324. })
  325. Context("FakeResponseWriter", func() {
  326. It("write data to fake response writer", func() {
  327. var someHandleFunc = func(w http.ResponseWriter) {
  328. w.Header().Set("Content-Type", "application/json")
  329. w.WriteHeader(http.StatusNotFound)
  330. _, _ = w.Write([]byte("body"))
  331. }
  332. writer := helpers.NewFakeResponseWriter()
  333. someHandleFunc(writer)
  334. Expect(writer.Body).To(Equal([]byte("body")))
  335. Expect(writer.Headers).To(Equal(http.Header{
  336. "Content-Type": []string{"application/json"},
  337. }))
  338. Expect(writer.StatusCode).To(Equal(http.StatusNotFound))
  339. })
  340. })
  341. })
  342. func TestSuite(t *testing.T) {
  343. RegisterFailHandler(Fail)
  344. RunSpecs(t, "helpers")
  345. }