1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-11-29 08:22:11 +02:00
imgproxy/options/presets_test.go

106 lines
2.5 KiB
Go
Raw Normal View History

2021-04-26 13:52:50 +02:00
package options
2018-11-06 13:19:34 +02:00
import (
"fmt"
"testing"
2021-09-30 16:23:30 +02:00
"github.com/imgproxy/imgproxy/v3/config"
2018-11-06 13:19:34 +02:00
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
2021-04-26 13:52:50 +02:00
type PresetsTestSuite struct{ suite.Suite }
2018-11-06 13:19:34 +02:00
2021-04-26 13:52:50 +02:00
func (s *PresetsTestSuite) SetupTest() {
config.Reset()
// Reset presets
presets = make(map[string]urlOptions)
}
2018-11-06 13:19:34 +02:00
2021-04-26 13:52:50 +02:00
func (s *PresetsTestSuite) TestParsePreset() {
err := parsePreset("test=resize:fit:100:200/sharpen:2")
2018-11-06 13:19:34 +02:00
require.Nil(s.T(), err)
2022-07-18 15:48:50 +02:00
require.Equal(s.T(), urlOptions{
2019-07-22 13:27:18 +02:00
urlOption{Name: "resize", Args: []string{"fit", "100", "200"}},
urlOption{Name: "sharpen", Args: []string{"2"}},
2021-04-26 13:52:50 +02:00
}, presets["test"])
2018-11-06 13:19:34 +02:00
}
func (s *PresetsTestSuite) TestParsePresetInvalidString() {
presetStr := "resize:fit:100:200/sharpen:2"
2021-04-26 13:52:50 +02:00
err := parsePreset(presetStr)
2018-11-06 13:19:34 +02:00
2022-07-18 15:48:50 +02:00
require.Equal(s.T(), fmt.Errorf("Invalid preset string: %s", presetStr), err)
require.Empty(s.T(), presets)
2018-11-06 13:19:34 +02:00
}
func (s *PresetsTestSuite) TestParsePresetEmptyName() {
presetStr := "=resize:fit:100:200/sharpen:2"
2021-04-26 13:52:50 +02:00
err := parsePreset(presetStr)
2018-11-06 13:19:34 +02:00
2022-07-18 15:48:50 +02:00
require.Equal(s.T(), fmt.Errorf("Empty preset name: %s", presetStr), err)
require.Empty(s.T(), presets)
2018-11-06 13:19:34 +02:00
}
func (s *PresetsTestSuite) TestParsePresetEmptyValue() {
presetStr := "test="
2021-04-26 13:52:50 +02:00
err := parsePreset(presetStr)
2018-11-06 13:19:34 +02:00
2022-07-18 15:48:50 +02:00
require.Equal(s.T(), fmt.Errorf("Empty preset value: %s", presetStr), err)
require.Empty(s.T(), presets)
2018-11-06 13:19:34 +02:00
}
func (s *PresetsTestSuite) TestParsePresetInvalidValue() {
presetStr := "test=resize:fit:100:200/sharpen:2/blur"
2021-04-26 13:52:50 +02:00
err := parsePreset(presetStr)
2018-11-06 13:19:34 +02:00
2022-07-18 15:48:50 +02:00
require.Equal(s.T(), fmt.Errorf("Invalid preset value: %s", presetStr), err)
require.Empty(s.T(), presets)
2018-11-06 13:19:34 +02:00
}
func (s *PresetsTestSuite) TestParsePresetEmptyString() {
2021-04-26 13:52:50 +02:00
err := parsePreset(" ")
2018-11-06 13:19:34 +02:00
2022-07-18 15:48:50 +02:00
require.Nil(s.T(), err)
require.Empty(s.T(), presets)
2018-11-06 13:19:34 +02:00
}
func (s *PresetsTestSuite) TestParsePresetComment() {
2021-04-26 13:52:50 +02:00
err := parsePreset("# test=resize:fit:100:200/sharpen:2")
2018-11-06 13:19:34 +02:00
2022-07-18 15:48:50 +02:00
require.Nil(s.T(), err)
require.Empty(s.T(), presets)
2018-11-06 13:19:34 +02:00
}
2021-04-26 13:52:50 +02:00
func (s *PresetsTestSuite) TestValidatePresets() {
presets = map[string]urlOptions{
2024-02-16 19:37:56 +02:00
"test": {
2019-07-22 13:27:18 +02:00
urlOption{Name: "resize", Args: []string{"fit", "100", "200"}},
urlOption{Name: "sharpen", Args: []string{"2"}},
2018-11-06 13:19:34 +02:00
},
}
2021-04-26 13:52:50 +02:00
err := ValidatePresets()
2018-11-06 13:19:34 +02:00
2022-07-18 15:48:50 +02:00
require.Nil(s.T(), err)
2018-11-06 13:19:34 +02:00
}
2021-04-26 13:52:50 +02:00
func (s *PresetsTestSuite) TestValidatePresetsInvalid() {
presets = map[string]urlOptions{
2024-02-16 19:37:56 +02:00
"test": {
2019-07-22 13:27:18 +02:00
urlOption{Name: "resize", Args: []string{"fit", "-1", "-2"}},
urlOption{Name: "sharpen", Args: []string{"2"}},
2018-11-06 13:19:34 +02:00
},
}
2021-04-26 13:52:50 +02:00
err := ValidatePresets()
2018-11-06 13:19:34 +02:00
2022-07-18 15:48:50 +02:00
require.Error(s.T(), err)
2018-11-06 13:19:34 +02:00
}
func TestPresets(t *testing.T) {
suite.Run(t, new(PresetsTestSuite))
}