test(imagorvideo): fallback image tests

This commit is contained in:
Adrian Shum 2022-09-16 21:56:09 +08:00 committed by GitHub
parent d9c697a0b2
commit 035cf78b96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 47 additions and 14 deletions

View file

@ -17,6 +17,7 @@ import (
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
)
@ -28,8 +29,9 @@ func init() {
}
type test struct {
name string
path string
name string
path string
expectCode int
}
func TestProcessor(t *testing.T) {
@ -38,8 +40,7 @@ func TestProcessor(t *testing.T) {
t.Cleanup(func() {
require.NoError(t, v.Shutdown(context.Background()))
})
var resultDir = filepath.Join(testDataDir, "golden/result")
doGoldenTests(t, resultDir, []test{
doGoldenTests(t, filepath.Join(testDataDir, "golden/result"), []test{
{name: "mkv", path: "fit-in/100x100/everybody-betray-me.mkv"},
{name: "mkv meta", path: "meta/everybody-betray-me.mkv"},
{name: "mp4", path: "fit-in/100x100/schizo_0.mp4"},
@ -47,17 +48,21 @@ func TestProcessor(t *testing.T) {
{name: "mp4 180", path: "fit-in/100x100/schizo_180.mp4"},
{name: "mp4 270", path: "fit-in/100x100/schizo_270.mp4"},
{name: "image", path: "fit-in/100x100/demo.png"},
}, WithDebug(true))
{name: "corrupted", path: "fit-in/100x100/corrupt/everybody-betray-me.mkv", expectCode: 406},
}, WithDebug(true), WithLogger(zap.NewExample()))
doGoldenTests(t, filepath.Join(testDataDir, "golden/result-fallback-image"), []test{
{name: "corrupted with fallback image", path: "fit-in/100x100/corrupt/everybody-betray-me.mkv", expectCode: 406},
}, WithDebug(false), WithLogger(zap.NewExample()), WithFallbackImage("demo.png"))
}
func doGoldenTests(t *testing.T, resultDir string, tests []test, opts ...Option) {
resStorage := filestorage.New(resultDir,
filestorage.WithSaveErrIfExists(true))
loader := filestorage.New(testDataDir)
fileLoader := filestorage.New(testDataDir)
loaders := []imagor.Loader{
loader,
fileLoader,
loaderFunc(func(r *http.Request, image string) (blob *imagor.Blob, err error) {
image, _ = loader.Path(image)
image, _ = fileLoader.Path(image)
return imagor.NewBlob(func() (reader io.ReadCloser, size int64, err error) {
// force read full file by 0 size
reader, err = os.Open(image)
@ -67,7 +72,21 @@ func doGoldenTests(t *testing.T, resultDir string, tests []test, opts ...Option)
}
for i, loader := range loaders {
app := imagor.New(
imagor.WithLoaders(loader),
imagor.WithLoaders(loaderFunc(func(r *http.Request, image string) (blob *imagor.Blob, err error) {
if strings.HasPrefix(image, "corrupt/") {
image, _ = fileLoader.Path(strings.TrimPrefix(image, "corrupt/"))
return imagor.NewBlob(func() (reader io.ReadCloser, size int64, err error) {
file, err := os.Open(image)
// truncate so it corrupt
reader = &readCloser{
Reader: io.LimitReader(file, 1024),
Closer: file,
}
return
}), nil
}
return nil, imagor.ErrNotFound
}), loader),
imagor.WithUnsafe(true),
imagor.WithDebug(true),
imagor.WithLogger(zap.NewExample()),
@ -85,7 +104,11 @@ func doGoldenTests(t *testing.T, resultDir string, tests []test, opts ...Option)
http.MethodGet, fmt.Sprintf("/unsafe/%s", tt.path), nil).WithContext(ctx)
app.ServeHTTP(w, req)
cancel()
assert.Equal(t, 200, w.Code)
if tt.expectCode == 0 {
assert.Equal(t, 200, w.Code)
} else {
assert.Equal(t, tt.expectCode, w.Code)
}
b := imagor.NewBlobFromBytes(w.Body.Bytes())
_ = resStorage.Put(context.Background(), tt.path, b)
path := filepath.Join(resultDir, imagorpath.Normalize(tt.path, nil))
@ -119,3 +142,8 @@ type loaderFunc func(r *http.Request, image string) (blob *imagor.Blob, err erro
func (f loaderFunc) Get(r *http.Request, image string) (*imagor.Blob, error) {
return f(r, image)
}
type readCloser struct {
io.Reader
io.Closer
}