2020-05-07 19:28:21 +02:00
|
|
|
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
|
2020-05-07 20:22:26 +02:00
|
|
|
// 🤖 Github Repository: https://github.com/gofiber/fiber
|
2020-05-07 19:28:21 +02:00
|
|
|
// 📌 API Documentation: https://docs.gofiber.io
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-02-21 01:54:50 +01:00
|
|
|
package fiber
|
|
|
|
|
|
|
|
import (
|
2020-06-06 07:27:01 +02:00
|
|
|
"errors"
|
2020-06-07 14:43:25 +02:00
|
|
|
"fmt"
|
2020-05-07 19:28:21 +02:00
|
|
|
"io/ioutil"
|
2020-04-13 09:01:27 +02:00
|
|
|
"net"
|
2020-05-07 19:28:21 +02:00
|
|
|
"net/http/httptest"
|
2020-06-07 14:43:25 +02:00
|
|
|
"regexp"
|
2020-05-29 10:22:54 +02:00
|
|
|
"strings"
|
2020-02-21 01:54:50 +01:00
|
|
|
"testing"
|
2020-04-13 09:01:27 +02:00
|
|
|
"time"
|
2020-05-23 09:25:49 +02:00
|
|
|
|
|
|
|
utils "github.com/gofiber/utils"
|
|
|
|
fasthttp "github.com/valyala/fasthttp"
|
2020-02-21 01:54:50 +01:00
|
|
|
)
|
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
func testStatus200(t *testing.T, app *App, url string, method string) {
|
|
|
|
req := httptest.NewRequest(method, url, nil)
|
2020-02-21 01:54:50 +01:00
|
|
|
|
2020-02-21 18:07:43 +01:00
|
|
|
resp, err := app.Test(req)
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
2020-02-21 18:07:43 +01:00
|
|
|
}
|
2020-05-07 19:28:21 +02:00
|
|
|
|
2020-06-20 17:26:48 +02:00
|
|
|
func Test_App_MethodNotAllowed(t *testing.T) {
|
|
|
|
app := New()
|
|
|
|
|
|
|
|
app.Post("/", func(c *Ctx) {})
|
|
|
|
|
|
|
|
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, 405, resp.StatusCode)
|
|
|
|
utils.AssertEqual(t, "POST", resp.Header.Get(HeaderAllow))
|
|
|
|
|
|
|
|
resp, err = app.Test(httptest.NewRequest("PATCH", "/", nil))
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, 405, resp.StatusCode)
|
|
|
|
utils.AssertEqual(t, "POST", resp.Header.Get(HeaderAllow))
|
|
|
|
|
|
|
|
resp, err = app.Test(httptest.NewRequest("PUT", "/", nil))
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, 405, resp.StatusCode)
|
|
|
|
utils.AssertEqual(t, "POST", resp.Header.Get(HeaderAllow))
|
2020-06-21 11:02:17 +02:00
|
|
|
|
|
|
|
app.Get("/", func(c *Ctx) {})
|
|
|
|
|
|
|
|
resp, err = app.Test(httptest.NewRequest("TRACE", "/", nil))
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, 405, resp.StatusCode)
|
|
|
|
utils.AssertEqual(t, "GET, HEAD, POST", resp.Header.Get(HeaderAllow))
|
|
|
|
|
|
|
|
resp, err = app.Test(httptest.NewRequest("PATCH", "/", nil))
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, 405, resp.StatusCode)
|
|
|
|
utils.AssertEqual(t, "GET, HEAD, POST", resp.Header.Get(HeaderAllow))
|
|
|
|
|
|
|
|
resp, err = app.Test(httptest.NewRequest("PUT", "/", nil))
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, 405, resp.StatusCode)
|
|
|
|
utils.AssertEqual(t, "GET, HEAD, POST", resp.Header.Get(HeaderAllow))
|
2020-06-20 17:26:48 +02:00
|
|
|
}
|
|
|
|
|
2020-06-06 07:27:01 +02:00
|
|
|
func Test_App_Routes(t *testing.T) {
|
|
|
|
app := New()
|
|
|
|
h := func(c *Ctx) {}
|
|
|
|
app.Get("/Get", h)
|
|
|
|
app.Head("/Head", h)
|
|
|
|
app.Post("/post", h)
|
|
|
|
utils.AssertEqual(t, 3, len(app.Routes()))
|
|
|
|
}
|
2020-05-23 09:25:49 +02:00
|
|
|
|
2020-06-07 14:43:25 +02:00
|
|
|
func Test_App_ServerErrorHandler_SmallReadBuffer(t *testing.T) {
|
|
|
|
expectedError := regexp.MustCompile(
|
|
|
|
`error when reading request headers: small read buffer\. Increase ReadBufferSize\. Buffer size=4096, contents: "GET / HTTP/1.1\\r\\nHost: example\.com\\r\\nVery-Long-Header: -+`,
|
|
|
|
)
|
|
|
|
app := New()
|
|
|
|
|
|
|
|
app.Get("/", func(c *Ctx) {
|
2020-06-08 02:55:19 +02:00
|
|
|
panic(errors.New("should never called"))
|
2020-06-07 14:43:25 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
request := httptest.NewRequest("GET", "/", nil)
|
|
|
|
logHeaderSlice := make([]string, 5000, 5000)
|
|
|
|
request.Header.Set("Very-Long-Header", strings.Join(logHeaderSlice, "-"))
|
|
|
|
_, err := app.Test(request)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expect an error at app.Test(request)")
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.AssertEqual(
|
|
|
|
t,
|
|
|
|
true,
|
|
|
|
expectedError.MatchString(err.Error()),
|
|
|
|
fmt.Sprintf("Has: %s, expected pattern: %s", err.Error(), expectedError.String()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-06 07:27:01 +02:00
|
|
|
func Test_App_ErrorHandler(t *testing.T) {
|
|
|
|
app := New()
|
2020-06-03 17:16:10 +02:00
|
|
|
|
2020-06-06 07:27:01 +02:00
|
|
|
app.Get("/", func(c *Ctx) {
|
2020-06-08 02:59:50 +02:00
|
|
|
c.Next(errors.New("hi, i'm an error"))
|
2020-06-06 07:27:01 +02:00
|
|
|
})
|
2020-06-03 17:16:10 +02:00
|
|
|
|
2020-06-06 07:27:01 +02:00
|
|
|
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 500, resp.StatusCode, "Status code")
|
2020-06-03 17:16:10 +02:00
|
|
|
|
2020-06-06 07:27:01 +02:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
utils.AssertEqual(t, nil, err)
|
2020-06-08 02:59:50 +02:00
|
|
|
utils.AssertEqual(t, "hi, i'm an error", string(body))
|
2020-06-03 17:16:10 +02:00
|
|
|
|
2020-06-06 07:27:01 +02:00
|
|
|
}
|
2020-06-03 17:16:10 +02:00
|
|
|
|
2020-06-06 07:27:01 +02:00
|
|
|
func Test_App_ErrorHandler_Custom(t *testing.T) {
|
|
|
|
app := New(&Settings{
|
|
|
|
ErrorHandler: func(ctx *Ctx, err error) {
|
2020-06-08 02:59:50 +02:00
|
|
|
ctx.Status(200).SendString("hi, i'm an custom error")
|
2020-06-06 07:27:01 +02:00
|
|
|
},
|
|
|
|
})
|
2020-06-03 17:16:10 +02:00
|
|
|
|
2020-06-06 07:27:01 +02:00
|
|
|
app.Get("/", func(c *Ctx) {
|
2020-06-08 02:59:50 +02:00
|
|
|
c.Next(errors.New("hi, i'm an error"))
|
2020-06-06 07:27:01 +02:00
|
|
|
})
|
2020-06-03 17:16:10 +02:00
|
|
|
|
2020-06-06 07:27:01 +02:00
|
|
|
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
2020-06-03 17:16:10 +02:00
|
|
|
|
2020-06-06 07:27:01 +02:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
utils.AssertEqual(t, nil, err)
|
2020-06-08 02:59:50 +02:00
|
|
|
utils.AssertEqual(t, "hi, i'm an custom error", string(body))
|
2020-06-06 07:27:01 +02:00
|
|
|
}
|
2020-06-03 17:16:10 +02:00
|
|
|
|
2020-05-12 19:24:04 +02:00
|
|
|
func Test_App_Nested_Params(t *testing.T) {
|
2020-05-11 13:42:42 +02:00
|
|
|
app := New()
|
|
|
|
|
|
|
|
app.Get("/test", func(c *Ctx) {
|
|
|
|
c.Status(400).Send("Should move on")
|
|
|
|
})
|
|
|
|
app.Get("/test/:param", func(c *Ctx) {
|
|
|
|
c.Status(400).Send("Should move on")
|
|
|
|
})
|
|
|
|
app.Get("/test/:param/test", func(c *Ctx) {
|
|
|
|
c.Status(400).Send("Should move on")
|
|
|
|
})
|
|
|
|
app.Get("/test/:param/test/:param2", func(c *Ctx) {
|
|
|
|
c.Status(200).Send("Good job")
|
|
|
|
})
|
|
|
|
|
|
|
|
req := httptest.NewRequest("GET", "/test/john/test/doe", nil)
|
|
|
|
resp, err := app.Test(req)
|
|
|
|
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
2020-05-11 13:42:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-12 19:24:04 +02:00
|
|
|
func Test_App_Use_Params(t *testing.T) {
|
2020-05-09 15:15:34 +02:00
|
|
|
app := New()
|
2020-05-12 19:24:04 +02:00
|
|
|
|
|
|
|
app.Use("/prefix/:param", func(c *Ctx) {
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, "john", c.Params("param"))
|
2020-05-09 15:15:34 +02:00
|
|
|
})
|
|
|
|
|
2020-05-12 19:24:04 +02:00
|
|
|
app.Use("/:param/*", func(c *Ctx) {
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, "john", c.Params("param"))
|
|
|
|
utils.AssertEqual(t, "doe", c.Params("*"))
|
2020-05-12 19:24:04 +02:00
|
|
|
})
|
2020-05-09 15:15:34 +02:00
|
|
|
|
2020-05-12 19:24:04 +02:00
|
|
|
resp, err := app.Test(httptest.NewRequest("GET", "/prefix/john", nil))
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
2020-05-12 19:24:04 +02:00
|
|
|
|
|
|
|
resp, err = app.Test(httptest.NewRequest("GET", "/john/doe", nil))
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
2020-05-12 19:24:04 +02:00
|
|
|
}
|
2020-05-13 20:21:49 +02:00
|
|
|
|
|
|
|
func Test_App_Use_Params_Group(t *testing.T) {
|
|
|
|
app := New()
|
|
|
|
|
|
|
|
group := app.Group("/prefix/:param/*")
|
|
|
|
group.Use("/", func(c *Ctx) {
|
|
|
|
c.Next()
|
|
|
|
})
|
|
|
|
group.Get("/test", func(c *Ctx) {
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, "john", c.Params("param"))
|
|
|
|
utils.AssertEqual(t, "doe", c.Params("*"))
|
2020-05-13 20:21:49 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
resp, err := app.Test(httptest.NewRequest("GET", "/prefix/john/doe/test", nil))
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
2020-05-13 20:21:49 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 22:53:40 +02:00
|
|
|
func Test_App_Chaining(t *testing.T) {
|
|
|
|
n := func(c *Ctx) {
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
app := New()
|
|
|
|
app.Use("/john", n, n, n, n, func(c *Ctx) {
|
|
|
|
c.Status(202)
|
|
|
|
})
|
|
|
|
|
|
|
|
req := httptest.NewRequest("POST", "/john", nil)
|
|
|
|
|
|
|
|
resp, err := app.Test(req)
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 202, resp.StatusCode, "Status code")
|
|
|
|
|
|
|
|
app.Get("/test", n, n, n, n, func(c *Ctx) {
|
|
|
|
c.Status(203)
|
|
|
|
})
|
|
|
|
|
|
|
|
req = httptest.NewRequest("GET", "/test", nil)
|
|
|
|
|
|
|
|
resp, err = app.Test(req)
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 203, resp.StatusCode, "Status code")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-12 19:24:04 +02:00
|
|
|
func Test_App_Order(t *testing.T) {
|
2020-05-09 15:15:34 +02:00
|
|
|
app := New()
|
|
|
|
|
|
|
|
app.Get("/test", func(c *Ctx) {
|
2020-05-07 19:28:21 +02:00
|
|
|
c.Write("1")
|
|
|
|
c.Next()
|
|
|
|
})
|
2020-05-09 15:15:34 +02:00
|
|
|
|
|
|
|
app.All("/test", func(c *Ctx) {
|
2020-05-07 19:28:21 +02:00
|
|
|
c.Write("2")
|
|
|
|
c.Next()
|
|
|
|
})
|
2020-05-09 15:15:34 +02:00
|
|
|
|
2020-05-07 19:28:21 +02:00
|
|
|
app.Use(func(c *Ctx) {
|
|
|
|
c.Write("3")
|
|
|
|
})
|
2020-05-09 15:15:34 +02:00
|
|
|
|
|
|
|
req := httptest.NewRequest("GET", "/test", nil)
|
|
|
|
|
2020-05-07 19:28:21 +02:00
|
|
|
resp, err := app.Test(req)
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
2020-05-09 15:15:34 +02:00
|
|
|
|
2020-05-07 19:28:21 +02:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, "123", string(body))
|
2020-05-07 19:28:21 +02:00
|
|
|
}
|
2020-05-12 19:24:04 +02:00
|
|
|
func Test_App_Methods(t *testing.T) {
|
2020-05-09 15:15:34 +02:00
|
|
|
var dummyHandler = func(c *Ctx) {}
|
|
|
|
|
|
|
|
app := New()
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
app.Connect("/:john?/:doe?", dummyHandler)
|
|
|
|
testStatus200(t, app, "/john/doe", "CONNECT")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
app.Put("/:john?/:doe?", dummyHandler)
|
2020-05-14 01:30:59 +07:00
|
|
|
testStatus200(t, app, "/john/doe", "PUT")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
app.Post("/:john?/:doe?", dummyHandler)
|
|
|
|
testStatus200(t, app, "/john/doe", "POST")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
app.Delete("/:john?/:doe?", dummyHandler)
|
|
|
|
testStatus200(t, app, "/john/doe", "DELETE")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
app.Head("/:john?/:doe?", dummyHandler)
|
|
|
|
testStatus200(t, app, "/john/doe", "HEAD")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
app.Patch("/:john?/:doe?", dummyHandler)
|
|
|
|
testStatus200(t, app, "/john/doe", "PATCH")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
app.Options("/:john?/:doe?", dummyHandler)
|
|
|
|
testStatus200(t, app, "/john/doe", "OPTIONS")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
app.Trace("/:john?/:doe?", dummyHandler)
|
|
|
|
testStatus200(t, app, "/john/doe", "TRACE")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
app.Get("/:john?/:doe?", dummyHandler)
|
|
|
|
testStatus200(t, app, "/john/doe", "GET")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
app.All("/:john?/:doe?", dummyHandler)
|
|
|
|
testStatus200(t, app, "/john/doe", "POST")
|
|
|
|
|
|
|
|
app.Use("/:john?/:doe?", dummyHandler)
|
|
|
|
testStatus200(t, app, "/john/doe", "GET")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-02-21 01:54:50 +01:00
|
|
|
}
|
|
|
|
|
2020-05-12 19:24:04 +02:00
|
|
|
func Test_App_New(t *testing.T) {
|
2020-05-09 15:15:34 +02:00
|
|
|
app := New()
|
|
|
|
app.Get("/", func(*Ctx) {
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
appConfig := New(&Settings{
|
2020-04-13 09:01:27 +02:00
|
|
|
Immutable: true,
|
|
|
|
})
|
2020-05-09 15:15:34 +02:00
|
|
|
appConfig.Get("/", func(*Ctx) {
|
2020-04-13 09:01:27 +02:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-12 19:24:04 +02:00
|
|
|
func Test_App_Shutdown(t *testing.T) {
|
2020-04-28 21:34:34 +02:00
|
|
|
app := New(&Settings{
|
|
|
|
DisableStartupMessage: true,
|
|
|
|
})
|
2020-04-13 09:01:27 +02:00
|
|
|
_ = app.Shutdown()
|
|
|
|
}
|
|
|
|
|
2020-06-03 17:16:10 +02:00
|
|
|
// go test -run Test_App_Static_Index_Default
|
|
|
|
func Test_App_Static_Index_Default(t *testing.T) {
|
2020-05-29 10:22:54 +02:00
|
|
|
app := New()
|
|
|
|
|
2020-06-01 12:54:32 +02:00
|
|
|
app.Static("/prefix", "./.github/workflows")
|
2020-05-29 10:22:54 +02:00
|
|
|
app.Static("/", "./.github")
|
|
|
|
|
2020-06-01 12:54:32 +02:00
|
|
|
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
|
2020-05-29 10:22:54 +02:00
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
|
|
|
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
|
|
|
|
utils.AssertEqual(t, "text/html; charset=utf-8", resp.Header.Get("Content-Type"))
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, true, strings.Contains(string(body), "Hello, World!"))
|
2020-06-01 12:54:32 +02:00
|
|
|
|
2020-05-29 10:22:54 +02:00
|
|
|
}
|
2020-06-03 17:16:10 +02:00
|
|
|
|
|
|
|
// go test -run Test_App_Static_Index
|
|
|
|
func Test_App_Static_Direct(t *testing.T) {
|
|
|
|
app := New()
|
|
|
|
|
|
|
|
app.Static("/", "./.github")
|
|
|
|
|
|
|
|
resp, err := app.Test(httptest.NewRequest("GET", "/index.html", nil))
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
|
|
|
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
|
|
|
|
utils.AssertEqual(t, "text/html; charset=utf-8", resp.Header.Get("Content-Type"))
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, true, strings.Contains(string(body), "Hello, World!"))
|
|
|
|
|
|
|
|
resp, err = app.Test(httptest.NewRequest("GET", "/FUNDING.yml", nil))
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
|
|
|
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
|
|
|
|
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
|
|
|
|
|
|
|
|
body, err = ioutil.ReadAll(resp.Body)
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, true, strings.Contains(string(body), "buymeacoffee"))
|
|
|
|
}
|
2020-05-27 22:53:40 +02:00
|
|
|
func Test_App_Static_Group(t *testing.T) {
|
2020-05-09 15:15:34 +02:00
|
|
|
app := New()
|
|
|
|
|
2020-05-27 22:53:40 +02:00
|
|
|
grp := app.Group("/v1", func(c *Ctx) {
|
|
|
|
c.Set("Test-Header", "123")
|
|
|
|
c.Next()
|
|
|
|
})
|
2020-05-09 15:15:34 +02:00
|
|
|
|
2020-05-27 22:53:40 +02:00
|
|
|
grp.Static("/v2", "./.github/FUNDING.yml")
|
2020-05-09 15:15:34 +02:00
|
|
|
|
2020-05-27 22:53:40 +02:00
|
|
|
req := httptest.NewRequest("GET", "/v1/v2", nil)
|
2020-03-04 12:30:29 +01:00
|
|
|
resp, err := app.Test(req)
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
|
|
|
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
|
2020-05-27 22:53:40 +02:00
|
|
|
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
|
|
|
|
utils.AssertEqual(t, "123", resp.Header.Get("Test-Header"))
|
|
|
|
|
|
|
|
grp = app.Group("/v2")
|
|
|
|
grp.Static("/v3*", "./.github/FUNDING.yml")
|
2020-05-09 15:15:34 +02:00
|
|
|
|
2020-05-27 22:53:40 +02:00
|
|
|
req = httptest.NewRequest("GET", "/v2/v3/john/doe", nil)
|
2020-03-04 12:30:29 +01:00
|
|
|
resp, err = app.Test(req)
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
|
|
|
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
|
2020-05-27 22:53:40 +02:00
|
|
|
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
|
2020-05-09 15:15:34 +02:00
|
|
|
|
2020-05-27 22:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_App_Static_Wildcard(t *testing.T) {
|
|
|
|
app := New()
|
|
|
|
|
|
|
|
app.Static("*", "./.github/FUNDING.yml")
|
|
|
|
|
|
|
|
req := httptest.NewRequest("GET", "/yesyes/john/doe", nil)
|
|
|
|
resp, err := app.Test(req)
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
|
|
|
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
|
|
|
|
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
|
|
|
|
|
2020-06-03 17:16:10 +02:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, true, strings.Contains(string(body), "buymeacoffee"))
|
|
|
|
|
2020-05-27 22:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_App_Static_Prefix_Wildcard(t *testing.T) {
|
|
|
|
app := New()
|
|
|
|
|
|
|
|
app.Static("/test/*", "./.github/FUNDING.yml")
|
|
|
|
|
|
|
|
req := httptest.NewRequest("GET", "/test/john/doe", nil)
|
|
|
|
resp, err := app.Test(req)
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
|
|
|
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
|
|
|
|
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
|
2020-06-03 17:16:10 +02:00
|
|
|
|
|
|
|
app.Static("/my/nameisjohn*", "./.github/FUNDING.yml")
|
|
|
|
|
|
|
|
resp, err = app.Test(httptest.NewRequest("GET", "/my/nameisjohn/no/its/not", nil))
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
|
|
|
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
|
|
|
|
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, true, strings.Contains(string(body), "buymeacoffee"))
|
2020-05-27 22:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_App_Static_Prefix(t *testing.T) {
|
|
|
|
app := New()
|
|
|
|
app.Static("/john", "./.github")
|
|
|
|
|
|
|
|
req := httptest.NewRequest("GET", "/john/stale.yml", nil)
|
|
|
|
resp, err := app.Test(req)
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
|
|
|
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
|
|
|
|
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
|
|
|
|
|
|
|
|
app.Static("/prefix", "./.github/workflows")
|
|
|
|
|
|
|
|
req = httptest.NewRequest("GET", "/prefix/test.yml", nil)
|
2020-03-04 12:30:29 +01:00
|
|
|
resp, err = app.Test(req)
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
|
|
|
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
|
2020-05-27 22:53:40 +02:00
|
|
|
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
|
|
|
|
|
|
|
|
app.Static("/single", "./.github/workflows/test.yml")
|
2020-05-09 15:15:34 +02:00
|
|
|
|
2020-05-27 22:53:40 +02:00
|
|
|
req = httptest.NewRequest("GET", "/single", nil)
|
2020-03-04 12:30:29 +01:00
|
|
|
resp, err = app.Test(req)
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
|
|
|
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
|
2020-05-27 22:53:40 +02:00
|
|
|
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
|
2020-03-04 12:30:29 +01:00
|
|
|
}
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-06-02 23:25:06 +02:00
|
|
|
// go test -run Test_App_Mixed_Routes_WithSameLen
|
|
|
|
func Test_App_Mixed_Routes_WithSameLen(t *testing.T) {
|
|
|
|
app := New()
|
|
|
|
|
|
|
|
// middleware
|
|
|
|
app.Use(func(ctx *Ctx) {
|
|
|
|
ctx.Set("TestHeader", "TestValue")
|
|
|
|
ctx.Next()
|
|
|
|
})
|
|
|
|
// routes with the same length
|
|
|
|
app.Static("/tesbar", "./.github")
|
|
|
|
app.Get("/foobar", func(ctx *Ctx) {
|
|
|
|
ctx.Send("FOO_BAR")
|
|
|
|
ctx.Type("html")
|
|
|
|
})
|
|
|
|
|
|
|
|
// match get route
|
|
|
|
req := httptest.NewRequest("GET", "/foobar", nil)
|
|
|
|
resp, err := app.Test(req)
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
|
|
|
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
|
|
|
|
utils.AssertEqual(t, "TestValue", resp.Header.Get("TestHeader"))
|
|
|
|
utils.AssertEqual(t, "text/html", resp.Header.Get("Content-Type"))
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, "FOO_BAR", string(body))
|
|
|
|
|
|
|
|
// match static route
|
|
|
|
req = httptest.NewRequest("GET", "/tesbar", nil)
|
|
|
|
resp, err = app.Test(req)
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
|
|
|
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
|
|
|
|
utils.AssertEqual(t, "TestValue", resp.Header.Get("TestHeader"))
|
|
|
|
utils.AssertEqual(t, "text/html; charset=utf-8", resp.Header.Get("Content-Type"))
|
|
|
|
|
|
|
|
body, err = ioutil.ReadAll(resp.Body)
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, true, strings.Contains(string(body), "Hello, World!"), "Response: "+string(body))
|
|
|
|
utils.AssertEqual(t, true, strings.HasPrefix(string(body), "<!DOCTYPE html>"), "Response: "+string(body))
|
|
|
|
}
|
|
|
|
|
2020-05-12 19:24:04 +02:00
|
|
|
func Test_App_Group(t *testing.T) {
|
2020-05-09 15:15:34 +02:00
|
|
|
var dummyHandler = func(c *Ctx) {}
|
|
|
|
|
|
|
|
app := New()
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-02-21 01:54:50 +01:00
|
|
|
grp := app.Group("/test")
|
2020-05-09 15:15:34 +02:00
|
|
|
grp.Get("/", dummyHandler)
|
|
|
|
testStatus200(t, app, "/test", "GET")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
grp.Get("/:demo?", dummyHandler)
|
|
|
|
testStatus200(t, app, "/test/john", "GET")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
grp.Connect("/CONNECT", dummyHandler)
|
|
|
|
testStatus200(t, app, "/test/CONNECT", "CONNECT")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
grp.Put("/PUT", dummyHandler)
|
|
|
|
testStatus200(t, app, "/test/PUT", "PUT")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
grp.Post("/POST", dummyHandler)
|
|
|
|
testStatus200(t, app, "/test/POST", "POST")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
grp.Delete("/DELETE", dummyHandler)
|
|
|
|
testStatus200(t, app, "/test/DELETE", "DELETE")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
grp.Head("/HEAD", dummyHandler)
|
|
|
|
testStatus200(t, app, "/test/HEAD", "HEAD")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
grp.Patch("/PATCH", dummyHandler)
|
|
|
|
testStatus200(t, app, "/test/PATCH", "PATCH")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
grp.Options("/OPTIONS", dummyHandler)
|
|
|
|
testStatus200(t, app, "/test/OPTIONS", "OPTIONS")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
grp.Trace("/TRACE", dummyHandler)
|
|
|
|
testStatus200(t, app, "/test/TRACE", "TRACE")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
grp.All("/ALL", dummyHandler)
|
|
|
|
testStatus200(t, app, "/test/ALL", "POST")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
grp.Use("/USE", dummyHandler)
|
|
|
|
testStatus200(t, app, "/test/USE/oke", "GET")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
|
|
|
api := grp.Group("/v1")
|
2020-05-09 15:15:34 +02:00
|
|
|
api.Post("/", dummyHandler)
|
2020-05-23 09:25:49 +02:00
|
|
|
|
|
|
|
resp, err := app.Test(httptest.NewRequest("POST", "/test/v1/", nil))
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
|
|
|
//utils.AssertEqual(t, "/test/v1", resp.Header.Get("Location"), "Location")
|
2020-02-21 18:07:43 +01:00
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
api.Get("/users", dummyHandler)
|
2020-05-23 09:25:49 +02:00
|
|
|
resp, err = app.Test(httptest.NewRequest("GET", "/test/v1/UsErS", nil))
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
|
|
|
//utils.AssertEqual(t, "/test/v1/users", resp.Header.Get("Location"), "Location")
|
2020-02-21 01:54:50 +01:00
|
|
|
}
|
|
|
|
|
2020-06-19 10:54:15 +02:00
|
|
|
func Test_App_Deep_Group(t *testing.T) {
|
|
|
|
runThroughCount := 0
|
|
|
|
var dummyHandler = func(c *Ctx) {
|
|
|
|
runThroughCount++
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
app := New()
|
|
|
|
gApi := app.Group("/api", dummyHandler)
|
|
|
|
gV1 := gApi.Group("/v1", dummyHandler)
|
|
|
|
gUser := gV1.Group("/user", dummyHandler)
|
|
|
|
gUser.Get("/authenticate", func(ctx *Ctx) {
|
|
|
|
runThroughCount++
|
|
|
|
ctx.SendStatus(200)
|
|
|
|
})
|
|
|
|
testStatus200(t, app, "/api/v1/user/authenticate", "GET")
|
|
|
|
utils.AssertEqual(t, 4, runThroughCount, "Loop count")
|
|
|
|
}
|
|
|
|
|
2020-05-12 19:24:04 +02:00
|
|
|
func Test_App_Listen(t *testing.T) {
|
2020-04-28 21:34:34 +02:00
|
|
|
app := New(&Settings{
|
|
|
|
DisableStartupMessage: true,
|
|
|
|
})
|
2020-04-13 09:01:27 +02:00
|
|
|
go func() {
|
2020-05-13 20:21:49 +02:00
|
|
|
time.Sleep(1000 * time.Millisecond)
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, nil, app.Shutdown())
|
2020-04-13 09:01:27 +02:00
|
|
|
}()
|
2020-05-13 20:21:49 +02:00
|
|
|
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, nil, app.Listen(4003))
|
2020-05-13 02:54:35 +02:00
|
|
|
|
2020-04-13 09:01:27 +02:00
|
|
|
go func() {
|
2020-05-13 20:21:49 +02:00
|
|
|
time.Sleep(1000 * time.Millisecond)
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, nil, app.Shutdown())
|
2020-04-13 09:01:27 +02:00
|
|
|
}()
|
2020-05-13 20:21:49 +02:00
|
|
|
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, nil, app.Listen("4010"))
|
2020-04-13 09:01:27 +02:00
|
|
|
}
|
|
|
|
|
2020-05-12 19:24:04 +02:00
|
|
|
func Test_App_Serve(t *testing.T) {
|
2020-04-13 09:01:27 +02:00
|
|
|
app := New(&Settings{
|
2020-04-28 21:34:34 +02:00
|
|
|
DisableStartupMessage: true,
|
|
|
|
Prefork: true,
|
2020-04-13 09:01:27 +02:00
|
|
|
})
|
2020-05-13 20:21:49 +02:00
|
|
|
ln, err := net.Listen("tcp4", ":4020")
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, nil, err)
|
2020-05-13 02:54:35 +02:00
|
|
|
|
2020-04-13 09:01:27 +02:00
|
|
|
go func() {
|
2020-05-13 20:21:49 +02:00
|
|
|
time.Sleep(1000 * time.Millisecond)
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, nil, app.Shutdown())
|
2020-04-13 09:01:27 +02:00
|
|
|
}()
|
2020-05-13 02:54:35 +02:00
|
|
|
|
2020-05-23 09:25:49 +02:00
|
|
|
utils.AssertEqual(t, nil, app.Serve(ln))
|
|
|
|
}
|
|
|
|
|
|
|
|
// go test -v -run=^$ -bench=Benchmark_App_ETag -benchmem -count=4
|
|
|
|
func Benchmark_App_ETag(b *testing.B) {
|
|
|
|
app := New()
|
|
|
|
c := app.AcquireCtx(&fasthttp.RequestCtx{})
|
|
|
|
defer app.ReleaseCtx(c)
|
|
|
|
c.Send("Hello, World!")
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
setETag(c, false)
|
|
|
|
}
|
|
|
|
utils.AssertEqual(b, `"13-1831710635"`, string(c.Fasthttp.Response.Header.Peek(HeaderETag)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// go test -v -run=^$ -bench=Benchmark_App_ETag_Weak -benchmem -count=4
|
|
|
|
func Benchmark_App_ETag_Weak(b *testing.B) {
|
|
|
|
app := New()
|
|
|
|
c := app.AcquireCtx(&fasthttp.RequestCtx{})
|
|
|
|
defer app.ReleaseCtx(c)
|
|
|
|
c.Send("Hello, World!")
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
setETag(c, true)
|
|
|
|
}
|
2020-05-23 14:56:52 +02:00
|
|
|
utils.AssertEqual(b, `W/"13-1831710635"`, string(c.Fasthttp.Response.Header.Peek(HeaderETag)))
|
2020-04-13 09:01:27 +02:00
|
|
|
}
|