helpers_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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("HandleImageJpeg", func() {
  139. BeforeEach(func() {
  140. srv = httptest.NewServer(helpers.HandleImageJpeg("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 jpeg", func() {
  150. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  151. Expect(resp.Header.Get("Content-Type")).To(Equal("image/jpeg"))
  152. body, err := io.ReadAll(resp.Body)
  153. Expect(err).To(Succeed())
  154. Expect(string(body)).To(Equal("MyContent"))
  155. })
  156. })
  157. Context("HandleImagePng", func() {
  158. BeforeEach(func() {
  159. srv = httptest.NewServer(helpers.HandleImagePng("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 png", func() {
  169. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  170. Expect(resp.Header.Get("Content-Type")).To(Equal("image/png"))
  171. body, err := io.ReadAll(resp.Body)
  172. Expect(err).To(Succeed())
  173. Expect(string(body)).To(Equal("MyContent"))
  174. })
  175. })
  176. Context("HandleTextCss", func() {
  177. BeforeEach(func() {
  178. srv = httptest.NewServer(helpers.HandleTextCss("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 text css", func() {
  188. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  189. Expect(resp.Header.Get("Content-Type")).To(Equal("text/css"))
  190. body, err := io.ReadAll(resp.Body)
  191. Expect(err).To(Succeed())
  192. Expect(string(body)).To(Equal("MyContent"))
  193. })
  194. })
  195. Context("HandleTextJavaScript", func() {
  196. BeforeEach(func() {
  197. srv = httptest.NewServer(helpers.HandleTextJavaScript("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 javascript", func() {
  207. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  208. Expect(resp.Header.Get("Content-Type")).To(Equal("text/javascript"))
  209. body, err := io.ReadAll(resp.Body)
  210. Expect(err).To(Succeed())
  211. Expect(string(body)).To(Equal("MyContent"))
  212. })
  213. })
  214. Context("HandleTextPlain", func() {
  215. BeforeEach(func() {
  216. srv = httptest.NewServer(helpers.HandleTextPlain("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 plain", func() {
  226. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  227. Expect(resp.Header.Get("Content-Type")).To(Equal("text/plain"))
  228. body, err := io.ReadAll(resp.Body)
  229. Expect(err).To(Succeed())
  230. Expect(string(body)).To(Equal("MyContent"))
  231. })
  232. })
  233. Context("HandleTextXml", func() {
  234. BeforeEach(func() {
  235. srv = httptest.NewServer(helpers.HandleTextXml("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 xml", func() {
  245. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  246. Expect(resp.Header.Get("Content-Type")).To(Equal("text/xml"))
  247. body, err := io.ReadAll(resp.Body)
  248. Expect(err).To(Succeed())
  249. Expect(string(body)).To(Equal("MyContent"))
  250. })
  251. })
  252. Context("IntToStr", func() {
  253. It("convert int to string", func() {
  254. Expect(helpers.IntToStr(1)).To(Equal("1"))
  255. Expect(helpers.IntToStr(5)).To(Equal("5"))
  256. Expect(helpers.IntToStr(50)).To(Equal("50"))
  257. Expect(helpers.IntToStr(100)).To(Equal("100"))
  258. Expect(helpers.IntToStr(1000)).To(Equal("1000"))
  259. })
  260. })
  261. Context("RespondAsBadRequest", func() {
  262. BeforeEach(func() {
  263. var handler = func() http.HandlerFunc {
  264. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  265. helpers.RespondAsBadRequest(w, r, fmt.Errorf("MyError"))
  266. })
  267. }
  268. srv = httptest.NewServer(handler())
  269. client = srv.Client()
  270. resp, err = client.Get(srv.URL + "/")
  271. Expect(err).To(Succeed())
  272. })
  273. AfterEach(func() {
  274. Expect(resp.Body.Close()).To(Succeed())
  275. srv.Close()
  276. })
  277. It("handle bad request", func() {
  278. Expect(resp.StatusCode).To(Equal(http.StatusBadRequest))
  279. Expect(resp.Header.Get("Content-Type")).To(Equal("application/json"))
  280. body, err := io.ReadAll(resp.Body)
  281. Expect(err).To(Succeed())
  282. Expect(string(body)).To(MatchRegexp(`{"error":"MyError"}`))
  283. })
  284. })
  285. Context("RespondAsInternalServerError", func() {
  286. BeforeEach(func() {
  287. var handler = func() http.HandlerFunc {
  288. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  289. helpers.RespondAsInternalServerError(w, r)
  290. })
  291. }
  292. srv = httptest.NewServer(handler())
  293. client = srv.Client()
  294. resp, err = client.Get(srv.URL + "/")
  295. Expect(err).To(Succeed())
  296. })
  297. AfterEach(func() {
  298. Expect(resp.Body.Close()).To(Succeed())
  299. srv.Close()
  300. })
  301. It("handle method not allowed", func() {
  302. Expect(resp.StatusCode).To(Equal(http.StatusInternalServerError))
  303. })
  304. })
  305. Context("RespondAsMethodNotAllowed", func() {
  306. BeforeEach(func() {
  307. var handler = func() http.HandlerFunc {
  308. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  309. helpers.RespondAsMethodNotAllowed(w, r)
  310. })
  311. }
  312. srv = httptest.NewServer(handler())
  313. client = srv.Client()
  314. resp, err = client.Get(srv.URL + "/")
  315. Expect(err).To(Succeed())
  316. })
  317. AfterEach(func() {
  318. Expect(resp.Body.Close()).To(Succeed())
  319. srv.Close()
  320. })
  321. It("handle method not allowed", func() {
  322. Expect(resp.StatusCode).To(Equal(http.StatusMethodNotAllowed))
  323. })
  324. })
  325. })
  326. Context("MinifyHtmlCode", func() {
  327. It("minify Html code", func() {
  328. Expect(helpers.MinifyHtmlCode(`
  329. <!doctype html>
  330. <html lang="uk">
  331. <head>
  332. <meta charset="utf-8" />
  333. <script>
  334. // Comment
  335. var LangTexts = {
  336. "empty": "Empty",
  337. "full": "Full"
  338. };
  339. /* Comment */
  340. /*
  341. Comment line 1
  342. Comment line 2
  343. */
  344. function Func(value) {
  345. console.log(value);
  346. };
  347. </script>
  348. </head>
  349. <body>
  350. Index
  351. </body>
  352. </html>
  353. `)).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>`))
  354. Expect(helpers.MinifyHtmlCode(`
  355. <div>
  356. <a href="#">Link 1</a>, <a href="#">Link 2</a>
  357. </div>
  358. `)).To(Equal(`<div><a href="#">Link 1</a>, <a href="#">Link 2</a></div>`))
  359. Expect(helpers.MinifyHtmlCode(`
  360. <div>
  361. <b>Contacts:</b> <a href="#">Link 1</a>, <a href="#">Link 2</a>
  362. </div>
  363. `)).To(Equal(`<div><b>Contacts:</b> <a href="#">Link 1</a>, <a href="#">Link 2</a></div>`))
  364. })
  365. })
  366. Context("FakeResponseWriter", func() {
  367. It("write data to fake response writer", func() {
  368. var someHandleFunc = func(w http.ResponseWriter) {
  369. w.Header().Set("Content-Type", "application/json")
  370. w.WriteHeader(http.StatusNotFound)
  371. _, _ = w.Write([]byte("body"))
  372. }
  373. writer := helpers.NewFakeResponseWriter()
  374. someHandleFunc(writer)
  375. Expect(writer.Body).To(Equal([]byte("body")))
  376. Expect(writer.Headers).To(Equal(http.Header{
  377. "Content-Type": []string{"application/json"},
  378. }))
  379. Expect(writer.StatusCode).To(Equal(http.StatusNotFound))
  380. })
  381. })
  382. })
  383. func TestSuite(t *testing.T) {
  384. RegisterFailHandler(Fail)
  385. RunSpecs(t, "helpers")
  386. }