Skip to content

Commit 14f99bf

Browse files
authored
Merge pull request #44 from webp-sh/unit-test
Added Unit Test to Travis CI
2 parents e99e918 + acf53d2 commit 14f99bf

File tree

8 files changed

+182
-43
lines changed

8 files changed

+182
-43
lines changed

.travis.yml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,28 @@ language: go
22

33
go:
44
- 1.13
5-
5+
6+
env: GO111MODULE=on
67
arch:
78
- amd64
89
- arm64
9-
1010
os:
1111
- windows
1212
- osx
1313
- linux
1414

15-
env: GO111MODULE=on
15+
script: sh scripts/build.sh $TRAVIS_OS_NAME $TRAVIS_CPU_ARCH
1616

17-
script:
18-
- sh scripts/build.sh $TRAVIS_OS_NAME $TRAVIS_CPU_ARCH
17+
jobs:
18+
include:
19+
- stage: "Unit Tests"
20+
arch:
21+
- amd64
22+
os:
23+
- linux
24+
script:
25+
- go test -v -cover encoder_test.go encoder.go helper.go
26+
- go test -v -cover helper_test.go helper.go
1927

2028
deploy:
2129
provider: releases

encoder_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
)
9+
10+
//go test -v -cover encoder_test.go encoder.go helper.go
11+
func TestWebpEncoder(t *testing.T) {
12+
13+
var webp = "/tmp/test-result.webp"
14+
var target = walker()
15+
16+
for _, f := range target {
17+
//fmt.Println(b, c, webp)
18+
runEncoder(t, f, webp)
19+
}
20+
_ = os.Remove(webp)
21+
22+
}
23+
24+
func walker() []string {
25+
var list []string
26+
_ = filepath.Walk("./pics", func(path string, info os.FileInfo, err error) error {
27+
if !info.IsDir() {
28+
list = append(list, path)
29+
}
30+
return nil
31+
})
32+
return list
33+
}
34+
35+
func runEncoder(t *testing.T, file string, webp string) {
36+
var c chan int
37+
//t.Logf("Convert from %s to %s", file, webp)
38+
var err = WebpEncoder(file, webp, 80, false, c)
39+
if file == "pics/empty.jpg" && err != nil {
40+
t.Log("Empty file, that's okay.")
41+
} else if err != nil {
42+
t.Fatalf("Fatal, convert failed for %s: %v ", file, err)
43+
}
44+
45+
data, _ := ioutil.ReadFile(webp)
46+
types := GetFileContentType(data[:512])
47+
if types != "image/webp" {
48+
t.Fatal("Fatal, file type is wrong!")
49+
}
50+
51+
}

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: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
// test this file: go test -v -cover helper_test.go helper.go
9+
// test one function: go test -run TestGetFileContentType helper_test.go helper.go -v
10+
func TestGetFileContentType(t *testing.T) {
11+
var data = []byte("hello")
12+
var expected = "text/plain; charset=utf-8"
13+
var result = GetFileContentType(data)
14+
15+
if result != expected {
16+
t.Errorf("Result: [%s], Expected: [%s]", result, expected)
17+
}
18+
19+
}
20+
21+
// TODO: make a universal logging function
22+
func TestFileCount(t *testing.T) {
23+
var data = ".github"
24+
var expected = 2
25+
var result = FileCount(data)
26+
27+
if result != expected {
28+
t.Errorf("Result: [%d], Expected: [%d]", result, expected)
29+
}
30+
}
31+
32+
func TestImageExists(t *testing.T) {
33+
var data = "./pics/empty.jpg"
34+
var result = !ImageExists(data)
35+
36+
if result {
37+
t.Errorf("Result: [%v], Expected: [%v]", result, false)
38+
}
39+
data = ".pics/empty2.jpg"
40+
result = ImageExists(data)
41+
42+
if result {
43+
t.Errorf("Result: [%v], Expected: [%v]", result, false)
44+
}
45+
46+
}
47+
48+
func TestGenWebpAbs(t *testing.T) {
49+
cwd, cooked := GenWebpAbs("./pics/webp_server.png", "/tmp",
50+
"test", "a")
51+
if !strings.Contains(cwd, "webp_server_go") {
52+
t.Logf("Result: [%v], Expected: [%v]", cwd, "webp_server_go")
53+
}
54+
var parts = strings.Split(cooked, ".")
55+
if parts[0] != "/tmp/test" || parts[2] != "webp" {
56+
t.Errorf("Result: [%v], Expected: [%v]", cooked, "/tmp/test.<ts>.webp")
57+
58+
}
59+
}
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)

scripts/unit_test.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
# bash scripts/unit_test.sh
4+
# check $? for success or failure
5+
go test -v -cover encoder_test.go encoder.go helper.go
6+
go test -v -cover helper_test.go helper.go
7+
8+
# if [[ $? -ne 0 ]] ; then
9+
# echo "TEST FAILED!!! PLEASE DOUBLE CHECK."
10+
# fi

update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func autoUpdate() {
1616
defer func() {
1717
if err := recover(); err != nil {
18-
log.Errorf("Download error.", err)
18+
log.Errorf("Download error: %s", err)
1919
}
2020
}()
2121

@@ -45,7 +45,7 @@ func autoUpdate() {
4545
log.Info("Downloading binary to update...")
4646
resp, _ := http.Get(releaseUrl)
4747
if resp.StatusCode != 200 {
48-
log.Debug("%s-%s not found on release.", runtime.GOOS, runtime.GOARCH)
48+
log.Debugf("%s-%s not found on release.", runtime.GOOS, runtime.GOARCH)
4949
return
5050
}
5151
data, _ := ioutil.ReadAll(resp.Body)

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)