utils_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 TestIsFloat(t *testing.T) {
  37. Expect(t, IsFloat("12345"), true)
  38. Expect(t, IsFloat("1.23"), true)
  39. Expect(t, IsFloat("1,23"), false)
  40. Expect(t, IsFloat("string"), false)
  41. }
  42. func TestIsValidEmail(t *testing.T) {
  43. Expect(t, IsValidEmail("test@gmail.com"), true)
  44. Expect(t, IsValidEmail("test@yandex.ru"), true)
  45. Expect(t, IsValidEmail("test@ya.ru"), true)
  46. Expect(t, IsValidEmail("test@test"), false)
  47. }
  48. func TestIsValidAlias(t *testing.T) {
  49. Expect(t, IsValidAlias("/"), true)
  50. Expect(t, IsValidAlias("/some-page/"), true)
  51. Expect(t, IsValidAlias("/some-page.html"), true)
  52. Expect(t, IsValidAlias("/some-page.html/"), true)
  53. Expect(t, IsValidAlias(""), false)
  54. Expect(t, IsValidAlias("some-page"), false)
  55. Expect(t, IsValidAlias("/some page/"), false)
  56. Expect(t, IsValidAlias("/cp"), false)
  57. Expect(t, IsValidAlias("/cp/"), false)
  58. Expect(t, IsValidAlias("/cp/some"), false)
  59. Expect(t, IsValidAlias("/cp-1"), true)
  60. Expect(t, IsValidAlias("/cp-some"), true)
  61. Expect(t, IsValidAlias("/blog"), false)
  62. Expect(t, IsValidAlias("/blog/"), false)
  63. Expect(t, IsValidAlias("/blog/some"), false)
  64. Expect(t, IsValidAlias("/blog-1"), true)
  65. Expect(t, IsValidAlias("/blog-some"), true)
  66. Expect(t, IsValidAlias("/shop"), false)
  67. Expect(t, IsValidAlias("/shop/"), false)
  68. Expect(t, IsValidAlias("/shop/some"), false)
  69. Expect(t, IsValidAlias("/shop-1"), true)
  70. Expect(t, IsValidAlias("/shop-some"), true)
  71. Expect(t, IsValidAlias("/api"), false)
  72. Expect(t, IsValidAlias("/api/"), false)
  73. Expect(t, IsValidAlias("/api/some"), false)
  74. Expect(t, IsValidAlias("/api-1"), true)
  75. Expect(t, IsValidAlias("/api-some"), true)
  76. }
  77. func TestIsValidSingleAlias(t *testing.T) {
  78. Expect(t, IsValidSingleAlias("some-category"), true)
  79. Expect(t, IsValidSingleAlias("some-category-12345"), true)
  80. Expect(t, IsValidSingleAlias("some_category_12345"), true)
  81. Expect(t, IsValidSingleAlias(""), false)
  82. Expect(t, IsValidSingleAlias("/"), false)
  83. Expect(t, IsValidSingleAlias("/some-category/"), false)
  84. Expect(t, IsValidSingleAlias("some-category.html"), false)
  85. Expect(t, IsValidSingleAlias("some category"), false)
  86. }
  87. func TestFixPath(t *testing.T) {
  88. Expect(t, FixPath(""), "")
  89. Expect(t, FixPath("/"), "")
  90. Expect(t, FixPath("./dir"), "./dir")
  91. Expect(t, FixPath("./dir/"), "./dir")
  92. Expect(t, FixPath("\\dir"), "\\dir")
  93. Expect(t, FixPath("\\dir\\"), "\\dir")
  94. }
  95. func TestExtractHostPort(t *testing.T) {
  96. h, p := ExtractHostPort("localhost:8080", false)
  97. Expect(t, h, "localhost")
  98. Expect(t, p, "8080")
  99. h, p = ExtractHostPort("localhost:80", false)
  100. Expect(t, h, "localhost")
  101. Expect(t, p, "80")
  102. h, p = ExtractHostPort("localhost", false)
  103. Expect(t, h, "localhost")
  104. Expect(t, p, "80")
  105. h, p = ExtractHostPort("localhost", true)
  106. Expect(t, h, "localhost")
  107. Expect(t, p, "443")
  108. }
  109. func TestGetAssetsUrl(t *testing.T) {
  110. Expect(t, GetAssetsUrl("style.css"), "/style.css?v="+consts.ServerVersion)
  111. }
  112. func TestGetTmplSystemData(t *testing.T) {
  113. Expect(t, GetTmplSystemData("module", "module"), consts.TmplSystem{
  114. CpModule: "module",
  115. CpSubModule: "module",
  116. InfoVersion: consts.ServerVersion,
  117. PathCssBootstrap: "/assets/bootstrap.css?v=" + consts.ServerVersion,
  118. PathCssCpCodeMirror: "/assets/cp/tmpl-editor/codemirror.css?v=" + consts.ServerVersion,
  119. PathCssCpStyles: "/assets/cp/styles.css?v=" + consts.ServerVersion,
  120. PathCssCpWysiwygPell: "/assets/cp/wysiwyg/pell.css?v=" + consts.ServerVersion,
  121. PathCssStyles: "/assets/sys/styles.css?v=" + consts.ServerVersion,
  122. PathJsBootstrap: "/assets/bootstrap.js?v=" + consts.ServerVersion,
  123. PathJsCpCodeMirror: "/assets/cp/tmpl-editor/codemirror.js?v=" + consts.ServerVersion,
  124. PathJsCpScripts: "/assets/cp/scripts.js?v=" + consts.ServerVersion,
  125. PathJsCpWysiwygPell: "/assets/cp/wysiwyg/pell.js?v=" + consts.ServerVersion,
  126. PathJsJquery: "/assets/jquery.js?v=" + consts.ServerVersion,
  127. PathJsPopper: "/assets/popper.js?v=" + consts.ServerVersion,
  128. PathSvgLogo: "/assets/sys/logo.svg?v=" + consts.ServerVersion,
  129. PathThemeScripts: "/assets/theme/scripts.js?v=" + consts.ServerVersion,
  130. PathThemeStyles: "/assets/theme/styles.css?v=" + consts.ServerVersion,
  131. PathIcoFav: "/assets/sys/fave.ico?v=" + consts.ServerVersion,
  132. PathCssLightGallery: "/assets/lightgallery.css?v=" + consts.ServerVersion,
  133. PathJsLightGallery: "/assets/lightgallery.js?v=" + consts.ServerVersion,
  134. })
  135. }
  136. func TestGetTmplError(t *testing.T) {
  137. Expect(t, GetTmplError(errors.New("some error")), consts.TmplError{
  138. ErrorMessage: "some error",
  139. })
  140. }
  141. func TestGetMd5(t *testing.T) {
  142. Expect(t, GetMd5("some string"), "5ac749fbeec93607fc28d666be85e73a")
  143. }
  144. func TestGetCurrentUnixTimestamp(t *testing.T) {
  145. Expect(t, GetCurrentUnixTimestamp(), int64(time.Now().Unix()))
  146. }
  147. func TestSystemRenderTemplate(t *testing.T) {
  148. request, err := http.NewRequest("GET", "/", nil)
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. recorder := httptest.NewRecorder()
  153. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  154. SystemRenderTemplate(w, []byte(`ok`), nil, "module", "module")
  155. }).ServeHTTP(recorder, request)
  156. Expect(t, recorder.Code, 200)
  157. Expect(t, recorder.Body.String(), `ok`)
  158. }
  159. func TestSystemErrorPageEngine(t *testing.T) {
  160. request, err := http.NewRequest("GET", "/", nil)
  161. if err != nil {
  162. t.Fatal(err)
  163. }
  164. recorder := httptest.NewRecorder()
  165. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  166. SystemErrorPageEngine(w, errors.New("Test error"))
  167. }).ServeHTTP(recorder, request)
  168. Expect(t, recorder.Code, http.StatusInternalServerError)
  169. Expect(t, strings.Contains(recorder.Body.String(), "Engine Error"), true)
  170. Expect(t, strings.Contains(recorder.Body.String(), "Test error"), true)
  171. }
  172. func TestSystemErrorPageTemplate(t *testing.T) {
  173. request, err := http.NewRequest("GET", "/", nil)
  174. if err != nil {
  175. t.Fatal(err)
  176. }
  177. recorder := httptest.NewRecorder()
  178. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  179. SystemErrorPageTemplate(w, errors.New("Test error"))
  180. }).ServeHTTP(recorder, request)
  181. Expect(t, recorder.Code, http.StatusInternalServerError)
  182. Expect(t, strings.Contains(recorder.Body.String(), "Template Error"), true)
  183. Expect(t, strings.Contains(recorder.Body.String(), "Test error"), true)
  184. }
  185. func TestSystemErrorPage404(t *testing.T) {
  186. request, err := http.NewRequest("GET", "/", nil)
  187. if err != nil {
  188. t.Fatal(err)
  189. }
  190. recorder := httptest.NewRecorder()
  191. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  192. SystemErrorPage404(w)
  193. }).ServeHTTP(recorder, request)
  194. Expect(t, recorder.Code, http.StatusNotFound)
  195. Expect(t, strings.Contains(recorder.Body.String(), "404 Not Found"), true)
  196. }
  197. func TestUrlToArray(t *testing.T) {
  198. a := UrlToArray("/some/url")
  199. Expect(t, len(a), 2)
  200. Expect(t, a[0], "some")
  201. Expect(t, a[1], "url")
  202. a = UrlToArray("/some/url/")
  203. Expect(t, len(a), 2)
  204. Expect(t, a[0], "some")
  205. Expect(t, a[1], "url")
  206. a = UrlToArray("/some/url?a=1&b=2")
  207. Expect(t, len(a), 2)
  208. Expect(t, a[0], "some")
  209. Expect(t, a[1], "url")
  210. a = UrlToArray("/some/url/?a=1&b=2")
  211. Expect(t, len(a), 2)
  212. Expect(t, a[0], "some")
  213. Expect(t, a[1], "url")
  214. }
  215. func TestIntToStr(t *testing.T) {
  216. Expect(t, IntToStr(2000), "2000")
  217. }
  218. func TestInt64ToStr(t *testing.T) {
  219. Expect(t, Int64ToStr(2000), "2000")
  220. }
  221. func TestStrToInt(t *testing.T) {
  222. Expect(t, StrToInt("2000"), 2000)
  223. Expect(t, StrToInt("string"), 0)
  224. }
  225. func TestFloat64ToStr(t *testing.T) {
  226. Expect(t, Float64ToStr(0), "0.00")
  227. Expect(t, Float64ToStr(0.5), "0.50")
  228. Expect(t, Float64ToStr(15.8100), "15.81")
  229. }
  230. func TestFloat64ToStrF(t *testing.T) {
  231. Expect(t, Float64ToStrF(0, "%.4f"), "0.0000")
  232. Expect(t, Float64ToStrF(0.5, "%.4f"), "0.5000")
  233. Expect(t, Float64ToStrF(15.8100, "%.4f"), "15.8100")
  234. }
  235. func TestStrToFloat64(t *testing.T) {
  236. Expect(t, StrToFloat64("0.00"), 0.0)
  237. Expect(t, StrToFloat64("0.5"), 0.5)
  238. Expect(t, StrToFloat64("0.50"), 0.5)
  239. Expect(t, StrToFloat64("15.8100"), 15.81)
  240. Expect(t, StrToFloat64("15.8155"), 15.8155)
  241. }
  242. func TestGenerateAlias(t *testing.T) {
  243. Expect(t, GenerateAlias(""), "")
  244. Expect(t, GenerateAlias("Some page name"), "/some-page-name/")
  245. Expect(t, GenerateAlias("Some page name 2"), "/some-page-name-2/")
  246. Expect(t, GenerateAlias("Какая-то страница"), "/kakayato-stranica/")
  247. Expect(t, GenerateAlias("Какая-то страница 2"), "/kakayato-stranica-2/")
  248. }
  249. func TestGenerateSingleAlias(t *testing.T) {
  250. Expect(t, GenerateSingleAlias(""), "")
  251. Expect(t, GenerateSingleAlias("Some category name"), "some-category-name")
  252. Expect(t, GenerateSingleAlias("Some category name 2"), "some-category-name-2")
  253. Expect(t, GenerateSingleAlias("Какая-то категория"), "kakayato-kategoriya")
  254. Expect(t, GenerateSingleAlias("Какая-то категория 2"), "kakayato-kategoriya-2")
  255. }
  256. func TestUnixTimestampToMySqlDateTime(t *testing.T) {
  257. Expect(t, UnixTimestampToMySqlDateTime(1551741275), "2019-03-05 01:14:35")
  258. }
  259. func TestUnixTimestampToFormat(t *testing.T) {
  260. Expect(t, UnixTimestampToFormat(1551741275, "2006/01/02 15:04"), "2019/03/05 01:14")
  261. }
  262. func TestExtractGetParams(t *testing.T) {
  263. Expect(t, ExtractGetParams("/some-url"), "")
  264. Expect(t, ExtractGetParams("/some-url/"), "")
  265. Expect(t, ExtractGetParams("/some-url?a=1&b=2"), "?a=1&b=2")
  266. Expect(t, ExtractGetParams("/some-url/?a=1&b=2"), "?a=1&b=2")
  267. }
  268. func TestJavaScriptVarValue(t *testing.T) {
  269. Expect(t, JavaScriptVarValue(`It's "string"`), "It’s ”string”")
  270. Expect(t, JavaScriptVarValue(`It is string`), "It is string")
  271. }
  272. func TestInArrayInt(t *testing.T) {
  273. slice := []int{1, 3, 5, 9, 0}
  274. Expect(t, InArrayInt(slice, 1), true)
  275. Expect(t, InArrayInt(slice, 9), true)
  276. Expect(t, InArrayInt(slice, 2), false)
  277. Expect(t, InArrayInt(slice, 8), false)
  278. }
  279. func TestInArrayString(t *testing.T) {
  280. slice := []string{"1", "3", "5", "9", "0"}
  281. Expect(t, InArrayString(slice, "1"), true)
  282. Expect(t, InArrayString(slice, "9"), true)
  283. Expect(t, InArrayString(slice, "2"), false)
  284. Expect(t, InArrayString(slice, "8"), false)
  285. }
  286. func TestGetPostArrayInt(t *testing.T) {
  287. request, err := http.NewRequest("POST", "/", strings.NewReader("cats[]=1&cats[]=3&cats[]=5"))
  288. if err != nil {
  289. t.Fatal(err)
  290. }
  291. request.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
  292. request.ParseForm()
  293. arr := GetPostArrayInt("cats[]", request)
  294. Expect(t, fmt.Sprintf("%T%v", arr, arr), "[]int[1 3 5]")
  295. }
  296. func TestGetPostArrayString(t *testing.T) {
  297. request, err := http.NewRequest("POST", "/", strings.NewReader("cats[]=1&cats[]=3&cats[]=5"))
  298. if err != nil {
  299. t.Fatal(err)
  300. }
  301. request.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
  302. request.ParseForm()
  303. arr := GetPostArrayString("cats[]", request)
  304. Expect(t, fmt.Sprintf("%T%v", arr, arr), "[]string[1 3 5]")
  305. }
  306. func TestArrayOfIntToArrayOfString(t *testing.T) {
  307. res := ArrayOfIntToArrayOfString([]int{1, 3, 5})
  308. Expect(t, len(res), 3)
  309. Expect(t, res[0], "1")
  310. Expect(t, res[1], "3")
  311. Expect(t, res[2], "5")
  312. }
  313. func TestArrayOfStringToArrayOfInt(t *testing.T) {
  314. res := ArrayOfStringToArrayOfInt([]string{"1", "3", "5", "abc"})
  315. Expect(t, len(res), 3)
  316. Expect(t, res[0], 1)
  317. Expect(t, res[1], 3)
  318. Expect(t, res[2], 5)
  319. }
  320. func TestGetImagePlaceholderSrc(t *testing.T) {
  321. Expect(t, GetImagePlaceholderSrc(), "/assets/sys/placeholder.png")
  322. }
  323. func TestFormatProductPrice(t *testing.T) {
  324. Expect(t, FormatProductPrice(123.4567, 0, 0), "123")
  325. Expect(t, FormatProductPrice(123.4567, 0, 1), "124")
  326. Expect(t, FormatProductPrice(123.4567, 0, 2), "123")
  327. Expect(t, FormatProductPrice(123.4567, 1, 0), "123.5")
  328. Expect(t, FormatProductPrice(123.4567, 2, 0), "123.46")
  329. Expect(t, FormatProductPrice(123.4567, 3, 0), "123.457")
  330. Expect(t, FormatProductPrice(123.4567, 4, 0), "123.4567")
  331. Expect(t, FormatProductPrice(123.4567, 1, 1), "124.0")
  332. Expect(t, FormatProductPrice(123.4567, 2, 1), "124.00")
  333. Expect(t, FormatProductPrice(123.4567, 3, 1), "124.000")
  334. Expect(t, FormatProductPrice(123.4567, 4, 1), "124.0000")
  335. Expect(t, FormatProductPrice(123.4567, 1, 2), "123.0")
  336. Expect(t, FormatProductPrice(123.4567, 2, 2), "123.00")
  337. Expect(t, FormatProductPrice(123.4567, 3, 2), "123.000")
  338. Expect(t, FormatProductPrice(123.4567, 4, 2), "123.0000")
  339. }