1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-23 14:03:53 +00:00
fiber/app_test.go

218 lines
4.7 KiB
Go
Raw Normal View History

2020-02-22 17:03:30 -05:00
// 🚀 Fiber is an Express inspired web framework written in Go with 💖
2020-04-23 00:33:36 +02:00
// 📌 API Documentation: https://docs.gofiber.io
2020-02-22 17:03:30 -05:00
// 📝 Github Repository: https://github.com/gofiber/fiber
2020-02-21 18:07:43 +01:00
2020-04-13 09:01:27 +02:00
// go test -v -coverprofile cover.out .
// go tool cover -html=cover.out -o cover.html
// open cover.html
2020-02-21 01:54:50 +01:00
package fiber
import (
2020-04-13 09:01:27 +02:00
"net"
2020-02-21 01:54:50 +01:00
"net/http"
"testing"
2020-04-13 09:01:27 +02:00
"time"
2020-02-21 01:54:50 +01:00
)
var handler = func(c *Ctx) {}
2020-03-31 10:03:39 +02:00
func is200(t *testing.T, app *App, url string, m ...string) {
2020-03-24 03:36:52 +01:00
2020-02-21 18:07:43 +01:00
method := "GET"
if len(m) > 0 {
method = m[0]
}
req, _ := http.NewRequest(method, url, nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("%s - %s - %v", method, url, err)
}
if resp.StatusCode != 200 {
t.Fatalf("%s - %s - %v", method, url, resp.StatusCode)
}
}
2020-02-21 01:54:50 +01:00
func Test_Methods(t *testing.T) {
app := New()
2020-02-21 18:07:43 +01:00
app.Connect("/:john?/:doe?", handler)
is200(t, app, "/", "CONNECT")
app.Connect("/:john?/:doe?", handler)
is200(t, app, "/", "CONNECT")
app.Put("/:john?/:doe?", handler)
is200(t, app, "/", "CONNECT")
app.Post("/:john?/:doe?", handler)
is200(t, app, "/", "POST")
app.Delete("/:john?/:doe?", handler)
is200(t, app, "/", "DELETE")
app.Head("/:john?/:doe?", handler)
is200(t, app, "/", "HEAD")
app.Patch("/:john?/:doe?", handler)
is200(t, app, "/", "PATCH")
app.Options("/:john?/:doe?", handler)
is200(t, app, "/", "OPTIONS")
app.Trace("/:john?/:doe?", handler)
is200(t, app, "/", "TRACE")
app.Get("/:john?/:doe?", handler)
is200(t, app, "/", "GET")
app.All("/:john?/:doe?", handler)
is200(t, app, "/", "POST")
app.Use("/:john?/:doe?", handler)
is200(t, app, "/", "GET")
2020-02-21 01:54:50 +01:00
}
2020-04-13 09:01:27 +02:00
func Test_New(t *testing.T) {
app := New(&Settings{
Immutable: true,
})
app.Get("/", func(*Ctx) {
})
}
func Test_Shutdown(t *testing.T) {
app := New()
_ = app.Shutdown()
}
2020-03-04 12:30:29 +01:00
func Test_Static(t *testing.T) {
app := New()
grp := app.Group("/v1")
grp.Static("/v2", ".travis.yml")
app.Static("/*", ".github/FUNDING.yml")
app.Static("/john", "./.github")
req, _ := http.NewRequest("GET", "/john/stale.yml", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
if resp.Header.Get("Content-Length") == "" {
t.Fatalf(`%s: Missing Content-Length`, t.Name())
}
req, _ = http.NewRequest("GET", "/yesyes/john/doe", nil)
resp, err = app.Test(req)
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
if resp.Header.Get("Content-Length") == "" {
t.Fatalf(`%s: Missing Content-Length`, t.Name())
}
req, _ = http.NewRequest("GET", "/john/stale.yml", nil)
resp, err = app.Test(req)
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
if resp.Header.Get("Content-Length") == "" {
t.Fatalf(`%s: Missing Content-Length`, t.Name())
}
req, _ = http.NewRequest("GET", "/v1/v2", nil)
resp, err = app.Test(req)
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
if resp.Header.Get("Content-Length") == "" {
t.Fatalf(`%s: Missing Content-Length`, t.Name())
}
}
2020-02-21 18:07:43 +01:00
2020-02-21 01:54:50 +01:00
func Test_Group(t *testing.T) {
app := New()
2020-02-21 18:07:43 +01:00
2020-02-21 01:54:50 +01:00
grp := app.Group("/test")
grp.Get("/", handler)
2020-02-21 18:07:43 +01:00
is200(t, app, "/test", "GET")
2020-02-21 01:54:50 +01:00
grp.Get("/:demo?", handler)
2020-02-21 18:07:43 +01:00
is200(t, app, "/test/john", "GET")
2020-02-21 01:54:50 +01:00
grp.Connect("/CONNECT", handler)
2020-02-21 18:07:43 +01:00
is200(t, app, "/test/CONNECT", "CONNECT")
2020-02-21 01:54:50 +01:00
grp.Put("/PUT", handler)
2020-02-21 18:07:43 +01:00
is200(t, app, "/test/PUT", "PUT")
2020-02-21 01:54:50 +01:00
grp.Post("/POST", handler)
2020-02-21 18:07:43 +01:00
is200(t, app, "/test/POST", "POST")
2020-02-21 01:54:50 +01:00
grp.Delete("/DELETE", handler)
2020-02-21 18:07:43 +01:00
is200(t, app, "/test/DELETE", "DELETE")
2020-02-21 01:54:50 +01:00
grp.Head("/HEAD", handler)
2020-02-21 18:07:43 +01:00
is200(t, app, "/test/HEAD", "HEAD")
2020-02-21 01:54:50 +01:00
grp.Patch("/PATCH", handler)
2020-02-21 18:07:43 +01:00
is200(t, app, "/test/PATCH", "PATCH")
2020-02-21 01:54:50 +01:00
grp.Options("/OPTIONS", handler)
2020-02-21 18:07:43 +01:00
is200(t, app, "/test/OPTIONS", "OPTIONS")
2020-02-21 01:54:50 +01:00
grp.Trace("/TRACE", handler)
2020-02-21 18:07:43 +01:00
is200(t, app, "/test/TRACE", "TRACE")
2020-02-21 01:54:50 +01:00
grp.All("/ALL", handler)
2020-02-21 18:07:43 +01:00
is200(t, app, "/test/ALL", "POST")
2020-02-21 01:54:50 +01:00
grp.Use("/USE", handler)
2020-02-21 18:07:43 +01:00
is200(t, app, "/test/USE/oke", "GET")
api := grp.Group("/v1")
api.Post("/", handler)
is200(t, app, "/test/v1/", "POST")
api.Get("/users", handler)
is200(t, app, "/test/v1/users", "GET")
2020-02-21 01:54:50 +01:00
}
2020-04-13 09:01:27 +02:00
func Test_Listen(t *testing.T) {
app := New()
go func() {
time.Sleep(500 * time.Millisecond)
_ = app.Shutdown()
}()
app.Listen(3002)
go func() {
time.Sleep(500 * time.Millisecond)
_ = app.Shutdown()
}()
app.Listen("3003")
}
func Test_Serve(t *testing.T) {
app := New(&Settings{
Prefork: true,
})
ln, err := net.Listen("tcp4", ":3004")
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
go func() {
time.Sleep(500 * time.Millisecond)
_ = app.Shutdown()
}()
app.Serve(ln)
}