Skip to content

Commit 6f8d978

Browse files
committed
isSafari test case
1 parent c26dfb9 commit 6f8d978

File tree

4 files changed

+49
-38
lines changed

4 files changed

+49
-38
lines changed

helper.go

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"fmt"
5-
"github.com/gofiber/fiber"
65
log "github.com/sirupsen/logrus"
76
"net/http"
87
"os"
@@ -62,44 +61,16 @@ func GenWebpAbs(RawImagePath string, ExhaustPath string, ImgFilename string, req
6261
return cwd, WebpAbsolutePath
6362
}
6463

65-
func CheckUA(c *fiber.Ctx, RawImageAbs string) (string, bool) {
66-
// reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent/Firefox
67-
// https://developer.chrome.com/multidevice/user-agent#chrome_for_ios_user_agent
68-
// Chrome
69-
// ✅ Windows: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36
70-
// ✅ macOS: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36
71-
// ✅ Linux: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36
72-
// ✅ iOS: Mozilla/5.0 (iPhone; CPU iPhone OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/83.0.4103.63 Mobile/15E148 Safari/604.1
73-
// ✅ Android: Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.60 Mobile Safari/537.36
74-
75-
// Firefox
76-
// ✅ Windows: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0
77-
// ✅ macOS: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:76.0) Gecko/20100101 Firefox/76.0
78-
// ✅ Linux: Mozilla/5.0 (X11; Linux i686; rv:76.0) Gecko/20100101 Firefox/76.0
79-
// ✅ iOS: Mozilla/5.0 (iPad; CPU OS 10_15_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/25.0 Mobile/15E148 Safari/605.1.15
80-
// ✅ Android: Mozilla/5.0 (Android 10; Mobile; rv:68.0) Gecko/68.0 Firefox/68.0
81-
82-
// Safari
83-
// ❎ macOS: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15
84-
// ❎ iOS: Mozilla/5.0 (iPad; CPU OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1
85-
86-
// WeChat
87-
// ❎ iOS: 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
88-
// ✅ Windows: 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
89-
// ✅ Android: 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
90-
91-
UA := c.Get("User-Agent")
92-
64+
func isSafari(UA string) bool {
65+
// for more information, please check test case
9366
if strings.Contains(UA, "Firefox") || strings.Contains(UA, "Chrome") {
9467
// Chrome or firefox on macOS Windows
9568
} else if strings.Contains(UA, "Android") || strings.Contains(UA, "Windows") || strings.Contains(UA, "Linux") {
9669
// on Android, Windows and Linux
9770
} else if strings.Contains(UA, "FxiOS") || strings.Contains(UA, "CriOS") {
9871
//firefox and Chrome on iOS
9972
} else {
100-
log.Infof("A Safari user has arrived...%s", UA)
101-
c.SendFile(RawImageAbs)
102-
return "", true
73+
return true
10374
}
104-
return UA, false
75+
return false
10576
}

helper_test.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"testing"
66
)
77

8-
// test this file: go test helper_test.go helper.go -v
8+
// test this file: go test -v -cover helper_test.go helper.go
99
// test one function: go test -run TestGetFileContentType helper_test.go helper.go -v
1010
func TestGetFileContentType(t *testing.T) {
1111
var data = []byte("hello")
@@ -34,7 +34,7 @@ func TestImageExists(t *testing.T) {
3434
var result = !ImageExists(data)
3535

3636
if result {
37-
t.Errorf("Result: [%v], Expected: [%v]", result, true)
37+
t.Errorf("Result: [%v], Expected: [%v]", result, false)
3838
}
3939
data = ".pics/empty2.jpg"
4040
result = ImageExists(data)
@@ -57,3 +57,41 @@ func TestGenWebpAbs(t *testing.T) {
5757

5858
}
5959
}
60+
61+
func TestisSafari(t *testing.T) {
62+
// reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent/Firefox
63+
// https://developer.chrome.com/multidevice/user-agent#chrome_for_ios_user_agent
64+
65+
var testCase = map[string]bool{
66+
// Chrome on Windows, macOS, linux, iOS and Android
67+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36": false,
68+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36": false,
69+
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36": false,
70+
"Mozilla/5.0 (iPhone; CPU iPhone OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/83.0.4103.63 Mobile/15E148 Safari/604.1": false,
71+
"Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.60 Mobile Safari/537.36": false,
72+
73+
// Firefox on Windows, macOS, linux, iOS and Android
74+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0": false,
75+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:76.0) Gecko/20100101 Firefox/76.0": false,
76+
"Mozilla/5.0 (X11; Linux i686; rv:76.0) Gecko/20100101 Firefox/76.0": false,
77+
"Mozilla/5.0 (iPad; CPU OS 10_15_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/25.0 Mobile/15E148 Safari/605.1.15": false,
78+
"Mozilla/5.0 (Android 10; Mobile; rv:68.0) Gecko/68.0 Firefox/68.0": false,
79+
80+
// Safari on macOS and iOS
81+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15": true,
82+
"Mozilla/5.0 (iPad; CPU OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1": true,
83+
84+
// WeChat on iOS, Windows, and Android
85+
"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,
86+
"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,
87+
"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,
88+
}
89+
90+
for browser, is := range testCase {
91+
92+
if is != isSafari(browser) {
93+
t.Errorf("[%v]:[%s]", is, browser)
94+
}
95+
}
96+
97+
}

router.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY
1919
var RawImageAbs = path.Join(ImgPath, reqURI) // /home/xxx/mypic/123.jpg
2020
var ImgFilename = path.Base(reqURI) // pure filename, 123.jpg
2121
var finalFile string // We'll only need one c.sendFile()
22-
23-
UA, done := CheckUA(c, RawImageAbs)
22+
var UA = c.Get("User-Agent")
23+
done := isSafari(UA)
2424
if done {
25+
log.Infof("A Safari user has arrived...%s", UA)
26+
c.SendFile(RawImageAbs)
2527
return
2628
}
2729
log.Debugf("Incoming connection from %s@%s with %s", UA, c.IP(), ImgFilename)

webp-server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Config struct {
2121
ExhaustPath string `json:"EXHAUST_PATH"`
2222
}
2323

24-
const version = "0.1.2"
24+
const version = "0.1.3"
2525

2626
var configPath string
2727
var prefetch bool

0 commit comments

Comments
 (0)