forked from yano3/oyaki
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webp_test.go
82 lines (69 loc) · 1.61 KB
/
webp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestProxyWebP(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(proxy))
origin := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./testdata/oyaki.jpg")
}))
orgSrvURL = origin.URL
url := ts.URL + "/oyaki.jpg.webp"
req, _ := http.NewRequest("GET", url, nil)
resp, err := doWebp(req)
if err != nil {
t.Fatal(err)
} else {
io.ReadAll(resp.Body)
resp.Body.Close()
}
// match with origin file info
if resp.Header.Get("Content-Type") != "image/jpeg" {
t.Error("wrong header Content-Type")
t.Error(resp.Header)
}
}
func TestConvJPG2WebP(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(proxy))
origin := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./testdata/oyaki.jpg")
}))
orgSrvURL = origin.URL
url := ts.URL + "/oyaki.jpg.webp"
req, _ := http.NewRequest("GET", url, nil)
resp, err := doWebp(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
_, err = convWebp(resp.Body, 90)
if err != nil {
t.Fatal(err)
}
}
func BenchmarkConvJPG2WebP_bimg(b *testing.B) {
f, err := os.Open("./testdata/oyaki.jpg")
if err != nil {
b.Fatal("failed to open testdata")
}
defer f.Close()
// to re-use src bytes
src, err := io.ReadAll(f)
if err != nil {
b.Fatal("failed to open testdata")
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
srcBuf := bytes.NewBuffer(src)
b.StartTimer()
if _, err = convWebp(srcBuf, 90); err != nil {
b.Fail()
}
}
}