GoBlog/mediaCompression_test.go

39 lines
1.0 KiB
Go
Raw Normal View History

2021-06-20 13:18:02 +00:00
package main
import (
2022-02-11 18:39:07 +00:00
"crypto/sha256"
"fmt"
2021-06-20 13:18:02 +00:00
"io"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_compress(t *testing.T) {
fakeFileContent := "Test"
2022-02-11 18:39:07 +00:00
hash := sha256.New()
2022-02-11 23:48:59 +00:00
_, _ = io.WriteString(hash, fakeFileContent)
2022-02-11 18:39:07 +00:00
fakeSha256 := fmt.Sprintf("%x", hash.Sum(nil))
2021-06-20 13:18:02 +00:00
var uf mediaStorageSaveFunc = func(filename string, f io.Reader) (location string, err error) {
2021-06-20 13:18:02 +00:00
return "https://example.com/" + filename, nil
}
t.Run("Cloudflare", func(t *testing.T) {
fakeClient := newFakeHttpClient()
2021-06-20 13:18:02 +00:00
fakeClient.setHandler(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
assert.Equal(t, "https://www.cloudflare.com/cdn-cgi/image/f=jpeg,q=75,metadata=none,fit=scale-down,w=2000,h=3000/https://example.com/original.jpg", r.URL.String())
rw.WriteHeader(http.StatusOK)
2022-02-11 18:39:07 +00:00
_, _ = io.WriteString(rw, fakeFileContent)
2021-06-20 13:18:02 +00:00
}))
cf := &cloudflare{}
res, err := cf.compress("https://example.com/original.jpg", uf, fakeClient.Client)
2021-06-20 13:18:02 +00:00
assert.Nil(t, err)
assert.Equal(t, "https://example.com/"+fakeSha256+".jpeg", res)
})
}