utils_test.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package utils
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "net/http/httptest"
  7. "strings"
  8. "testing"
  9. "time"
  10. "golang-fave/consts"
  11. )
  12. func Expect(t *testing.T, actual, expect interface{}) {
  13. if actual != expect {
  14. t.Fatalf("\033[0;33mExpected \033[0;32m`(%T) %v`\033[0;33m but got \033[0;31m`(%T) %v`\033[0m",
  15. expect, expect, actual, actual)
  16. }
  17. }
  18. func TestIsFileExists(t *testing.T) {
  19. Expect(t, IsFileExists("./../support/some-file.txt"), true)
  20. Expect(t, IsFileExists("./../support/no-existed-file"), false)
  21. }
  22. func TestIsDir(t *testing.T) {
  23. Expect(t, IsDir("./../support"), true)
  24. Expect(t, IsDir("./../support/some-file.txt"), false)
  25. Expect(t, IsDir("./../support/no-existed-dir"), false)
  26. }
  27. func TestIsDirExists(t *testing.T) {
  28. Expect(t, IsDirExists("./../support"), true)
  29. Expect(t, IsDirExists("./../support/some-file.txt"), false)
  30. Expect(t, IsDirExists("./../support/no-existed-dir"), false)
  31. }
  32. func TestIsNumeric(t *testing.T) {
  33. Expect(t, IsNumeric("12345"), true)
  34. Expect(t, IsNumeric("string"), false)
  35. }
  36. func TestIsValidEmail(t *testing.T) {
  37. Expect(t, IsValidEmail("test@gmail.com"), true)
  38. Expect(t, IsValidEmail("test@yandex.ru"), true)
  39. Expect(t, IsValidEmail("test@ya.ru"), true)
  40. Expect(t, IsValidEmail("test@test"), false)
  41. }
  42. func TestIsValidAlias(t *testing.T) {
  43. Expect(t, IsValidAlias("/"), true)
  44. Expect(t, IsValidAlias("/some-page/"), true)
  45. Expect(t, IsValidAlias("/some-page.html"), true)
  46. Expect(t, IsValidAlias("/some-page.html/"), true)
  47. Expect(t, IsValidAlias(""), false)
  48. Expect(t, IsValidAlias("some-page"), false)
  49. Expect(t, IsValidAlias("/some page/"), false)
  50. }
  51. func TestIsValidSingleAlias(t *testing.T) {
  52. Expect(t, IsValidSingleAlias("some-category"), true)
  53. Expect(t, IsValidSingleAlias("some-category-12345"), true)
  54. Expect(t, IsValidSingleAlias("some_category_12345"), true)
  55. Expect(t, IsValidSingleAlias(""), false)
  56. Expect(t, IsValidSingleAlias("/"), false)
  57. Expect(t, IsValidSingleAlias("/some-category/"), false)
  58. Expect(t, IsValidSingleAlias("some-category.html"), false)
  59. Expect(t, IsValidSingleAlias("some category"), false)
  60. }
  61. func TestFixPath(t *testing.T) {
  62. Expect(t, FixPath(""), "")
  63. Expect(t, FixPath("/"), "")
  64. Expect(t, FixPath("./dir"), "./dir")
  65. Expect(t, FixPath("./dir/"), "./dir")
  66. Expect(t, FixPath("\\dir"), "\\dir")
  67. Expect(t, FixPath("\\dir\\"), "\\dir")
  68. }
  69. func TestExtractHostPort(t *testing.T) {
  70. h, p := ExtractHostPort("localhost:8080", false)
  71. Expect(t, h, "localhost")
  72. Expect(t, p, "8080")
  73. h, p = ExtractHostPort("localhost:80", false)
  74. Expect(t, h, "localhost")
  75. Expect(t, p, "80")
  76. h, p = ExtractHostPort("localhost", false)
  77. Expect(t, h, "localhost")
  78. Expect(t, p, "80")
  79. h, p = ExtractHostPort("localhost", true)
  80. Expect(t, h, "localhost")
  81. Expect(t, p, "443")
  82. }
  83. func TestGetAssetsUrl(t *testing.T) {
  84. Expect(t, GetAssetsUrl("style.css"), "/style.css?v="+consts.AssetsVersion)
  85. }
  86. func TestGetTmplSystemData(t *testing.T) {
  87. Expect(t, GetTmplSystemData(), consts.TmplSystem{
  88. PathIcoFav: "/assets/sys/fave.ico?v=" + consts.AssetsVersion,
  89. PathSvgLogo: "/assets/sys/logo.svg?v=" + consts.AssetsVersion,
  90. PathCssStyles: "/assets/sys/styles.css?v=" + consts.AssetsVersion,
  91. PathCssCpStyles: "/assets/cp/styles.css?v=" + consts.AssetsVersion,
  92. PathCssBootstrap: "/assets/bootstrap.css?v=" + consts.AssetsVersion,
  93. PathJsJquery: "/assets/jquery.js?v=" + consts.AssetsVersion,
  94. PathJsPopper: "/assets/popper.js?v=" + consts.AssetsVersion,
  95. PathJsBootstrap: "/assets/bootstrap.js?v=" + consts.AssetsVersion,
  96. PathJsCpScripts: "/assets/cp/scripts.js?v=" + consts.AssetsVersion,
  97. PathThemeStyles: "/assets/theme/styles.css",
  98. PathThemeScripts: "/assets/theme/scripts.js",
  99. InfoVersion: consts.ServerVersion,
  100. })
  101. }
  102. func TestGetTmplError(t *testing.T) {
  103. Expect(t, GetTmplError(errors.New("some error")), consts.TmplError{
  104. ErrorMessage: "some error",
  105. })
  106. }
  107. func TestGetMd5(t *testing.T) {
  108. Expect(t, GetMd5("some string"), "5ac749fbeec93607fc28d666be85e73a")
  109. }
  110. func TestGetCurrentUnixTimestamp(t *testing.T) {
  111. Expect(t, GetCurrentUnixTimestamp(), int64(time.Now().Unix()))
  112. }
  113. func TestSystemRenderTemplate(t *testing.T) {
  114. request, err := http.NewRequest("GET", "/", nil)
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. recorder := httptest.NewRecorder()
  119. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  120. SystemRenderTemplate(w, []byte(`ok`), nil)
  121. }).ServeHTTP(recorder, request)
  122. Expect(t, recorder.Code, 200)
  123. Expect(t, recorder.Body.String(), `ok`)
  124. }
  125. func TestSystemErrorPageEngine(t *testing.T) {
  126. request, err := http.NewRequest("GET", "/", nil)
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. recorder := httptest.NewRecorder()
  131. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  132. SystemErrorPageEngine(w, errors.New("Test error"))
  133. }).ServeHTTP(recorder, request)
  134. Expect(t, recorder.Code, http.StatusInternalServerError)
  135. Expect(t, strings.Contains(recorder.Body.String(), "Engine Error"), true)
  136. Expect(t, strings.Contains(recorder.Body.String(), "Test error"), true)
  137. }
  138. func TestSystemErrorPageTemplate(t *testing.T) {
  139. request, err := http.NewRequest("GET", "/", nil)
  140. if err != nil {
  141. t.Fatal(err)
  142. }
  143. recorder := httptest.NewRecorder()
  144. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  145. SystemErrorPageTemplate(w, errors.New("Test error"))
  146. }).ServeHTTP(recorder, request)
  147. Expect(t, recorder.Code, http.StatusInternalServerError)
  148. Expect(t, strings.Contains(recorder.Body.String(), "Template Error"), true)
  149. Expect(t, strings.Contains(recorder.Body.String(), "Test error"), true)
  150. }
  151. func TestSystemErrorPage404(t *testing.T) {
  152. request, err := http.NewRequest("GET", "/", nil)
  153. if err != nil {
  154. t.Fatal(err)
  155. }
  156. recorder := httptest.NewRecorder()
  157. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  158. SystemErrorPage404(w)
  159. }).ServeHTTP(recorder, request)
  160. Expect(t, recorder.Code, http.StatusNotFound)
  161. Expect(t, strings.Contains(recorder.Body.String(), "404 Not Found"), true)
  162. }
  163. func TestUrlToArray(t *testing.T) {
  164. a := UrlToArray("/some/url")
  165. Expect(t, len(a), 2)
  166. Expect(t, a[0], "some")
  167. Expect(t, a[1], "url")
  168. a = UrlToArray("/some/url/")
  169. Expect(t, len(a), 2)
  170. Expect(t, a[0], "some")
  171. Expect(t, a[1], "url")
  172. a = UrlToArray("/some/url?a=1&b=2")
  173. Expect(t, len(a), 2)
  174. Expect(t, a[0], "some")
  175. Expect(t, a[1], "url")
  176. a = UrlToArray("/some/url/?a=1&b=2")
  177. Expect(t, len(a), 2)
  178. Expect(t, a[0], "some")
  179. Expect(t, a[1], "url")
  180. }
  181. func TestIntToStr(t *testing.T) {
  182. Expect(t, IntToStr(2000), "2000")
  183. }
  184. func TestInt64ToStr(t *testing.T) {
  185. Expect(t, Int64ToStr(2000), "2000")
  186. }
  187. func TestStrToInt(t *testing.T) {
  188. Expect(t, StrToInt("2000"), 2000)
  189. Expect(t, StrToInt("string"), 0)
  190. }
  191. func TestGenerateAlias(t *testing.T) {
  192. Expect(t, GenerateAlias(""), "")
  193. Expect(t, GenerateAlias("Some page name"), "/some-page-name/")
  194. Expect(t, GenerateAlias("Some page name 2"), "/some-page-name-2/")
  195. Expect(t, GenerateAlias("Какая-то страница"), "/kakayato-stranica/")
  196. Expect(t, GenerateAlias("Какая-то страница 2"), "/kakayato-stranica-2/")
  197. }
  198. func TestGenerateSingleAlias(t *testing.T) {
  199. Expect(t, GenerateSingleAlias(""), "")
  200. Expect(t, GenerateSingleAlias("Some category name"), "some-category-name")
  201. Expect(t, GenerateSingleAlias("Some category name 2"), "some-category-name-2")
  202. Expect(t, GenerateSingleAlias("Какая-то категория"), "kakayato-kategoriya")
  203. Expect(t, GenerateSingleAlias("Какая-то категория 2"), "kakayato-kategoriya-2")
  204. }
  205. func TestUnixTimestampToMySqlDateTime(t *testing.T) {
  206. Expect(t, UnixTimestampToMySqlDateTime(1551741275), "2019-03-05 01:14:35")
  207. }
  208. func TestUnixTimestampToFormat(t *testing.T) {
  209. Expect(t, UnixTimestampToFormat(1551741275, "2006/01/02 15:04"), "2019/03/05 01:14")
  210. }
  211. func TestExtractGetParams(t *testing.T) {
  212. Expect(t, ExtractGetParams("/some-url"), "")
  213. Expect(t, ExtractGetParams("/some-url/"), "")
  214. Expect(t, ExtractGetParams("/some-url?a=1&b=2"), "?a=1&b=2")
  215. Expect(t, ExtractGetParams("/some-url/?a=1&b=2"), "?a=1&b=2")
  216. }
  217. func TestJavaScriptVarValue(t *testing.T) {
  218. Expect(t, JavaScriptVarValue(`It's "string"`), "It’s ”string”")
  219. Expect(t, JavaScriptVarValue(`It is string`), "It is string")
  220. }
  221. func TestInArrayInt(t *testing.T) {
  222. slice := []int{1, 3, 5, 9, 0}
  223. Expect(t, InArrayInt(slice, 1), true)
  224. Expect(t, InArrayInt(slice, 9), true)
  225. Expect(t, InArrayInt(slice, 2), false)
  226. Expect(t, InArrayInt(slice, 8), false)
  227. }
  228. func TestInArrayString(t *testing.T) {
  229. slice := []string{"1", "3", "5", "9", "0"}
  230. Expect(t, InArrayString(slice, "1"), true)
  231. Expect(t, InArrayString(slice, "9"), true)
  232. Expect(t, InArrayString(slice, "2"), false)
  233. Expect(t, InArrayString(slice, "8"), false)
  234. }
  235. func TestGetPostArrayInt(t *testing.T) {
  236. request, err := http.NewRequest("POST", "/", strings.NewReader("cats[]=1&cats[]=3&cats[]=5"))
  237. if err != nil {
  238. t.Fatal(err)
  239. }
  240. request.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
  241. request.ParseForm()
  242. arr := GetPostArrayInt("cats[]", request)
  243. Expect(t, fmt.Sprintf("%T%v", arr, arr), "[]int[1 3 5]")
  244. }
  245. func TestGetPostArrayString(t *testing.T) {
  246. request, err := http.NewRequest("POST", "/", strings.NewReader("cats[]=1&cats[]=3&cats[]=5"))
  247. if err != nil {
  248. t.Fatal(err)
  249. }
  250. request.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
  251. request.ParseForm()
  252. arr := GetPostArrayString("cats[]", request)
  253. Expect(t, fmt.Sprintf("%T%v", arr, arr), "[]string[1 3 5]")
  254. }
  255. func TestArrayOfIntToArrayOfString(t *testing.T) {
  256. res := ArrayOfIntToArrayOfString([]int{1, 3, 5})
  257. Expect(t, len(res), 3)
  258. Expect(t, res[0], "1")
  259. Expect(t, res[1], "3")
  260. Expect(t, res[2], "5")
  261. }
  262. func TestArrayOfStringToArrayOfInt(t *testing.T) {
  263. res := ArrayOfStringToArrayOfInt([]string{"1", "3", "5", "abc"})
  264. Expect(t, len(res), 3)
  265. Expect(t, res[0], 1)
  266. Expect(t, res[1], 3)
  267. Expect(t, res[2], 5)
  268. }