123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- package utils
- import (
- "errors"
- "fmt"
- "net/http"
- "net/http/httptest"
- "strings"
- "testing"
- "time"
- "golang-fave/consts"
- )
- func Expect(t *testing.T, actual, expect interface{}) {
- if actual != expect {
- t.Fatalf("\033[0;33mExpected \033[0;32m`(%T) %v`\033[0;33m but got \033[0;31m`(%T) %v`\033[0m",
- expect, expect, actual, actual)
- }
- }
- func TestIsFileExists(t *testing.T) {
- Expect(t, IsFileExists("./../support/some-file.txt"), true)
- Expect(t, IsFileExists("./../support/no-existed-file"), false)
- }
- func TestIsDir(t *testing.T) {
- Expect(t, IsDir("./../support"), true)
- Expect(t, IsDir("./../support/some-file.txt"), false)
- Expect(t, IsDir("./../support/no-existed-dir"), false)
- }
- func TestIsDirExists(t *testing.T) {
- Expect(t, IsDirExists("./../support"), true)
- Expect(t, IsDirExists("./../support/some-file.txt"), false)
- Expect(t, IsDirExists("./../support/no-existed-dir"), false)
- }
- func TestIsNumeric(t *testing.T) {
- Expect(t, IsNumeric("12345"), true)
- Expect(t, IsNumeric("string"), false)
- }
- func TestIsValidEmail(t *testing.T) {
- Expect(t, IsValidEmail("test@gmail.com"), true)
- Expect(t, IsValidEmail("test@yandex.ru"), true)
- Expect(t, IsValidEmail("test@ya.ru"), true)
- Expect(t, IsValidEmail("test@test"), false)
- }
- func TestIsValidAlias(t *testing.T) {
- Expect(t, IsValidAlias("/"), true)
- Expect(t, IsValidAlias("/some-page/"), true)
- Expect(t, IsValidAlias("/some-page.html"), true)
- Expect(t, IsValidAlias("/some-page.html/"), true)
- Expect(t, IsValidAlias(""), false)
- Expect(t, IsValidAlias("some-page"), false)
- Expect(t, IsValidAlias("/some page/"), false)
- }
- func TestIsValidSingleAlias(t *testing.T) {
- Expect(t, IsValidSingleAlias("some-category"), true)
- Expect(t, IsValidSingleAlias("some-category-12345"), true)
- Expect(t, IsValidSingleAlias("some_category_12345"), true)
- Expect(t, IsValidSingleAlias(""), false)
- Expect(t, IsValidSingleAlias("/"), false)
- Expect(t, IsValidSingleAlias("/some-category/"), false)
- Expect(t, IsValidSingleAlias("some-category.html"), false)
- Expect(t, IsValidSingleAlias("some category"), false)
- }
- func TestFixPath(t *testing.T) {
- Expect(t, FixPath(""), "")
- Expect(t, FixPath("/"), "")
- Expect(t, FixPath("./dir"), "./dir")
- Expect(t, FixPath("./dir/"), "./dir")
- Expect(t, FixPath("\\dir"), "\\dir")
- Expect(t, FixPath("\\dir\\"), "\\dir")
- }
- func TestExtractHostPort(t *testing.T) {
- h, p := ExtractHostPort("localhost:8080", false)
- Expect(t, h, "localhost")
- Expect(t, p, "8080")
- h, p = ExtractHostPort("localhost:80", false)
- Expect(t, h, "localhost")
- Expect(t, p, "80")
- h, p = ExtractHostPort("localhost", false)
- Expect(t, h, "localhost")
- Expect(t, p, "80")
- h, p = ExtractHostPort("localhost", true)
- Expect(t, h, "localhost")
- Expect(t, p, "443")
- }
- func TestGetAssetsUrl(t *testing.T) {
- Expect(t, GetAssetsUrl("style.css"), "/style.css?v="+consts.AssetsVersion)
- }
- func TestGetTmplSystemData(t *testing.T) {
- Expect(t, GetTmplSystemData(), consts.TmplSystem{
- PathIcoFav: "/assets/sys/fave.ico?v=" + consts.AssetsVersion,
- PathSvgLogo: "/assets/sys/logo.svg?v=" + consts.AssetsVersion,
- PathCssStyles: "/assets/sys/styles.css?v=" + consts.AssetsVersion,
- PathCssCpStyles: "/assets/cp/styles.css?v=" + consts.AssetsVersion,
- PathCssBootstrap: "/assets/bootstrap.css?v=" + consts.AssetsVersion,
- PathJsJquery: "/assets/jquery.js?v=" + consts.AssetsVersion,
- PathJsPopper: "/assets/popper.js?v=" + consts.AssetsVersion,
- PathJsBootstrap: "/assets/bootstrap.js?v=" + consts.AssetsVersion,
- PathJsCpScripts: "/assets/cp/scripts.js?v=" + consts.AssetsVersion,
- PathThemeStyles: "/assets/theme/styles.css",
- PathThemeScripts: "/assets/theme/scripts.js",
- InfoVersion: consts.ServerVersion,
- })
- }
- func TestGetTmplError(t *testing.T) {
- Expect(t, GetTmplError(errors.New("some error")), consts.TmplError{
- ErrorMessage: "some error",
- })
- }
- func TestGetMd5(t *testing.T) {
- Expect(t, GetMd5("some string"), "5ac749fbeec93607fc28d666be85e73a")
- }
- func TestGetCurrentUnixTimestamp(t *testing.T) {
- Expect(t, GetCurrentUnixTimestamp(), int64(time.Now().Unix()))
- }
- func TestSystemRenderTemplate(t *testing.T) {
- request, err := http.NewRequest("GET", "/", nil)
- if err != nil {
- t.Fatal(err)
- }
- recorder := httptest.NewRecorder()
- http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- SystemRenderTemplate(w, []byte(`ok`), nil)
- }).ServeHTTP(recorder, request)
- Expect(t, recorder.Code, 200)
- Expect(t, recorder.Body.String(), `ok`)
- }
- func TestSystemErrorPageEngine(t *testing.T) {
- request, err := http.NewRequest("GET", "/", nil)
- if err != nil {
- t.Fatal(err)
- }
- recorder := httptest.NewRecorder()
- http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- SystemErrorPageEngine(w, errors.New("Test error"))
- }).ServeHTTP(recorder, request)
- Expect(t, recorder.Code, http.StatusInternalServerError)
- Expect(t, strings.Contains(recorder.Body.String(), "Engine Error"), true)
- Expect(t, strings.Contains(recorder.Body.String(), "Test error"), true)
- }
- func TestSystemErrorPageTemplate(t *testing.T) {
- request, err := http.NewRequest("GET", "/", nil)
- if err != nil {
- t.Fatal(err)
- }
- recorder := httptest.NewRecorder()
- http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- SystemErrorPageTemplate(w, errors.New("Test error"))
- }).ServeHTTP(recorder, request)
- Expect(t, recorder.Code, http.StatusInternalServerError)
- Expect(t, strings.Contains(recorder.Body.String(), "Template Error"), true)
- Expect(t, strings.Contains(recorder.Body.String(), "Test error"), true)
- }
- func TestSystemErrorPage404(t *testing.T) {
- request, err := http.NewRequest("GET", "/", nil)
- if err != nil {
- t.Fatal(err)
- }
- recorder := httptest.NewRecorder()
- http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- SystemErrorPage404(w)
- }).ServeHTTP(recorder, request)
- Expect(t, recorder.Code, http.StatusNotFound)
- Expect(t, strings.Contains(recorder.Body.String(), "404 Not Found"), true)
- }
- func TestUrlToArray(t *testing.T) {
- a := UrlToArray("/some/url")
- Expect(t, len(a), 2)
- Expect(t, a[0], "some")
- Expect(t, a[1], "url")
- a = UrlToArray("/some/url/")
- Expect(t, len(a), 2)
- Expect(t, a[0], "some")
- Expect(t, a[1], "url")
- a = UrlToArray("/some/url?a=1&b=2")
- Expect(t, len(a), 2)
- Expect(t, a[0], "some")
- Expect(t, a[1], "url")
- a = UrlToArray("/some/url/?a=1&b=2")
- Expect(t, len(a), 2)
- Expect(t, a[0], "some")
- Expect(t, a[1], "url")
- }
- func TestIntToStr(t *testing.T) {
- Expect(t, IntToStr(2000), "2000")
- }
- func TestInt64ToStr(t *testing.T) {
- Expect(t, Int64ToStr(2000), "2000")
- }
- func TestStrToInt(t *testing.T) {
- Expect(t, StrToInt("2000"), 2000)
- Expect(t, StrToInt("string"), 0)
- }
- func TestGenerateAlias(t *testing.T) {
- Expect(t, GenerateAlias(""), "")
- Expect(t, GenerateAlias("Some page name"), "/some-page-name/")
- Expect(t, GenerateAlias("Some page name 2"), "/some-page-name-2/")
- Expect(t, GenerateAlias("Какая-то страница"), "/kakayato-stranica/")
- Expect(t, GenerateAlias("Какая-то страница 2"), "/kakayato-stranica-2/")
- }
- func TestGenerateSingleAlias(t *testing.T) {
- Expect(t, GenerateSingleAlias(""), "")
- Expect(t, GenerateSingleAlias("Some category name"), "some-category-name")
- Expect(t, GenerateSingleAlias("Some category name 2"), "some-category-name-2")
- Expect(t, GenerateSingleAlias("Какая-то категория"), "kakayato-kategoriya")
- Expect(t, GenerateSingleAlias("Какая-то категория 2"), "kakayato-kategoriya-2")
- }
- func TestUnixTimestampToMySqlDateTime(t *testing.T) {
- Expect(t, UnixTimestampToMySqlDateTime(1551741275), "2019-03-05 01:14:35")
- }
- func TestUnixTimestampToFormat(t *testing.T) {
- Expect(t, UnixTimestampToFormat(1551741275, "2006/01/02 15:04"), "2019/03/05 01:14")
- }
- func TestExtractGetParams(t *testing.T) {
- Expect(t, ExtractGetParams("/some-url"), "")
- Expect(t, ExtractGetParams("/some-url/"), "")
- Expect(t, ExtractGetParams("/some-url?a=1&b=2"), "?a=1&b=2")
- Expect(t, ExtractGetParams("/some-url/?a=1&b=2"), "?a=1&b=2")
- }
- func TestJavaScriptVarValue(t *testing.T) {
- Expect(t, JavaScriptVarValue(`It's "string"`), "It’s ”string”")
- Expect(t, JavaScriptVarValue(`It is string`), "It is string")
- }
- func TestInArrayInt(t *testing.T) {
- slice := []int{1, 3, 5, 9, 0}
- Expect(t, InArrayInt(slice, 1), true)
- Expect(t, InArrayInt(slice, 9), true)
- Expect(t, InArrayInt(slice, 2), false)
- Expect(t, InArrayInt(slice, 8), false)
- }
- func TestInArrayString(t *testing.T) {
- slice := []string{"1", "3", "5", "9", "0"}
- Expect(t, InArrayString(slice, "1"), true)
- Expect(t, InArrayString(slice, "9"), true)
- Expect(t, InArrayString(slice, "2"), false)
- Expect(t, InArrayString(slice, "8"), false)
- }
- func TestGetPostArrayInt(t *testing.T) {
- request, err := http.NewRequest("POST", "/", strings.NewReader("cats[]=1&cats[]=3&cats[]=5"))
- if err != nil {
- t.Fatal(err)
- }
- request.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
- request.ParseForm()
- arr := GetPostArrayInt("cats[]", request)
- Expect(t, fmt.Sprintf("%T%v", arr, arr), "[]int[1 3 5]")
- }
- func TestGetPostArrayString(t *testing.T) {
- request, err := http.NewRequest("POST", "/", strings.NewReader("cats[]=1&cats[]=3&cats[]=5"))
- if err != nil {
- t.Fatal(err)
- }
- request.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
- request.ParseForm()
- arr := GetPostArrayString("cats[]", request)
- Expect(t, fmt.Sprintf("%T%v", arr, arr), "[]string[1 3 5]")
- }
- func TestArrayOfIntToArrayOfString(t *testing.T) {
- res := ArrayOfIntToArrayOfString([]int{1, 3, 5})
- Expect(t, len(res), 3)
- Expect(t, res[0], "1")
- Expect(t, res[1], "3")
- Expect(t, res[2], "5")
- }
- func TestArrayOfStringToArrayOfInt(t *testing.T) {
- res := ArrayOfStringToArrayOfInt([]string{"1", "3", "5", "abc"})
- Expect(t, len(res), 3)
- Expect(t, res[0], 1)
- Expect(t, res[1], 3)
- Expect(t, res[2], 5)
- }
|