From 07f2fc69696473865f023236903106722bf9026a Mon Sep 17 00:00:00 2001 From: Benny Date: Wed, 1 Jul 2020 21:39:28 +0800 Subject: [PATCH] IE and assert fix (#49) * Add test case for IE, postman and curl * Simplify goOrigin code * use assert --- go.mod | 1 + helper.go | 8 ++------ helper_test.go | 37 +++++++++++++++++-------------------- 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index 40503a6..de70ada 100644 --- a/go.mod +++ b/go.mod @@ -6,5 +6,6 @@ require ( github.com/chai2010/webp v1.1.0 github.com/gofiber/fiber v1.4.0 github.com/sirupsen/logrus v1.6.0 + github.com/stretchr/testify v1.3.0 golang.org/x/image v0.0.0-20200119044424-58c23975cae1 ) diff --git a/helper.go b/helper.go index 5125b76..e398277 100644 --- a/helper.go +++ b/helper.go @@ -77,16 +77,12 @@ func goOrigin(UA string) bool { // for more information, please check test case if strings.Contains(UA, "Firefox") || strings.Contains(UA, "Chrome") { // Chrome or firefox on macOS Windows - } else if strings.Contains(UA, "Android") || strings.Contains(UA, "Windows") || strings.Contains(UA, "Linux") { - // on Android, Windows and Linux + } else if strings.Contains(UA, "Android") || strings.Contains(UA, "Linux") { + // on Android and Linux } else if strings.Contains(UA, "FxiOS") || strings.Contains(UA, "CriOS") { //firefox and Chrome on iOS } else { return true } - if strings.Contains(UA, "rv:11.0") || strings.Contains(UA, "MSIE") { - // MSIE - return true - } return false } diff --git a/helper_test.go b/helper_test.go index 490f728..0fe27a4 100644 --- a/helper_test.go +++ b/helper_test.go @@ -4,6 +4,7 @@ import ( "strings" "testing" ) +import "github.com/stretchr/testify/assert" // test this file: go test -v -cover helper_test.go helper.go // test one function: go test -run TestGetFileContentType helper_test.go helper.go -v @@ -12,9 +13,7 @@ func TestGetFileContentType(t *testing.T) { var expected = "text/plain; charset=utf-8" var result = GetFileContentType(data) - if result != expected { - t.Errorf("Result: [%s], Expected: [%s]", result, expected) - } + assert.Equalf(t, result, expected, "Result: [%s], Expected: [%s]", result, expected) } @@ -23,10 +22,8 @@ func TestFileCount(t *testing.T) { var data = ".github" var expected = 2 var result = FileCount(data) + assert.Equalf(t, result, expected, "Result: [%d], Expected: [%d]", result, expected) - if result != expected { - t.Errorf("Result: [%d], Expected: [%d]", result, expected) - } } func TestImageExists(t *testing.T) { @@ -39,9 +36,7 @@ func TestImageExists(t *testing.T) { data = ".pics/empty2.jpg" result = ImageExists(data) - if result { - t.Errorf("Result: [%v], Expected: [%v]", result, false) - } + assert.Falsef(t, result, "Result: [%v], Expected: [%v]", result, false) } @@ -52,10 +47,9 @@ func TestGenWebpAbs(t *testing.T) { t.Logf("Result: [%v], Expected: [%v]", cwd, "webp_server_go") } var parts = strings.Split(cooked, ".") - if parts[0] != "/tmp/test" || parts[2] != "webp" { - t.Errorf("Result: [%v], Expected: [%v]", cooked, "/tmp/test..webp") - } + assert.Equalf(t, parts[0], "/tmp/test", "Result: [%v], Expected: [%v]", cooked, "/tmp/test..webp") + assert.Equalf(t, parts[2], "webp", "Result: [%v], Expected: [%v]", cooked, "/tmp/test..webp") } func TestGenEtag(t *testing.T) { @@ -63,11 +57,9 @@ func TestGenEtag(t *testing.T) { var expected = "W/\"1020764-262C0329\"" var result = GenEtag(data) - if result != expected { - t.Errorf("Result: [%s], Expected: [%s]", result, expected) - } -} + assert.Equalf(t, result, expected, "Result: [%s], Expected: [%s]", result, expected) +} func TestGoOrigin(t *testing.T) { // reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent/Firefox @@ -96,13 +88,18 @@ func TestGoOrigin(t *testing.T) { "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 wxwork/2.1.5 MicroMessenger/6.3.22": true, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat QBCore/3.43.691.400 QQBrowser/9.0.2524.400": false, "Mozilla/5.0 (Linux; Android 7.0; LG-H831 Build/NRD90U; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/68.0.3440.91 Mobile Safari/537.36 MicroMessenger/6.6.7.1303(0x26060743) NetType/WIFI Language/zh_TW": false, + + // IE + "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko": true, + "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)": true, + + // Others + "PostmanRuntime/7.26.1": true, + "curl/7.64.1": true, } for browser, is := range testCase { - - if is != goOrigin(browser) { - t.Errorf("[%v]:[%s]", is, browser) - } + assert.Equalf(t, is, goOrigin(browser), "[%v]:[%s]", is, browser) } }