helpers_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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("IntToStr", func() {
  236. It("convert int to string", func() {
  237. Expect(helpers.IntToStr(1)).To(Equal("1"))
  238. Expect(helpers.IntToStr(5)).To(Equal("5"))
  239. Expect(helpers.IntToStr(50)).To(Equal("50"))
  240. Expect(helpers.IntToStr(100)).To(Equal("100"))
  241. Expect(helpers.IntToStr(1000)).To(Equal("1000"))
  242. })
  243. })
  244. Context("RespondAsBadRequest", func() {
  245. BeforeEach(func() {
  246. var handler = func() http.HandlerFunc {
  247. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  248. helpers.RespondAsBadRequest(w, r, fmt.Errorf("MyError"))
  249. })
  250. }
  251. srv = httptest.NewServer(handler())
  252. client = srv.Client()
  253. resp, err = client.Get(srv.URL + "/")
  254. Expect(err).To(Succeed())
  255. })
  256. AfterEach(func() {
  257. Expect(resp.Body.Close()).To(Succeed())
  258. srv.Close()
  259. })
  260. It("handle bad request", func() {
  261. Expect(resp.StatusCode).To(Equal(http.StatusBadRequest))
  262. Expect(resp.Header.Get("Content-Type")).To(Equal("application/json"))
  263. body, err := io.ReadAll(resp.Body)
  264. Expect(err).To(Succeed())
  265. Expect(string(body)).To(MatchRegexp(`{"error":"MyError"}`))
  266. })
  267. })
  268. Context("RespondAsInternalServerError", func() {
  269. BeforeEach(func() {
  270. var handler = func() http.HandlerFunc {
  271. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  272. helpers.RespondAsInternalServerError(w, r)
  273. })
  274. }
  275. srv = httptest.NewServer(handler())
  276. client = srv.Client()
  277. resp, err = client.Get(srv.URL + "/")
  278. Expect(err).To(Succeed())
  279. })
  280. AfterEach(func() {
  281. Expect(resp.Body.Close()).To(Succeed())
  282. srv.Close()
  283. })
  284. It("handle method not allowed", func() {
  285. Expect(resp.StatusCode).To(Equal(http.StatusInternalServerError))
  286. })
  287. })
  288. Context("RespondAsMethodNotAllowed", func() {
  289. BeforeEach(func() {
  290. var handler = func() http.HandlerFunc {
  291. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  292. helpers.RespondAsMethodNotAllowed(w, r)
  293. })
  294. }
  295. srv = httptest.NewServer(handler())
  296. client = srv.Client()
  297. resp, err = client.Get(srv.URL + "/")
  298. Expect(err).To(Succeed())
  299. })
  300. AfterEach(func() {
  301. Expect(resp.Body.Close()).To(Succeed())
  302. srv.Close()
  303. })
  304. It("handle method not allowed", func() {
  305. Expect(resp.StatusCode).To(Equal(http.StatusMethodNotAllowed))
  306. })
  307. })
  308. })
  309. Context("MinifyHtmlCode", func() {
  310. It("minify Html code", func() {
  311. Expect(helpers.MinifyHtmlCode(`
  312. <!doctype html>
  313. <html lang="uk">
  314. <head>
  315. <meta charset="utf-8" />
  316. <script>
  317. // Comment
  318. var LangTexts = {
  319. "empty": "Empty",
  320. "full": "Full"
  321. };
  322. /* Comment */
  323. /*
  324. Comment line 1
  325. Comment line 2
  326. */
  327. function Func(value) {
  328. console.log(value);
  329. };
  330. </script>
  331. </head>
  332. <body>
  333. Index
  334. </body>
  335. </html>
  336. `)).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>`))
  337. Expect(helpers.MinifyHtmlCode(`
  338. <div>
  339. <a href="#">Link 1</a>, <a href="#">Link 2</a>
  340. </div>
  341. `)).To(Equal(`<div><a href="#">Link 1</a>, <a href="#">Link 2</a></div>`))
  342. Expect(helpers.MinifyHtmlCode(`
  343. <div>
  344. <b>Contacts:</b> <a href="#">Link 1</a>, <a href="#">Link 2</a>
  345. </div>
  346. `)).To(Equal(`<div><b>Contacts:</b> <a href="#">Link 1</a>, <a href="#">Link 2</a></div>`))
  347. })
  348. })
  349. Context("FakeResponseWriter", func() {
  350. It("write data to fake response writer", func() {
  351. var someHandleFunc = func(w http.ResponseWriter) {
  352. w.Header().Set("Content-Type", "application/json")
  353. w.WriteHeader(http.StatusNotFound)
  354. _, _ = w.Write([]byte("body"))
  355. }
  356. writer := helpers.NewFakeResponseWriter()
  357. someHandleFunc(writer)
  358. Expect(writer.Body).To(Equal([]byte("body")))
  359. Expect(writer.Headers).To(Equal(http.Header{
  360. "Content-Type": []string{"application/json"},
  361. }))
  362. Expect(writer.StatusCode).To(Equal(http.StatusNotFound))
  363. })
  364. })
  365. })
  366. func TestSuite(t *testing.T) {
  367. RegisterFailHandler(Fail)
  368. RunSpecs(t, "helpers")
  369. }