helpers_test.go 9.5 KB

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