helpers_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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("HandleTextXml", func() {
  253. BeforeEach(func() {
  254. srv = httptest.NewServer(helpers.HandleTextXml("MyContent"))
  255. client = srv.Client()
  256. resp, err = client.Get(srv.URL + "/")
  257. Expect(err).To(Succeed())
  258. })
  259. AfterEach(func() {
  260. Expect(resp.Body.Close()).To(Succeed())
  261. srv.Close()
  262. })
  263. It("handle text xml", func() {
  264. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  265. Expect(resp.Header.Get("Content-Type")).To(Equal("text/xml"))
  266. body, err := io.ReadAll(resp.Body)
  267. Expect(err).To(Succeed())
  268. Expect(string(body)).To(Equal("MyContent"))
  269. })
  270. })
  271. Context("IntToStr", func() {
  272. It("convert int to string", func() {
  273. Expect(helpers.IntToStr(1)).To(Equal("1"))
  274. Expect(helpers.IntToStr(5)).To(Equal("5"))
  275. Expect(helpers.IntToStr(50)).To(Equal("50"))
  276. Expect(helpers.IntToStr(100)).To(Equal("100"))
  277. Expect(helpers.IntToStr(1000)).To(Equal("1000"))
  278. })
  279. })
  280. Context("IntToStr64", func() {
  281. It("convert int to string", func() {
  282. Expect(helpers.IntToStr64(1)).To(Equal("1"))
  283. Expect(helpers.IntToStr64(5)).To(Equal("5"))
  284. Expect(helpers.IntToStr64(50)).To(Equal("50"))
  285. Expect(helpers.IntToStr64(100)).To(Equal("100"))
  286. Expect(helpers.IntToStr64(1000)).To(Equal("1000"))
  287. })
  288. })
  289. Context("RespondAsBadRequest", func() {
  290. BeforeEach(func() {
  291. var handler = func() http.HandlerFunc {
  292. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  293. helpers.RespondAsBadRequest(w, r, fmt.Errorf("MyError"))
  294. })
  295. }
  296. srv = httptest.NewServer(handler())
  297. client = srv.Client()
  298. resp, err = client.Get(srv.URL + "/")
  299. Expect(err).To(Succeed())
  300. })
  301. AfterEach(func() {
  302. Expect(resp.Body.Close()).To(Succeed())
  303. srv.Close()
  304. })
  305. It("handle bad request", func() {
  306. Expect(resp.StatusCode).To(Equal(http.StatusBadRequest))
  307. Expect(resp.Header.Get("Content-Type")).To(Equal("application/json"))
  308. body, err := io.ReadAll(resp.Body)
  309. Expect(err).To(Succeed())
  310. Expect(string(body)).To(MatchRegexp(`{"error":"MyError"}`))
  311. })
  312. })
  313. Context("RespondAsInternalServerError", func() {
  314. BeforeEach(func() {
  315. var handler = func() http.HandlerFunc {
  316. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  317. helpers.RespondAsInternalServerError(w, r)
  318. })
  319. }
  320. srv = httptest.NewServer(handler())
  321. client = srv.Client()
  322. resp, err = client.Get(srv.URL + "/")
  323. Expect(err).To(Succeed())
  324. })
  325. AfterEach(func() {
  326. Expect(resp.Body.Close()).To(Succeed())
  327. srv.Close()
  328. })
  329. It("handle method not allowed", func() {
  330. Expect(resp.StatusCode).To(Equal(http.StatusInternalServerError))
  331. })
  332. })
  333. Context("RespondAsMethodNotAllowed", func() {
  334. BeforeEach(func() {
  335. var handler = func() http.HandlerFunc {
  336. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  337. helpers.RespondAsMethodNotAllowed(w, r)
  338. })
  339. }
  340. srv = httptest.NewServer(handler())
  341. client = srv.Client()
  342. resp, err = client.Get(srv.URL + "/")
  343. Expect(err).To(Succeed())
  344. })
  345. AfterEach(func() {
  346. Expect(resp.Body.Close()).To(Succeed())
  347. srv.Close()
  348. })
  349. It("handle method not allowed", func() {
  350. Expect(resp.StatusCode).To(Equal(http.StatusMethodNotAllowed))
  351. })
  352. })
  353. })
  354. Context("MinifyHtmlCode", func() {
  355. It("minify Html code", func() {
  356. Expect(helpers.MinifyHtmlCode(`
  357. <!doctype html>
  358. <html lang="uk">
  359. <head>
  360. <meta charset="utf-8" />
  361. <script>
  362. // Comment
  363. var LangTexts = {
  364. "empty": "Empty",
  365. "full": "Full"
  366. };
  367. /* Comment */
  368. /*
  369. Comment line 1
  370. Comment line 2
  371. */
  372. function Func(value) {
  373. console.log(value);
  374. };
  375. </script>
  376. </head>
  377. <body>
  378. Index
  379. </body>
  380. </html>
  381. `)).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>`))
  382. Expect(helpers.MinifyHtmlCode(`
  383. <div>
  384. <a href="#">Link 1</a>, <a href="#">Link 2</a>
  385. </div>
  386. `)).To(Equal(`<div><a href="#">Link 1</a>, <a href="#">Link 2</a></div>`))
  387. Expect(helpers.MinifyHtmlCode(`
  388. <div>
  389. <b>Contacts:</b> <a href="#">Link 1</a>, <a href="#">Link 2</a>
  390. </div>
  391. `)).To(Equal(`<div><b>Contacts:</b> <a href="#">Link 1</a>, <a href="#">Link 2</a></div>`))
  392. })
  393. })
  394. Context("StrToInt", func() {
  395. It("convert int to string", func() {
  396. Expect(helpers.StrToInt("")).To(Equal(0))
  397. Expect(helpers.StrToInt("1")).To(Equal(1))
  398. Expect(helpers.StrToInt("5")).To(Equal(5))
  399. Expect(helpers.StrToInt("50")).To(Equal(50))
  400. Expect(helpers.StrToInt("100")).To(Equal(100))
  401. Expect(helpers.StrToInt("1000")).To(Equal(1000))
  402. })
  403. })
  404. Context("StrToInt64", func() {
  405. It("convert int to string", func() {
  406. Expect(helpers.StrToInt64("")).To(Equal(int64(0)))
  407. Expect(helpers.StrToInt64("1")).To(Equal(int64(1)))
  408. Expect(helpers.StrToInt64("5")).To(Equal(int64(5)))
  409. Expect(helpers.StrToInt64("50")).To(Equal(int64(50)))
  410. Expect(helpers.StrToInt64("100")).To(Equal(int64(100)))
  411. Expect(helpers.StrToInt64("1000")).To(Equal(int64(1000)))
  412. })
  413. })
  414. Context("FakeResponseWriter", func() {
  415. It("write data to fake response writer", func() {
  416. var someHandleFunc = func(w http.ResponseWriter) {
  417. w.Header().Set("Content-Type", "application/json")
  418. w.WriteHeader(http.StatusNotFound)
  419. _, _ = w.Write([]byte("body"))
  420. }
  421. writer := helpers.NewFakeResponseWriter()
  422. someHandleFunc(writer)
  423. Expect(writer.Body).To(Equal([]byte("body")))
  424. Expect(writer.Headers).To(Equal(http.Header{
  425. "Content-Type": []string{"application/json"},
  426. }))
  427. Expect(writer.StatusCode).To(Equal(http.StatusNotFound))
  428. })
  429. })
  430. })
  431. func TestSuite(t *testing.T) {
  432. RegisterFailHandler(Fail)
  433. RunSpecs(t, "helpers")
  434. }