utils_test.go 14 KB

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