mirror of
https://github.com/gofiber/fiber.git
synced 2025-05-31 11:52:41 +00:00
Merge pull request #897 from klipitkas/custom-type-compress-middleware
Add custom types to compress middleware
This commit is contained in:
commit
d4750fbf2b
@ -12,30 +12,31 @@ type Config struct {
|
|||||||
// Optional. Default: nil
|
// Optional. Default: nil
|
||||||
Next func(c *fiber.Ctx) bool
|
Next func(c *fiber.Ctx) bool
|
||||||
|
|
||||||
// CompressLevel determines the compression algoritm
|
// Level determines the compression algorithm
|
||||||
//
|
//
|
||||||
// Optional. Default: LevelDefault
|
// Optional. Default: LevelDefault
|
||||||
// LevelDisabled: -1
|
// LevelDisabled: -1
|
||||||
// LevelDefault: 0
|
// LevelDefault: 0
|
||||||
// LevelBestSpeed: 1
|
// LevelBestSpeed: 1
|
||||||
// LevelBestCompression: 2
|
// 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
|
// ConfigDefault is the default config
|
||||||
var ConfigDefault = Config{
|
var ConfigDefault = Config{
|
||||||
Next: nil,
|
Next: nil,
|
||||||
Level: LevelDefault,
|
Level: LevelDefault,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compression levels
|
|
||||||
const (
|
|
||||||
LevelDisabled = -1
|
|
||||||
LevelDefault = 0
|
|
||||||
LevelBestSpeed = 1
|
|
||||||
LevelBestCompression = 2
|
|
||||||
)
|
|
||||||
|
|
||||||
// New creates a new middleware handler
|
// New creates a new middleware handler
|
||||||
func New(config ...Config) fiber.Handler {
|
func New(config ...Config) fiber.Handler {
|
||||||
// Set default config
|
// Set default config
|
||||||
@ -61,13 +62,22 @@ func New(config ...Config) fiber.Handler {
|
|||||||
switch cfg.Level {
|
switch cfg.Level {
|
||||||
case LevelDefault:
|
case LevelDefault:
|
||||||
// LevelDefault
|
// LevelDefault
|
||||||
compressor = fasthttp.CompressHandlerBrotliLevel(fctx, fasthttp.CompressBrotliDefaultCompression, fasthttp.CompressDefaultCompression)
|
compressor = fasthttp.CompressHandlerBrotliLevel(fctx,
|
||||||
|
fasthttp.CompressBrotliDefaultCompression,
|
||||||
|
fasthttp.CompressDefaultCompression,
|
||||||
|
)
|
||||||
case LevelBestSpeed:
|
case LevelBestSpeed:
|
||||||
// LevelBestSpeed
|
// LevelBestSpeed
|
||||||
compressor = fasthttp.CompressHandlerBrotliLevel(fctx, fasthttp.CompressBrotliBestSpeed, fasthttp.CompressBestSpeed)
|
compressor = fasthttp.CompressHandlerBrotliLevel(fctx,
|
||||||
|
fasthttp.CompressBrotliBestSpeed,
|
||||||
|
fasthttp.CompressBestSpeed,
|
||||||
|
)
|
||||||
case LevelBestCompression:
|
case LevelBestCompression:
|
||||||
// LevelBestCompression
|
// LevelBestCompression
|
||||||
compressor = fasthttp.CompressHandlerBrotliLevel(fctx, fasthttp.CompressBrotliBestCompression, fasthttp.CompressBestCompression)
|
compressor = fasthttp.CompressHandlerBrotliLevel(fctx,
|
||||||
|
fasthttp.CompressBrotliBestCompression,
|
||||||
|
fasthttp.CompressBestCompression,
|
||||||
|
)
|
||||||
default:
|
default:
|
||||||
// LevelDisabled
|
// LevelDisabled
|
||||||
return func(c *fiber.Ctx) error {
|
return func(c *fiber.Ctx) error {
|
||||||
|
@ -40,7 +40,7 @@ func Test_Compress_Gzip(t *testing.T) {
|
|||||||
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
||||||
utils.AssertEqual(t, "gzip", resp.Header.Get(fiber.HeaderContentEncoding))
|
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)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
utils.AssertEqual(t, nil, err)
|
utils.AssertEqual(t, nil, err)
|
||||||
utils.AssertEqual(t, true, len(body) < len(filedata))
|
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
|
// go test -run Test_Compress_Different_Level
|
||||||
func Test_Compress_Different_Level(t *testing.T) {
|
func Test_Compress_Different_Level(t *testing.T) {
|
||||||
levels := []int{LevelBestSpeed, LevelBestCompression, 10}
|
levels := []Level{LevelBestSpeed, LevelBestCompression}
|
||||||
for _, level := range levels {
|
for _, level := range levels {
|
||||||
t.Run(fmt.Sprintf("level %d", level), func(t *testing.T) {
|
t.Run(fmt.Sprintf("level %d", level), func(t *testing.T) {
|
||||||
app := fiber.New()
|
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, 200, resp.StatusCode, "Status code")
|
||||||
utils.AssertEqual(t, "deflate", resp.Header.Get(fiber.HeaderContentEncoding))
|
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)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
utils.AssertEqual(t, nil, err)
|
utils.AssertEqual(t, nil, err)
|
||||||
utils.AssertEqual(t, true, len(body) < len(filedata))
|
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, 200, resp.StatusCode, "Status code")
|
||||||
utils.AssertEqual(t, "br", resp.Header.Get(fiber.HeaderContentEncoding))
|
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)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
utils.AssertEqual(t, nil, err)
|
utils.AssertEqual(t, nil, err)
|
||||||
utils.AssertEqual(t, true, len(body) < len(filedata))
|
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, 200, resp.StatusCode, "Status code")
|
||||||
utils.AssertEqual(t, "", resp.Header.Get(fiber.HeaderContentEncoding))
|
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)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
utils.AssertEqual(t, nil, err)
|
utils.AssertEqual(t, nil, err)
|
||||||
utils.AssertEqual(t, true, len(body) == len(filedata))
|
utils.AssertEqual(t, true, len(body) == len(filedata))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user