2020-09-13 11:20:11 +02:00
|
|
|
package compress
|
|
|
|
|
|
|
|
import (
|
2020-09-16 09:48:59 +08:00
|
|
|
"errors"
|
2020-09-16 16:50:13 +08:00
|
|
|
"fmt"
|
2020-09-13 11:20:11 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2020-09-14 12:12:29 +02:00
|
|
|
"github.com/gofiber/fiber/v2/utils"
|
2020-09-13 11:20:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var filedata []byte
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
dat, err := ioutil.ReadFile("../../.github/README.md")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
filedata = dat
|
|
|
|
}
|
|
|
|
|
2020-09-16 16:50:13 +08:00
|
|
|
// go test -run Test_Compress_Gzip
|
2020-09-13 11:20:11 +02:00
|
|
|
func Test_Compress_Gzip(t *testing.T) {
|
|
|
|
app := fiber.New()
|
|
|
|
|
2020-09-16 16:50:13 +08:00
|
|
|
app.Use(New())
|
2020-09-13 11:20:11 +02:00
|
|
|
|
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)
|
|
|
|
return c.Send(filedata)
|
|
|
|
})
|
|
|
|
|
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
|
|
req.Header.Set("Accept-Encoding", "gzip")
|
|
|
|
|
|
|
|
resp, err := app.Test(req)
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
|
|
|
utils.AssertEqual(t, "gzip", resp.Header.Get(fiber.HeaderContentEncoding))
|
|
|
|
|
2020-10-08 10:56:25 +03:00
|
|
|
// Validate that the file size has shrunk
|
2020-09-13 11:20:11 +02:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
2020-09-16 16:50:13 +08:00
|
|
|
utils.AssertEqual(t, nil, err)
|
2020-09-13 11:20:11 +02:00
|
|
|
utils.AssertEqual(t, true, len(body) < len(filedata))
|
|
|
|
}
|
|
|
|
|
2020-09-16 16:50:13 +08:00
|
|
|
// go test -run Test_Compress_Different_Level
|
|
|
|
func Test_Compress_Different_Level(t *testing.T) {
|
2020-10-08 00:05:34 +03:00
|
|
|
levels := []Level{LevelBestSpeed, LevelBestCompression}
|
2020-09-16 16:50:13 +08:00
|
|
|
for _, level := range levels {
|
|
|
|
t.Run(fmt.Sprintf("level %d", level), func(t *testing.T) {
|
|
|
|
app := fiber.New()
|
|
|
|
|
|
|
|
app.Use(New(Config{Level: level}))
|
|
|
|
|
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)
|
|
|
|
return c.Send(filedata)
|
|
|
|
})
|
|
|
|
|
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
2020-10-25 11:55:39 +01:00
|
|
|
req.Header.Set("Accept-Encoding", "gzip")
|
2020-09-16 16:50:13 +08:00
|
|
|
|
|
|
|
resp, err := app.Test(req)
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
2020-10-25 11:55:39 +01:00
|
|
|
utils.AssertEqual(t, "gzip", resp.Header.Get(fiber.HeaderContentEncoding))
|
|
|
|
|
|
|
|
// 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))
|
2020-09-16 16:50:13 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-13 11:20:11 +02:00
|
|
|
func Test_Compress_Deflate(t *testing.T) {
|
|
|
|
app := fiber.New()
|
|
|
|
|
2020-09-16 16:50:13 +08:00
|
|
|
app.Use(New())
|
2020-09-13 11:20:11 +02:00
|
|
|
|
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
return c.Send(filedata)
|
|
|
|
})
|
|
|
|
|
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
|
|
req.Header.Set("Accept-Encoding", "deflate")
|
|
|
|
|
|
|
|
resp, err := app.Test(req)
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
|
|
|
utils.AssertEqual(t, "deflate", resp.Header.Get(fiber.HeaderContentEncoding))
|
|
|
|
|
2020-10-08 10:56:25 +03:00
|
|
|
// Validate that the file size has shrunk
|
2020-09-13 11:20:11 +02:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
2020-09-16 16:50:13 +08:00
|
|
|
utils.AssertEqual(t, nil, err)
|
2020-09-13 11:20:11 +02:00
|
|
|
utils.AssertEqual(t, true, len(body) < len(filedata))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Compress_Brotli(t *testing.T) {
|
|
|
|
app := fiber.New()
|
|
|
|
|
2020-09-16 16:50:13 +08:00
|
|
|
app.Use(New())
|
2020-09-13 11:20:11 +02:00
|
|
|
|
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
return c.Send(filedata)
|
|
|
|
})
|
|
|
|
|
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
|
|
req.Header.Set("Accept-Encoding", "br")
|
|
|
|
|
2020-09-16 15:22:04 +08:00
|
|
|
resp, err := app.Test(req, 10000)
|
2020-09-13 11:20:11 +02:00
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
|
|
|
utils.AssertEqual(t, "br", resp.Header.Get(fiber.HeaderContentEncoding))
|
|
|
|
|
2020-10-08 10:56:25 +03:00
|
|
|
// Validate that the file size has shrunk
|
2020-09-13 11:20:11 +02:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
2020-09-16 16:50:13 +08:00
|
|
|
utils.AssertEqual(t, nil, err)
|
2020-09-13 11:20:11 +02:00
|
|
|
utils.AssertEqual(t, true, len(body) < len(filedata))
|
|
|
|
}
|
2020-09-16 09:44:05 +08:00
|
|
|
|
2020-09-16 09:48:59 +08:00
|
|
|
func Test_Compress_Disabled(t *testing.T) {
|
|
|
|
app := fiber.New()
|
|
|
|
|
|
|
|
app.Use(New(Config{Level: LevelDisabled}))
|
|
|
|
|
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
return c.Send(filedata)
|
|
|
|
})
|
|
|
|
|
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
|
|
req.Header.Set("Accept-Encoding", "br")
|
|
|
|
|
|
|
|
resp, err := app.Test(req)
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
|
|
|
utils.AssertEqual(t, "", resp.Header.Get(fiber.HeaderContentEncoding))
|
|
|
|
|
2020-10-08 00:05:34 +03:00
|
|
|
// Validate the file size is not shrunk
|
2020-09-16 09:48:59 +08:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
2020-09-16 16:50:13 +08:00
|
|
|
utils.AssertEqual(t, nil, err)
|
2020-09-16 09:48:59 +08:00
|
|
|
utils.AssertEqual(t, true, len(body) == len(filedata))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Compress_Next_Error(t *testing.T) {
|
|
|
|
app := fiber.New()
|
|
|
|
|
|
|
|
app.Use(New())
|
|
|
|
|
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
return errors.New("next error")
|
|
|
|
})
|
|
|
|
|
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
|
|
req.Header.Set("Accept-Encoding", "gzip")
|
|
|
|
|
|
|
|
resp, err := app.Test(req)
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 500, resp.StatusCode, "Status code")
|
|
|
|
utils.AssertEqual(t, "", resp.Header.Get(fiber.HeaderContentEncoding))
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, "next error", string(body))
|
|
|
|
}
|
|
|
|
|
2020-09-16 09:44:05 +08:00
|
|
|
// go test -run Test_Compress_Next
|
|
|
|
func Test_Compress_Next(t *testing.T) {
|
2020-09-17 13:41:06 +08:00
|
|
|
app := fiber.New()
|
2020-09-16 09:44:05 +08:00
|
|
|
app.Use(New(Config{
|
|
|
|
Next: func(_ *fiber.Ctx) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
|
|
|
|
}
|