helpers_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. It("return array of client IPs without port", func() {
  78. Expect(helpers.ClientIPs(&http.Request{
  79. RemoteAddr: "[::1]:544722",
  80. })).To(ConsistOf(
  81. "[::1]",
  82. ))
  83. Expect(helpers.ClientIPs(&http.Request{
  84. RemoteAddr: "127.0.0.1:8080",
  85. })).To(ConsistOf(
  86. "127.0.0.1",
  87. ))
  88. Expect(helpers.ClientIPs(&http.Request{
  89. RemoteAddr: "192.168.0.1:80,127.0.0.1:443",
  90. })).To(ConsistOf(
  91. "192.168.0.1", "127.0.0.1",
  92. ))
  93. })
  94. })
  95. Context("Handles", func() {
  96. var srv *httptest.Server
  97. var client *http.Client
  98. var resp *http.Response
  99. var err error
  100. Context("HandleAppStatus", func() {
  101. BeforeEach(func() {
  102. srv = httptest.NewServer(helpers.HandleAppStatus())
  103. client = srv.Client()
  104. resp, err = client.Get(srv.URL + "/")
  105. Expect(err).To(Succeed())
  106. })
  107. AfterEach(func() {
  108. Expect(resp.Body.Close()).To(Succeed())
  109. srv.Close()
  110. })
  111. It("handle app status", func() {
  112. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  113. Expect(resp.Header.Get("Content-Type")).To(Equal("application/json"))
  114. body, err := io.ReadAll(resp.Body)
  115. Expect(err).To(Succeed())
  116. Expect(string(body)).To(MatchRegexp(`{"memory":{"alloc":[0-9]+,"num_gc":[0-9]+,"sys":[0-9]+,"total_alloc":[0-9]+},"routines":[0-9]+}`))
  117. })
  118. })
  119. Context("HandleFile", func() {
  120. BeforeEach(func() {
  121. srv = httptest.NewServer(helpers.HandleFile("MyContent", "my/type"))
  122. client = srv.Client()
  123. resp, err = client.Get(srv.URL + "/")
  124. Expect(err).To(Succeed())
  125. })
  126. AfterEach(func() {
  127. Expect(resp.Body.Close()).To(Succeed())
  128. srv.Close()
  129. })
  130. It("handle file", func() {
  131. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  132. Expect(resp.Header.Get("Content-Type")).To(Equal("my/type"))
  133. body, err := io.ReadAll(resp.Body)
  134. Expect(err).To(Succeed())
  135. Expect(string(body)).To(Equal("MyContent"))
  136. })
  137. })
  138. Context("HandleImageGif", func() {
  139. BeforeEach(func() {
  140. srv = httptest.NewServer(helpers.HandleImageGif("MyContent"))
  141. client = srv.Client()
  142. resp, err = client.Get(srv.URL + "/")
  143. Expect(err).To(Succeed())
  144. })
  145. AfterEach(func() {
  146. Expect(resp.Body.Close()).To(Succeed())
  147. srv.Close()
  148. })
  149. It("handle image gif", func() {
  150. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  151. Expect(resp.Header.Get("Content-Type")).To(Equal("image/gif"))
  152. body, err := io.ReadAll(resp.Body)
  153. Expect(err).To(Succeed())
  154. Expect(string(body)).To(Equal("MyContent"))
  155. })
  156. })
  157. Context("HandleImageJpeg", func() {
  158. BeforeEach(func() {
  159. srv = httptest.NewServer(helpers.HandleImageJpeg("MyContent"))
  160. client = srv.Client()
  161. resp, err = client.Get(srv.URL + "/")
  162. Expect(err).To(Succeed())
  163. })
  164. AfterEach(func() {
  165. Expect(resp.Body.Close()).To(Succeed())
  166. srv.Close()
  167. })
  168. It("handle image jpeg", func() {
  169. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  170. Expect(resp.Header.Get("Content-Type")).To(Equal("image/jpeg"))
  171. body, err := io.ReadAll(resp.Body)
  172. Expect(err).To(Succeed())
  173. Expect(string(body)).To(Equal("MyContent"))
  174. })
  175. })
  176. Context("HandleImagePng", func() {
  177. BeforeEach(func() {
  178. srv = httptest.NewServer(helpers.HandleImagePng("MyContent"))
  179. client = srv.Client()
  180. resp, err = client.Get(srv.URL + "/")
  181. Expect(err).To(Succeed())
  182. })
  183. AfterEach(func() {
  184. Expect(resp.Body.Close()).To(Succeed())
  185. srv.Close()
  186. })
  187. It("handle image png", func() {
  188. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  189. Expect(resp.Header.Get("Content-Type")).To(Equal("image/png"))
  190. body, err := io.ReadAll(resp.Body)
  191. Expect(err).To(Succeed())
  192. Expect(string(body)).To(Equal("MyContent"))
  193. })
  194. })
  195. Context("HandleTextCss", func() {
  196. BeforeEach(func() {
  197. srv = httptest.NewServer(helpers.HandleTextCss("MyContent"))
  198. client = srv.Client()
  199. resp, err = client.Get(srv.URL + "/")
  200. Expect(err).To(Succeed())
  201. })
  202. AfterEach(func() {
  203. Expect(resp.Body.Close()).To(Succeed())
  204. srv.Close()
  205. })
  206. It("handle text css", func() {
  207. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  208. Expect(resp.Header.Get("Content-Type")).To(Equal("text/css"))
  209. body, err := io.ReadAll(resp.Body)
  210. Expect(err).To(Succeed())
  211. Expect(string(body)).To(Equal("MyContent"))
  212. })
  213. })
  214. Context("HandleTextJavaScript", func() {
  215. BeforeEach(func() {
  216. srv = httptest.NewServer(helpers.HandleTextJavaScript("MyContent"))
  217. client = srv.Client()
  218. resp, err = client.Get(srv.URL + "/")
  219. Expect(err).To(Succeed())
  220. })
  221. AfterEach(func() {
  222. Expect(resp.Body.Close()).To(Succeed())
  223. srv.Close()
  224. })
  225. It("handle text javascript", func() {
  226. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  227. Expect(resp.Header.Get("Content-Type")).To(Equal("text/javascript"))
  228. body, err := io.ReadAll(resp.Body)
  229. Expect(err).To(Succeed())
  230. Expect(string(body)).To(Equal("MyContent"))
  231. })
  232. })
  233. Context("HandleTextPlain", func() {
  234. BeforeEach(func() {
  235. srv = httptest.NewServer(helpers.HandleTextPlain("MyContent"))
  236. client = srv.Client()
  237. resp, err = client.Get(srv.URL + "/")
  238. Expect(err).To(Succeed())
  239. })
  240. AfterEach(func() {
  241. Expect(resp.Body.Close()).To(Succeed())
  242. srv.Close()
  243. })
  244. It("handle text plain", func() {
  245. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  246. Expect(resp.Header.Get("Content-Type")).To(Equal("text/plain"))
  247. body, err := io.ReadAll(resp.Body)
  248. Expect(err).To(Succeed())
  249. Expect(string(body)).To(Equal("MyContent"))
  250. })
  251. })
  252. Context("InArrayInt", func() {
  253. Expect(helpers.InArrayInt(3, []int{1, 2, 3, 4, 5})).To(BeTrue())
  254. Expect(helpers.InArrayInt(9, []int{1, 2, 3, 4, 5})).To(BeFalse())
  255. })
  256. Context("InArrayInt64", func() {
  257. Expect(helpers.InArrayInt64(3, []int64{1, 2, 3, 4, 5})).To(BeTrue())
  258. Expect(helpers.InArrayInt64(9, []int64{1, 2, 3, 4, 5})).To(BeFalse())
  259. })
  260. Context("InArrayStr", func() {
  261. Expect(helpers.InArrayStr("3", []string{"1", "2", "3"})).To(BeTrue())
  262. Expect(helpers.InArrayStr("9", []string{"1", "2", "3"})).To(BeFalse())
  263. })
  264. Context("HandleTextXml", func() {
  265. BeforeEach(func() {
  266. srv = httptest.NewServer(helpers.HandleTextXml("MyContent"))
  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 text xml", func() {
  276. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  277. Expect(resp.Header.Get("Content-Type")).To(Equal("text/xml"))
  278. body, err := io.ReadAll(resp.Body)
  279. Expect(err).To(Succeed())
  280. Expect(string(body)).To(Equal("MyContent"))
  281. })
  282. })
  283. Context("IntToStr", func() {
  284. It("convert int to string", func() {
  285. Expect(helpers.IntToStr(1)).To(Equal("1"))
  286. Expect(helpers.IntToStr(5)).To(Equal("5"))
  287. Expect(helpers.IntToStr(50)).To(Equal("50"))
  288. Expect(helpers.IntToStr(100)).To(Equal("100"))
  289. Expect(helpers.IntToStr(1000)).To(Equal("1000"))
  290. })
  291. })
  292. Context("IntToStr64", func() {
  293. It("convert int to string", func() {
  294. Expect(helpers.IntToStr64(1)).To(Equal("1"))
  295. Expect(helpers.IntToStr64(5)).To(Equal("5"))
  296. Expect(helpers.IntToStr64(50)).To(Equal("50"))
  297. Expect(helpers.IntToStr64(100)).To(Equal("100"))
  298. Expect(helpers.IntToStr64(1000)).To(Equal("1000"))
  299. })
  300. })
  301. Context("RespondAsBadRequest", func() {
  302. BeforeEach(func() {
  303. var handler = func() http.HandlerFunc {
  304. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  305. helpers.RespondAsBadRequest(w, r, fmt.Errorf("MyError"))
  306. })
  307. }
  308. srv = httptest.NewServer(handler())
  309. client = srv.Client()
  310. resp, err = client.Get(srv.URL + "/")
  311. Expect(err).To(Succeed())
  312. })
  313. AfterEach(func() {
  314. Expect(resp.Body.Close()).To(Succeed())
  315. srv.Close()
  316. })
  317. It("handle bad request", func() {
  318. Expect(resp.StatusCode).To(Equal(http.StatusBadRequest))
  319. Expect(resp.Header.Get("Content-Type")).To(Equal("application/json"))
  320. body, err := io.ReadAll(resp.Body)
  321. Expect(err).To(Succeed())
  322. Expect(string(body)).To(MatchRegexp(`{"error":"MyError"}`))
  323. })
  324. })
  325. Context("RespondAsInternalServerError", func() {
  326. BeforeEach(func() {
  327. var handler = func() http.HandlerFunc {
  328. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  329. helpers.RespondAsInternalServerError(w, r)
  330. })
  331. }
  332. srv = httptest.NewServer(handler())
  333. client = srv.Client()
  334. resp, err = client.Get(srv.URL + "/")
  335. Expect(err).To(Succeed())
  336. })
  337. AfterEach(func() {
  338. Expect(resp.Body.Close()).To(Succeed())
  339. srv.Close()
  340. })
  341. It("handle method not allowed", func() {
  342. Expect(resp.StatusCode).To(Equal(http.StatusInternalServerError))
  343. })
  344. })
  345. Context("RespondAsMethodNotAllowed", func() {
  346. BeforeEach(func() {
  347. var handler = func() http.HandlerFunc {
  348. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  349. helpers.RespondAsMethodNotAllowed(w, r)
  350. })
  351. }
  352. srv = httptest.NewServer(handler())
  353. client = srv.Client()
  354. resp, err = client.Get(srv.URL + "/")
  355. Expect(err).To(Succeed())
  356. })
  357. AfterEach(func() {
  358. Expect(resp.Body.Close()).To(Succeed())
  359. srv.Close()
  360. })
  361. It("handle method not allowed", func() {
  362. Expect(resp.StatusCode).To(Equal(http.StatusMethodNotAllowed))
  363. })
  364. })
  365. })
  366. Context("MinifyHtmlCode", func() {
  367. It("minify Html code", func() {
  368. Expect(helpers.MinifyHtmlCode(`
  369. <!doctype html>
  370. <html lang="uk">
  371. <head>
  372. <meta charset="utf-8" />
  373. <script>
  374. // Comment
  375. var LangTexts = {
  376. "empty": "Empty",
  377. "full": "Full"
  378. };
  379. /* Comment */
  380. /*
  381. Comment line 1
  382. Comment line 2
  383. */
  384. function Func(value) {
  385. console.log(value);
  386. };
  387. </script>
  388. </head>
  389. <body>
  390. Index
  391. </body>
  392. </html>
  393. `)).To(Equal(`<!doctype html><html lang="uk"><head><meta charset="utf-8" /><script>var LangTexts={"empty":"Empty","full":"Full"};function Func(value){console.log(value);};</script></head><body>Index</body></html>`))
  394. Expect(helpers.MinifyHtmlCode(`
  395. <div>
  396. <a href="#">Link 1</a>, <a href="#">Link 2</a>
  397. </div>
  398. `)).To(Equal(`<div><a href="#">Link 1</a>, <a href="#">Link 2</a></div>`))
  399. Expect(helpers.MinifyHtmlCode(`
  400. <div>
  401. <b>Contacts:</b> <a href="#">Link 1</a>, <a href="#">Link 2</a>
  402. </div>
  403. `)).To(Equal(`<div><b>Contacts:</b> <a href="#">Link 1</a>, <a href="#">Link 2</a></div>`))
  404. })
  405. })
  406. Context("StrToInt", func() {
  407. It("convert int to string", func() {
  408. Expect(helpers.StrToInt("")).To(Equal(0))
  409. Expect(helpers.StrToInt("1")).To(Equal(1))
  410. Expect(helpers.StrToInt("5")).To(Equal(5))
  411. Expect(helpers.StrToInt("50")).To(Equal(50))
  412. Expect(helpers.StrToInt("100")).To(Equal(100))
  413. Expect(helpers.StrToInt("1000")).To(Equal(1000))
  414. })
  415. })
  416. Context("StrToInt64", func() {
  417. It("convert int to string", func() {
  418. Expect(helpers.StrToInt64("")).To(Equal(int64(0)))
  419. Expect(helpers.StrToInt64("1")).To(Equal(int64(1)))
  420. Expect(helpers.StrToInt64("5")).To(Equal(int64(5)))
  421. Expect(helpers.StrToInt64("50")).To(Equal(int64(50)))
  422. Expect(helpers.StrToInt64("100")).To(Equal(int64(100)))
  423. Expect(helpers.StrToInt64("1000")).To(Equal(int64(1000)))
  424. })
  425. })
  426. Context("FakeResponseWriter", func() {
  427. It("write data to fake response writer", func() {
  428. var someHandleFunc = func(w http.ResponseWriter) {
  429. w.Header().Set("Content-Type", "application/json")
  430. w.WriteHeader(http.StatusNotFound)
  431. _, _ = w.Write([]byte("body"))
  432. }
  433. writer := helpers.NewFakeResponseWriter()
  434. someHandleFunc(writer)
  435. Expect(writer.Body).To(Equal([]byte("body")))
  436. Expect(writer.Headers).To(Equal(http.Header{
  437. "Content-Type": []string{"application/json"},
  438. }))
  439. Expect(writer.StatusCode).To(Equal(http.StatusNotFound))
  440. })
  441. })
  442. })
  443. func TestSuite(t *testing.T) {
  444. RegisterFailHandler(Fail)
  445. RunSpecs(t, "helpers")
  446. }