Merge pull request #897 from klipitkas/custom-type-compress-middleware

Add custom types to compress middleware
pull/901/head
Fenny 2020-10-08 18:13:49 +02:00 committed by GitHub
commit d4750fbf2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 18 deletions

View File

@ -12,30 +12,31 @@ type Config struct {
// Optional. Default: nil
Next func(c *fiber.Ctx) bool
// CompressLevel determines the compression algoritm
// Level determines the compression algorithm
//
// Optional. Default: LevelDefault
// LevelDisabled: -1
// LevelDefault: 0
// LevelBestSpeed: 1
// LevelBestCompression: 2
Level int
Level Level
}
type Level int
const (
LevelDisabled Level = -1
LevelDefault Level = 0
LevelBestSpeed Level = 1
LevelBestCompression Level = 2
)
// ConfigDefault is the default config
var ConfigDefault = Config{
Next: nil,
Level: LevelDefault,
}
// Compression levels
const (
LevelDisabled = -1
LevelDefault = 0
LevelBestSpeed = 1
LevelBestCompression = 2
)
// New creates a new middleware handler
func New(config ...Config) fiber.Handler {
// Set default config
@ -61,13 +62,22 @@ func New(config ...Config) fiber.Handler {
switch cfg.Level {
case LevelDefault:
// LevelDefault
compressor = fasthttp.CompressHandlerBrotliLevel(fctx, fasthttp.CompressBrotliDefaultCompression, fasthttp.CompressDefaultCompression)
compressor = fasthttp.CompressHandlerBrotliLevel(fctx,
fasthttp.CompressBrotliDefaultCompression,
fasthttp.CompressDefaultCompression,
)
case LevelBestSpeed:
// LevelBestSpeed
compressor = fasthttp.CompressHandlerBrotliLevel(fctx, fasthttp.CompressBrotliBestSpeed, fasthttp.CompressBestSpeed)
compressor = fasthttp.CompressHandlerBrotliLevel(fctx,
fasthttp.CompressBrotliBestSpeed,
fasthttp.CompressBestSpeed,
)
case LevelBestCompression:
// LevelBestCompression
compressor = fasthttp.CompressHandlerBrotliLevel(fctx, fasthttp.CompressBrotliBestCompression, fasthttp.CompressBestCompression)
compressor = fasthttp.CompressHandlerBrotliLevel(fctx,
fasthttp.CompressBrotliBestCompression,
fasthttp.CompressBestCompression,
)
default:
// LevelDisabled
return func(c *fiber.Ctx) error {

View File

@ -40,7 +40,7 @@ func Test_Compress_Gzip(t *testing.T) {
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
utils.AssertEqual(t, "gzip", resp.Header.Get(fiber.HeaderContentEncoding))
// Validate the file size is shrinked
// Validate that the file size has shrunk
body, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, len(body) < len(filedata))
@ -48,7 +48,7 @@ func Test_Compress_Gzip(t *testing.T) {
// go test -run Test_Compress_Different_Level
func Test_Compress_Different_Level(t *testing.T) {
levels := []int{LevelBestSpeed, LevelBestCompression, 10}
levels := []Level{LevelBestSpeed, LevelBestCompression}
for _, level := range levels {
t.Run(fmt.Sprintf("level %d", level), func(t *testing.T) {
app := fiber.New()
@ -86,7 +86,7 @@ func Test_Compress_Deflate(t *testing.T) {
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
utils.AssertEqual(t, "deflate", resp.Header.Get(fiber.HeaderContentEncoding))
// Validate the file size is shrinked
// Validate that the file size has shrunk
body, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, len(body) < len(filedata))
@ -109,7 +109,7 @@ func Test_Compress_Brotli(t *testing.T) {
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
utils.AssertEqual(t, "br", resp.Header.Get(fiber.HeaderContentEncoding))
// Validate the file size is shrinked
// Validate that the file size has shrunk
body, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, len(body) < len(filedata))
@ -132,7 +132,7 @@ func Test_Compress_Disabled(t *testing.T) {
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
utils.AssertEqual(t, "", resp.Header.Get(fiber.HeaderContentEncoding))
// Validate the file size is not shrinked
// Validate the file size is not shrunk
body, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, len(body) == len(filedata))