1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-22 09:33:21 +00:00
fiber/app_test.go

970 lines
27 KiB
Go
Raw Normal View History

// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 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-07-29 08:46:36 +08:00
"bytes"
2020-07-15 18:50:28 +02:00
"crypto/tls"
2020-06-06 07:27:01 +02:00
"errors"
"fmt"
2020-07-16 16:42:36 +08:00
"io"
"io/ioutil"
2020-07-15 18:50:28 +02:00
"net"
2020-07-29 08:46:36 +08:00
"net/http"
"net/http/httptest"
2020-07-15 10:20:53 +08:00
"reflect"
"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"
utils "github.com/gofiber/utils"
fasthttp "github.com/valyala/fasthttp"
2020-07-14 15:23:41 +08:00
fasthttputil "github.com/valyala/fasthttp/fasthttputil"
2020-02-21 01:54:50 +01: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)
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-06-20 17:26:48 +02:00
func Test_App_MethodNotAllowed(t *testing.T) {
app := New()
app.Use(func(ctx *Ctx) { ctx.Next() })
2020-06-20 17:26:48 +02:00
app.Post("/", func(c *Ctx) {})
2020-07-10 14:15:58 +02:00
app.Options("/", func(c *Ctx) {})
resp, err := app.Test(httptest.NewRequest("POST", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 200, resp.StatusCode)
utils.AssertEqual(t, "", resp.Header.Get(HeaderAllow))
resp, err = app.Test(httptest.NewRequest("GET", "/", nil))
2020-06-20 17:26:48 +02:00
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 405, resp.StatusCode)
2020-07-10 14:15:58 +02:00
utils.AssertEqual(t, "POST, OPTIONS", resp.Header.Get(HeaderAllow))
2020-06-20 17:26:48 +02:00
resp, err = app.Test(httptest.NewRequest("PATCH", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 405, resp.StatusCode)
2020-07-10 14:15:58 +02:00
utils.AssertEqual(t, "POST, OPTIONS", resp.Header.Get(HeaderAllow))
2020-06-20 17:26:48 +02:00
resp, err = app.Test(httptest.NewRequest("PUT", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 405, resp.StatusCode)
2020-07-10 14:15:58 +02:00
utils.AssertEqual(t, "POST, OPTIONS", 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)
2020-07-10 14:15:58 +02:00
utils.AssertEqual(t, "GET, HEAD, POST, OPTIONS", resp.Header.Get(HeaderAllow))
2020-06-21 11:02:17 +02:00
resp, err = app.Test(httptest.NewRequest("PATCH", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 405, resp.StatusCode)
2020-07-10 14:15:58 +02:00
utils.AssertEqual(t, "GET, HEAD, POST, OPTIONS", resp.Header.Get(HeaderAllow))
2020-06-21 11:02:17 +02:00
resp, err = app.Test(httptest.NewRequest("PUT", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 405, resp.StatusCode)
2020-07-10 14:15:58 +02:00
utils.AssertEqual(t, "GET, HEAD, POST, OPTIONS", resp.Header.Get(HeaderAllow))
2020-06-20 17:26:48 +02:00
}
func Test_App_Custom_Middleware_404_Should_Not_SetMethodNotAllowed(t *testing.T) {
app := New()
app.Use(func(ctx *Ctx) {
ctx.Status(404)
})
app.Post("/", func(c *Ctx) {})
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 404, resp.StatusCode)
g := app.Group("/with-next", func(ctx *Ctx) {
ctx.Status(404)
ctx.Next()
})
g.Post("/", func(c *Ctx) {})
resp, err = app.Test(httptest.NewRequest("GET", "/with-next", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 404, resp.StatusCode)
}
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"))
})
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) {
2020-07-15 10:20:53 +08:00
app := New(&Settings{
BodyLimit: 4,
})
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-07-15 10:20:53 +08:00
_, err = app.Test(httptest.NewRequest("GET", "/", strings.NewReader("big body")))
if err != nil {
utils.AssertEqual(t, "body size exceeds the given limit", err.Error(), "app.Test(req)")
}
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
Use param support + optimizations (#361) * Benchmark workflow * Update router.go * Clean root * Add mutex * Benchmark workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Benchmark Workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Update security workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Make Ctx pool accessible - Add ctx benchmarks * v1.9.6 * v1.9.6 Co-Authored-By: ReneWerner87 <renewerner87@googlemail.com> * Improve context functions * Add utils benchmarks * Update benchmarks & tests * Add utils tests * New tests * update test * Move fastpath tests * offer negotiation * Cleanup * Update Vary Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize Append Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize more methods Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add param support to Use Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add use_params tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Update app_test.go Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-Authored-By: Nifty255 <nifty255@users.noreply.github.com> * Rename argument Co-Authored-By: RW <renewerner87@googlemail.com> * Add nosec for WriteByte Co-Authored-By: RW <renewerner87@googlemail.com> * Add media article * Update media articles * Fix typo Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Fix typo Co-authored-by: ReneWerner87 <renewerner87@users.noreply.github.com> Co-authored-by: ReneWerner87 <renewerner87@googlemail.com> Co-authored-by: Vic Shóstak <vikkyshostak@gmail.com> Co-authored-by: József Sallai <jozsef@sallai.me> Co-authored-by: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-authored-by: Nifty255 <nifty255@users.noreply.github.com>
2020-05-12 19:24:04 +02:00
func Test_App_Nested_Params(t *testing.T) {
v1.9.6 (#360) **🚀 Fiber `v1.9.6`** Special thanks to @renanbastos93 & @renewerner87 for optimizing the current router. Help use translate our API documentation by [clicking here](https://crowdin.com/project/gofiber) 🔥 New - `AcquireCtx` / `ReleaseCtx` The Ctx pool is now accessible for third-party packages - Fiber docs merged [Russian](https://docs.gofiber.io/v/ru/) translations **84%** - Fiber docs merged [Spanish](https://docs.gofiber.io/v/es/) translations **65%** - Fiber docs merged [French](https://docs.gofiber.io/v/fr/) translations **40%** - Fiber docs merged [German](https://docs.gofiber.io/v/de/) translations **32%** - Fiber docs merged [Portuguese](https://docs.gofiber.io/v/pt/) translations **24%** 🩹 Fixes - Hotfix for interpolated params in nested routes https://github.com/gofiber/fiber/issues/354 - Some `Ctx` methods didn't work correctly when called without an `*App` pointer. - `ctx.Vary` sometimes added duplicates to the response header - Improved router by ditching regexp and increased performance by **817%** without allocations. ```go // Tested with 350 github API routes Benchmark_Router_OLD-4 614 2467460 ns/op 68902 B/op 600 allocs/op Benchmark_Router_NEW-4 3429 302033 ns/op 0 B/op 0 allocs/op ``` 🧹 Updates - Add context benchmarks - Remove some unnecessary functions from `utils` - Add router & param test cases - Add new coffee supporters to readme - Add third party middlewares to readme - Add more comments to source code - Cleanup some old helper functions 🧬 Middleware - [gofiber/adaptor](https://github.com/gofiber/adaptor) `v0.0.1` Converter for net/http handlers to/from Fiber handlers - [gofiber/session](https://github.com/gofiber/session) `v1.0.0` big improvements and support for storage providers - [gofiber/logger](https://github.com/gofiber/logger) `v0.0.6` supports `${error}` param - [gofiber/embed](https://github.com/gofiber/embed) `v0.0.9` minor improvements and support for directory browsing Co-authored-by: ReneWerner87 <renewerner87@googlemail.com>
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)
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
v1.9.6 (#360) **🚀 Fiber `v1.9.6`** Special thanks to @renanbastos93 & @renewerner87 for optimizing the current router. Help use translate our API documentation by [clicking here](https://crowdin.com/project/gofiber) 🔥 New - `AcquireCtx` / `ReleaseCtx` The Ctx pool is now accessible for third-party packages - Fiber docs merged [Russian](https://docs.gofiber.io/v/ru/) translations **84%** - Fiber docs merged [Spanish](https://docs.gofiber.io/v/es/) translations **65%** - Fiber docs merged [French](https://docs.gofiber.io/v/fr/) translations **40%** - Fiber docs merged [German](https://docs.gofiber.io/v/de/) translations **32%** - Fiber docs merged [Portuguese](https://docs.gofiber.io/v/pt/) translations **24%** 🩹 Fixes - Hotfix for interpolated params in nested routes https://github.com/gofiber/fiber/issues/354 - Some `Ctx` methods didn't work correctly when called without an `*App` pointer. - `ctx.Vary` sometimes added duplicates to the response header - Improved router by ditching regexp and increased performance by **817%** without allocations. ```go // Tested with 350 github API routes Benchmark_Router_OLD-4 614 2467460 ns/op 68902 B/op 600 allocs/op Benchmark_Router_NEW-4 3429 302033 ns/op 0 B/op 0 allocs/op ``` 🧹 Updates - Add context benchmarks - Remove some unnecessary functions from `utils` - Add router & param test cases - Add new coffee supporters to readme - Add third party middlewares to readme - Add more comments to source code - Cleanup some old helper functions 🧬 Middleware - [gofiber/adaptor](https://github.com/gofiber/adaptor) `v0.0.1` Converter for net/http handlers to/from Fiber handlers - [gofiber/session](https://github.com/gofiber/session) `v1.0.0` big improvements and support for storage providers - [gofiber/logger](https://github.com/gofiber/logger) `v0.0.6` supports `${error}` param - [gofiber/embed](https://github.com/gofiber/embed) `v0.0.9` minor improvements and support for directory browsing Co-authored-by: ReneWerner87 <renewerner87@googlemail.com>
2020-05-11 13:42:42 +02:00
}
Use param support + optimizations (#361) * Benchmark workflow * Update router.go * Clean root * Add mutex * Benchmark workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Benchmark Workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Update security workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Make Ctx pool accessible - Add ctx benchmarks * v1.9.6 * v1.9.6 Co-Authored-By: ReneWerner87 <renewerner87@googlemail.com> * Improve context functions * Add utils benchmarks * Update benchmarks & tests * Add utils tests * New tests * update test * Move fastpath tests * offer negotiation * Cleanup * Update Vary Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize Append Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize more methods Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add param support to Use Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add use_params tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Update app_test.go Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-Authored-By: Nifty255 <nifty255@users.noreply.github.com> * Rename argument Co-Authored-By: RW <renewerner87@googlemail.com> * Add nosec for WriteByte Co-Authored-By: RW <renewerner87@googlemail.com> * Add media article * Update media articles * Fix typo Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Fix typo Co-authored-by: ReneWerner87 <renewerner87@users.noreply.github.com> Co-authored-by: ReneWerner87 <renewerner87@googlemail.com> Co-authored-by: Vic Shóstak <vikkyshostak@gmail.com> Co-authored-by: József Sallai <jozsef@sallai.me> Co-authored-by: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-authored-by: Nifty255 <nifty255@users.noreply.github.com>
2020-05-12 19:24:04 +02:00
func Test_App_Use_Params(t *testing.T) {
app := New()
Use param support + optimizations (#361) * Benchmark workflow * Update router.go * Clean root * Add mutex * Benchmark workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Benchmark Workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Update security workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Make Ctx pool accessible - Add ctx benchmarks * v1.9.6 * v1.9.6 Co-Authored-By: ReneWerner87 <renewerner87@googlemail.com> * Improve context functions * Add utils benchmarks * Update benchmarks & tests * Add utils tests * New tests * update test * Move fastpath tests * offer negotiation * Cleanup * Update Vary Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize Append Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize more methods Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add param support to Use Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add use_params tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Update app_test.go Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-Authored-By: Nifty255 <nifty255@users.noreply.github.com> * Rename argument Co-Authored-By: RW <renewerner87@googlemail.com> * Add nosec for WriteByte Co-Authored-By: RW <renewerner87@googlemail.com> * Add media article * Update media articles * Fix typo Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Fix typo Co-authored-by: ReneWerner87 <renewerner87@users.noreply.github.com> Co-authored-by: ReneWerner87 <renewerner87@googlemail.com> Co-authored-by: Vic Shóstak <vikkyshostak@gmail.com> Co-authored-by: József Sallai <jozsef@sallai.me> Co-authored-by: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-authored-by: Nifty255 <nifty255@users.noreply.github.com>
2020-05-12 19:24:04 +02:00
app.Use("/prefix/:param", func(c *Ctx) {
utils.AssertEqual(t, "john", c.Params("param"))
})
2020-07-04 10:11:23 +02:00
app.Use("/foo/:bar?", func(c *Ctx) {
utils.AssertEqual(t, "foobar", c.Params("bar", "foobar"))
})
Use param support + optimizations (#361) * Benchmark workflow * Update router.go * Clean root * Add mutex * Benchmark workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Benchmark Workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Update security workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Make Ctx pool accessible - Add ctx benchmarks * v1.9.6 * v1.9.6 Co-Authored-By: ReneWerner87 <renewerner87@googlemail.com> * Improve context functions * Add utils benchmarks * Update benchmarks & tests * Add utils tests * New tests * update test * Move fastpath tests * offer negotiation * Cleanup * Update Vary Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize Append Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize more methods Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add param support to Use Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add use_params tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Update app_test.go Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-Authored-By: Nifty255 <nifty255@users.noreply.github.com> * Rename argument Co-Authored-By: RW <renewerner87@googlemail.com> * Add nosec for WriteByte Co-Authored-By: RW <renewerner87@googlemail.com> * Add media article * Update media articles * Fix typo Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Fix typo Co-authored-by: ReneWerner87 <renewerner87@users.noreply.github.com> Co-authored-by: ReneWerner87 <renewerner87@googlemail.com> Co-authored-by: Vic Shóstak <vikkyshostak@gmail.com> Co-authored-by: József Sallai <jozsef@sallai.me> Co-authored-by: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-authored-by: Nifty255 <nifty255@users.noreply.github.com>
2020-05-12 19:24:04 +02:00
app.Use("/:param/*", func(c *Ctx) {
utils.AssertEqual(t, "john", c.Params("param"))
utils.AssertEqual(t, "doe", c.Params("*"))
Use param support + optimizations (#361) * Benchmark workflow * Update router.go * Clean root * Add mutex * Benchmark workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Benchmark Workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Update security workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Make Ctx pool accessible - Add ctx benchmarks * v1.9.6 * v1.9.6 Co-Authored-By: ReneWerner87 <renewerner87@googlemail.com> * Improve context functions * Add utils benchmarks * Update benchmarks & tests * Add utils tests * New tests * update test * Move fastpath tests * offer negotiation * Cleanup * Update Vary Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize Append Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize more methods Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add param support to Use Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add use_params tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Update app_test.go Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-Authored-By: Nifty255 <nifty255@users.noreply.github.com> * Rename argument Co-Authored-By: RW <renewerner87@googlemail.com> * Add nosec for WriteByte Co-Authored-By: RW <renewerner87@googlemail.com> * Add media article * Update media articles * Fix typo Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Fix typo Co-authored-by: ReneWerner87 <renewerner87@users.noreply.github.com> Co-authored-by: ReneWerner87 <renewerner87@googlemail.com> Co-authored-by: Vic Shóstak <vikkyshostak@gmail.com> Co-authored-by: József Sallai <jozsef@sallai.me> Co-authored-by: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-authored-by: Nifty255 <nifty255@users.noreply.github.com>
2020-05-12 19:24:04 +02:00
})
Use param support + optimizations (#361) * Benchmark workflow * Update router.go * Clean root * Add mutex * Benchmark workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Benchmark Workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Update security workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Make Ctx pool accessible - Add ctx benchmarks * v1.9.6 * v1.9.6 Co-Authored-By: ReneWerner87 <renewerner87@googlemail.com> * Improve context functions * Add utils benchmarks * Update benchmarks & tests * Add utils tests * New tests * update test * Move fastpath tests * offer negotiation * Cleanup * Update Vary Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize Append Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize more methods Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add param support to Use Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add use_params tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Update app_test.go Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-Authored-By: Nifty255 <nifty255@users.noreply.github.com> * Rename argument Co-Authored-By: RW <renewerner87@googlemail.com> * Add nosec for WriteByte Co-Authored-By: RW <renewerner87@googlemail.com> * Add media article * Update media articles * Fix typo Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Fix typo Co-authored-by: ReneWerner87 <renewerner87@users.noreply.github.com> Co-authored-by: ReneWerner87 <renewerner87@googlemail.com> Co-authored-by: Vic Shóstak <vikkyshostak@gmail.com> Co-authored-by: József Sallai <jozsef@sallai.me> Co-authored-by: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-authored-by: Nifty255 <nifty255@users.noreply.github.com>
2020-05-12 19:24:04 +02:00
resp, err := app.Test(httptest.NewRequest("GET", "/prefix/john", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
Use param support + optimizations (#361) * Benchmark workflow * Update router.go * Clean root * Add mutex * Benchmark workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Benchmark Workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Update security workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Make Ctx pool accessible - Add ctx benchmarks * v1.9.6 * v1.9.6 Co-Authored-By: ReneWerner87 <renewerner87@googlemail.com> * Improve context functions * Add utils benchmarks * Update benchmarks & tests * Add utils tests * New tests * update test * Move fastpath tests * offer negotiation * Cleanup * Update Vary Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize Append Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize more methods Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add param support to Use Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add use_params tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Update app_test.go Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-Authored-By: Nifty255 <nifty255@users.noreply.github.com> * Rename argument Co-Authored-By: RW <renewerner87@googlemail.com> * Add nosec for WriteByte Co-Authored-By: RW <renewerner87@googlemail.com> * Add media article * Update media articles * Fix typo Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Fix typo Co-authored-by: ReneWerner87 <renewerner87@users.noreply.github.com> Co-authored-by: ReneWerner87 <renewerner87@googlemail.com> Co-authored-by: Vic Shóstak <vikkyshostak@gmail.com> Co-authored-by: József Sallai <jozsef@sallai.me> Co-authored-by: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-authored-by: Nifty255 <nifty255@users.noreply.github.com>
2020-05-12 19:24:04 +02:00
resp, err = app.Test(httptest.NewRequest("GET", "/john/doe", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
2020-07-04 10:11:23 +02:00
resp, err = app.Test(httptest.NewRequest("GET", "/foo", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
2020-07-15 17:43:30 +02:00
defer func() {
if err := recover(); err != nil {
utils.AssertEqual(t, "use: invalid handler func()\n", fmt.Sprintf("%v", err))
}
}()
app.Use("/:param/*", func() {
// this should panic
})
}
func Test_App_Add_Method_Test(t *testing.T) {
app := New()
defer func() {
if err := recover(); err != nil {
utils.AssertEqual(t, "add: invalid http method JOHN\n", fmt.Sprintf("%v", err))
}
}()
app.Add("JOHN", "/doe", func(c *Ctx) {
})
Use param support + optimizations (#361) * Benchmark workflow * Update router.go * Clean root * Add mutex * Benchmark workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Benchmark Workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Update security workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Make Ctx pool accessible - Add ctx benchmarks * v1.9.6 * v1.9.6 Co-Authored-By: ReneWerner87 <renewerner87@googlemail.com> * Improve context functions * Add utils benchmarks * Update benchmarks & tests * Add utils tests * New tests * update test * Move fastpath tests * offer negotiation * Cleanup * Update Vary Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize Append Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize more methods Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add param support to Use Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add use_params tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Update app_test.go Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-Authored-By: Nifty255 <nifty255@users.noreply.github.com> * Rename argument Co-Authored-By: RW <renewerner87@googlemail.com> * Add nosec for WriteByte Co-Authored-By: RW <renewerner87@googlemail.com> * Add media article * Update media articles * Fix typo Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Fix typo Co-authored-by: ReneWerner87 <renewerner87@users.noreply.github.com> Co-authored-by: ReneWerner87 <renewerner87@googlemail.com> Co-authored-by: Vic Shóstak <vikkyshostak@gmail.com> Co-authored-by: József Sallai <jozsef@sallai.me> Co-authored-by: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-authored-by: Nifty255 <nifty255@users.noreply.github.com>
2020-05-12 19:24:04 +02:00
}
2020-07-15 18:50:28 +02:00
func Test_App_Listen_TLS(t *testing.T) {
app := New()
// Create tls certificate
cer, err := tls.LoadX509KeyPair("./.github/TEST_DATA/ssl.pem", "./.github/TEST_DATA/ssl.key")
if err != nil {
utils.AssertEqual(t, nil, err)
}
config := &tls.Config{Certificates: []tls.Certificate{cer}}
go func() {
2020-07-15 18:56:42 +02:00
time.Sleep(1000 * time.Millisecond)
2020-07-15 18:50:28 +02:00
utils.AssertEqual(t, nil, app.Shutdown())
}()
utils.AssertEqual(t, nil, app.Listen(3078, config))
}
func Test_App_Listener_TLS(t *testing.T) {
app := New()
// Create tls certificate
cer, err := tls.LoadX509KeyPair("./.github/TEST_DATA/ssl.pem", "./.github/TEST_DATA/ssl.key")
if err != nil {
utils.AssertEqual(t, nil, err)
}
config := &tls.Config{Certificates: []tls.Certificate{cer}}
go func() {
2020-07-15 18:56:42 +02:00
time.Sleep(1000 * time.Millisecond)
2020-07-15 18:50:28 +02:00
utils.AssertEqual(t, nil, app.Shutdown())
}()
2020-07-15 18:56:42 +02:00
ln, err := net.Listen("tcp4", ":3055")
utils.AssertEqual(t, nil, err)
2020-07-15 18:50:28 +02:00
utils.AssertEqual(t, nil, app.Listener(ln, config))
}
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) {
utils.AssertEqual(t, "john", c.Params("param"))
utils.AssertEqual(t, "doe", c.Params("*"))
})
resp, err := app.Test(httptest.NewRequest("GET", "/prefix/john/doe/test", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
}
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)
})
2020-07-20 19:29:54 +02:00
// check handler count for registered HEAD route
utils.AssertEqual(t, 5, len(app.stack[methodInt(MethodHead)][0].Handlers), "app.Test(req)")
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")
}
Use param support + optimizations (#361) * Benchmark workflow * Update router.go * Clean root * Add mutex * Benchmark workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Benchmark Workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Update security workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Make Ctx pool accessible - Add ctx benchmarks * v1.9.6 * v1.9.6 Co-Authored-By: ReneWerner87 <renewerner87@googlemail.com> * Improve context functions * Add utils benchmarks * Update benchmarks & tests * Add utils tests * New tests * update test * Move fastpath tests * offer negotiation * Cleanup * Update Vary Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize Append Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize more methods Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add param support to Use Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add use_params tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Update app_test.go Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-Authored-By: Nifty255 <nifty255@users.noreply.github.com> * Rename argument Co-Authored-By: RW <renewerner87@googlemail.com> * Add nosec for WriteByte Co-Authored-By: RW <renewerner87@googlemail.com> * Add media article * Update media articles * Fix typo Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Fix typo Co-authored-by: ReneWerner87 <renewerner87@users.noreply.github.com> Co-authored-by: ReneWerner87 <renewerner87@googlemail.com> Co-authored-by: Vic Shóstak <vikkyshostak@gmail.com> Co-authored-by: József Sallai <jozsef@sallai.me> Co-authored-by: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-authored-by: Nifty255 <nifty255@users.noreply.github.com>
2020-05-12 19:24:04 +02:00
func Test_App_Order(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
c.Write("1")
c.Next()
})
app.All("/test", func(c *Ctx) {
c.Write("2")
c.Next()
})
app.Use(func(c *Ctx) {
c.Write("3")
})
req := httptest.NewRequest("GET", "/test", nil)
resp, err := app.Test(req)
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
body, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "123", string(body))
}
Use param support + optimizations (#361) * Benchmark workflow * Update router.go * Clean root * Add mutex * Benchmark workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Benchmark Workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Update security workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Make Ctx pool accessible - Add ctx benchmarks * v1.9.6 * v1.9.6 Co-Authored-By: ReneWerner87 <renewerner87@googlemail.com> * Improve context functions * Add utils benchmarks * Update benchmarks & tests * Add utils tests * New tests * update test * Move fastpath tests * offer negotiation * Cleanup * Update Vary Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize Append Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize more methods Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add param support to Use Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add use_params tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Update app_test.go Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-Authored-By: Nifty255 <nifty255@users.noreply.github.com> * Rename argument Co-Authored-By: RW <renewerner87@googlemail.com> * Add nosec for WriteByte Co-Authored-By: RW <renewerner87@googlemail.com> * Add media article * Update media articles * Fix typo Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Fix typo Co-authored-by: ReneWerner87 <renewerner87@users.noreply.github.com> Co-authored-by: ReneWerner87 <renewerner87@googlemail.com> Co-authored-by: Vic Shóstak <vikkyshostak@gmail.com> Co-authored-by: József Sallai <jozsef@sallai.me> Co-authored-by: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-authored-by: Nifty255 <nifty255@users.noreply.github.com>
2020-05-12 19:24:04 +02:00
func Test_App_Methods(t *testing.T) {
var dummyHandler = func(c *Ctx) {}
app := New()
2020-02-21 18:07:43 +01:00
app.Connect("/:john?/:doe?", dummyHandler)
testStatus200(t, app, "/john/doe", "CONNECT")
2020-02-21 18:07:43 +01:00
app.Put("/:john?/:doe?", dummyHandler)
testStatus200(t, app, "/john/doe", "PUT")
2020-02-21 18:07:43 +01:00
app.Post("/:john?/:doe?", dummyHandler)
testStatus200(t, app, "/john/doe", "POST")
2020-02-21 18:07:43 +01:00
app.Delete("/:john?/:doe?", dummyHandler)
testStatus200(t, app, "/john/doe", "DELETE")
2020-02-21 18:07:43 +01:00
app.Head("/:john?/:doe?", dummyHandler)
testStatus200(t, app, "/john/doe", "HEAD")
2020-02-21 18:07:43 +01:00
app.Patch("/:john?/:doe?", dummyHandler)
testStatus200(t, app, "/john/doe", "PATCH")
2020-02-21 18:07:43 +01:00
app.Options("/:john?/:doe?", dummyHandler)
testStatus200(t, app, "/john/doe", "OPTIONS")
2020-02-21 18:07:43 +01:00
app.Trace("/:john?/:doe?", dummyHandler)
testStatus200(t, app, "/john/doe", "TRACE")
2020-02-21 18:07:43 +01:00
app.Get("/:john?/:doe?", dummyHandler)
testStatus200(t, app, "/john/doe", "GET")
2020-02-21 18:07:43 +01: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
}
Use param support + optimizations (#361) * Benchmark workflow * Update router.go * Clean root * Add mutex * Benchmark workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Benchmark Workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Update security workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Make Ctx pool accessible - Add ctx benchmarks * v1.9.6 * v1.9.6 Co-Authored-By: ReneWerner87 <renewerner87@googlemail.com> * Improve context functions * Add utils benchmarks * Update benchmarks & tests * Add utils tests * New tests * update test * Move fastpath tests * offer negotiation * Cleanup * Update Vary Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize Append Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize more methods Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add param support to Use Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add use_params tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Update app_test.go Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-Authored-By: Nifty255 <nifty255@users.noreply.github.com> * Rename argument Co-Authored-By: RW <renewerner87@googlemail.com> * Add nosec for WriteByte Co-Authored-By: RW <renewerner87@googlemail.com> * Add media article * Update media articles * Fix typo Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Fix typo Co-authored-by: ReneWerner87 <renewerner87@users.noreply.github.com> Co-authored-by: ReneWerner87 <renewerner87@googlemail.com> Co-authored-by: Vic Shóstak <vikkyshostak@gmail.com> Co-authored-by: József Sallai <jozsef@sallai.me> Co-authored-by: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-authored-by: Nifty255 <nifty255@users.noreply.github.com>
2020-05-12 19:24:04 +02:00
func Test_App_New(t *testing.T) {
app := New()
app.Get("/", func(*Ctx) {
})
appConfig := New(&Settings{
2020-04-13 09:01:27 +02:00
Immutable: true,
})
appConfig.Get("/", func(*Ctx) {
2020-04-13 09:01:27 +02:00
})
}
Use param support + optimizations (#361) * Benchmark workflow * Update router.go * Clean root * Add mutex * Benchmark workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Benchmark Workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Update security workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Make Ctx pool accessible - Add ctx benchmarks * v1.9.6 * v1.9.6 Co-Authored-By: ReneWerner87 <renewerner87@googlemail.com> * Improve context functions * Add utils benchmarks * Update benchmarks & tests * Add utils tests * New tests * update test * Move fastpath tests * offer negotiation * Cleanup * Update Vary Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize Append Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize more methods Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add param support to Use Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add use_params tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Update app_test.go Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-Authored-By: Nifty255 <nifty255@users.noreply.github.com> * Rename argument Co-Authored-By: RW <renewerner87@googlemail.com> * Add nosec for WriteByte Co-Authored-By: RW <renewerner87@googlemail.com> * Add media article * Update media articles * Fix typo Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Fix typo Co-authored-by: ReneWerner87 <renewerner87@users.noreply.github.com> Co-authored-by: ReneWerner87 <renewerner87@googlemail.com> Co-authored-by: Vic Shóstak <vikkyshostak@gmail.com> Co-authored-by: József Sallai <jozsef@sallai.me> Co-authored-by: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-authored-by: Nifty255 <nifty255@users.noreply.github.com>
2020-05-12 19:24:04 +02:00
func Test_App_Shutdown(t *testing.T) {
app := New(&Settings{
DisableStartupMessage: true,
})
2020-07-06 17:51:25 +02:00
if err := app.Shutdown(); err != nil {
if err.Error() != "shutdown: server is not running" {
t.Fatal()
}
}
2020-04-13 09:01:27 +02:00
}
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-07-14 15:23:41 +08:00
app.Static("", "./.github/")
app.Static("test", "", Static{Index: "index.html"})
2020-05-29 10:22:54 +02:00
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")
2020-07-14 15:23:41 +08:00
utils.AssertEqual(t, false, resp.Header.Get(HeaderContentLength) == "")
utils.AssertEqual(t, MIMETextHTMLCharsetUTF8, resp.Header.Get(HeaderContentType))
2020-05-29 10:22:54 +02:00
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-07-14 15:23:41 +08:00
resp, err = app.Test(httptest.NewRequest("GET", "/not-found", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 404, resp.StatusCode, "Status code")
utils.AssertEqual(t, false, resp.Header.Get(HeaderContentLength) == "")
utils.AssertEqual(t, MIMETextPlainCharsetUTF8, resp.Header.Get(HeaderContentType))
body, err = ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "Cannot GET /not-found", string(body))
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)
2020-06-27 17:16:31 +02:00
utils.AssertEqual(t, true, strings.Contains(string(body), "gofiber.io/support"))
2020-06-03 17:16:10 +02:00
}
func Test_App_Static_Group(t *testing.T) {
app := New()
grp := app.Group("/v1", func(c *Ctx) {
c.Set("Test-Header", "123")
c.Next()
})
grp.Static("/v2", "./.github/FUNDING.yml")
req := httptest.NewRequest("GET", "/v1/v2", nil)
2020-03-04 12:30:29 +01:00
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"))
utils.AssertEqual(t, "123", resp.Header.Get("Test-Header"))
grp = app.Group("/v2")
grp.Static("/v3*", "./.github/FUNDING.yml")
req = httptest.NewRequest("GET", "/v2/v3/john/doe", nil)
2020-03-04 12:30:29 +01:00
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"))
}
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)
2020-06-27 17:16:31 +02:00
utils.AssertEqual(t, true, strings.Contains(string(body), "gofiber.io/support"))
2020-06-03 17:16:10 +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)
2020-06-27 17:16:31 +02:00
utils.AssertEqual(t, true, strings.Contains(string(body), "gofiber.io/support"))
}
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)
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("/single", "./.github/workflows/test.yml")
req = httptest.NewRequest("GET", "/single", nil)
2020-03-04 12:30:29 +01:00
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-03-04 12:30:29 +01:00
}
2020-02-21 18:07:43 +01: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-07-16 16:42:36 +08:00
func Test_App_Group_Invalid(t *testing.T) {
defer func() {
if err := recover(); err != nil {
utils.AssertEqual(t, "use: invalid handler int\n", fmt.Sprintf("%v", err))
}
}()
New().Group("/").Use(1)
}
Use param support + optimizations (#361) * Benchmark workflow * Update router.go * Clean root * Add mutex * Benchmark workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Benchmark Workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Update security workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Make Ctx pool accessible - Add ctx benchmarks * v1.9.6 * v1.9.6 Co-Authored-By: ReneWerner87 <renewerner87@googlemail.com> * Improve context functions * Add utils benchmarks * Update benchmarks & tests * Add utils tests * New tests * update test * Move fastpath tests * offer negotiation * Cleanup * Update Vary Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize Append Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize more methods Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add param support to Use Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add use_params tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Update app_test.go Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-Authored-By: Nifty255 <nifty255@users.noreply.github.com> * Rename argument Co-Authored-By: RW <renewerner87@googlemail.com> * Add nosec for WriteByte Co-Authored-By: RW <renewerner87@googlemail.com> * Add media article * Update media articles * Fix typo Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Fix typo Co-authored-by: ReneWerner87 <renewerner87@users.noreply.github.com> Co-authored-by: ReneWerner87 <renewerner87@googlemail.com> Co-authored-by: Vic Shóstak <vikkyshostak@gmail.com> Co-authored-by: József Sallai <jozsef@sallai.me> Co-authored-by: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-authored-by: Nifty255 <nifty255@users.noreply.github.com>
2020-05-12 19:24:04 +02:00
func Test_App_Group(t *testing.T) {
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")
grp.Get("/", dummyHandler)
testStatus200(t, app, "/test", "GET")
2020-02-21 18:07:43 +01:00
grp.Get("/:demo?", dummyHandler)
testStatus200(t, app, "/test/john", "GET")
2020-02-21 18:07:43 +01:00
grp.Connect("/CONNECT", dummyHandler)
testStatus200(t, app, "/test/CONNECT", "CONNECT")
2020-02-21 18:07:43 +01:00
grp.Put("/PUT", dummyHandler)
testStatus200(t, app, "/test/PUT", "PUT")
2020-02-21 18:07:43 +01:00
grp.Post("/POST", dummyHandler)
testStatus200(t, app, "/test/POST", "POST")
2020-02-21 18:07:43 +01:00
grp.Delete("/DELETE", dummyHandler)
testStatus200(t, app, "/test/DELETE", "DELETE")
2020-02-21 18:07:43 +01:00
grp.Head("/HEAD", dummyHandler)
testStatus200(t, app, "/test/HEAD", "HEAD")
2020-02-21 18:07:43 +01:00
grp.Patch("/PATCH", dummyHandler)
testStatus200(t, app, "/test/PATCH", "PATCH")
2020-02-21 18:07:43 +01:00
grp.Options("/OPTIONS", dummyHandler)
testStatus200(t, app, "/test/OPTIONS", "OPTIONS")
2020-02-21 18:07:43 +01:00
grp.Trace("/TRACE", dummyHandler)
testStatus200(t, app, "/test/TRACE", "TRACE")
2020-02-21 18:07:43 +01:00
grp.All("/ALL", dummyHandler)
testStatus200(t, app, "/test/ALL", "POST")
2020-02-21 18:07:43 +01:00
grp.Use("/USE", dummyHandler)
testStatus200(t, app, "/test/USE/oke", "GET")
2020-02-21 18:07:43 +01:00
api := grp.Group("/v1")
api.Post("/", dummyHandler)
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
api.Get("/users", dummyHandler)
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
}
func Test_App_Deep_Group(t *testing.T) {
runThroughCount := 0
var dummyHandler = func(c *Ctx) {
runThroughCount++
c.Next()
}
app := New()
2020-07-12 16:26:08 +08:00
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-07-10 13:57:53 +02:00
// go test -run Test_App_Next_Method
func Test_App_Next_Method(t *testing.T) {
app := New()
app.Settings.DisableStartupMessage = true
app.Use(func(c *Ctx) {
utils.AssertEqual(t, "GET", c.Method())
c.Next()
utils.AssertEqual(t, "GET", c.Method())
})
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 404, resp.StatusCode, "Status code")
}
2020-07-13 15:41:22 +02:00
// go test -run Test_App_Listen
Use param support + optimizations (#361) * Benchmark workflow * Update router.go * Clean root * Add mutex * Benchmark workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Benchmark Workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Update security workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Make Ctx pool accessible - Add ctx benchmarks * v1.9.6 * v1.9.6 Co-Authored-By: ReneWerner87 <renewerner87@googlemail.com> * Improve context functions * Add utils benchmarks * Update benchmarks & tests * Add utils tests * New tests * update test * Move fastpath tests * offer negotiation * Cleanup * Update Vary Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize Append Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize more methods Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add param support to Use Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add use_params tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Update app_test.go Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-Authored-By: Nifty255 <nifty255@users.noreply.github.com> * Rename argument Co-Authored-By: RW <renewerner87@googlemail.com> * Add nosec for WriteByte Co-Authored-By: RW <renewerner87@googlemail.com> * Add media article * Update media articles * Fix typo Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Fix typo Co-authored-by: ReneWerner87 <renewerner87@users.noreply.github.com> Co-authored-by: ReneWerner87 <renewerner87@googlemail.com> Co-authored-by: Vic Shóstak <vikkyshostak@gmail.com> Co-authored-by: József Sallai <jozsef@sallai.me> Co-authored-by: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-authored-by: Nifty255 <nifty255@users.noreply.github.com>
2020-05-12 19:24:04 +02:00
func Test_App_Listen(t *testing.T) {
2020-07-15 17:43:30 +02:00
app := New()
2020-07-15 10:20:53 +08:00
utils.AssertEqual(t, false, app.Listen(1.23) == nil)
2020-07-16 16:42:36 +08:00
utils.AssertEqual(t, false, app.Listen(":1.23") == nil)
2020-04-13 09:01:27 +02:00
go func() {
time.Sleep(1000 * time.Millisecond)
utils.AssertEqual(t, nil, app.Shutdown())
2020-04-13 09:01:27 +02:00
}()
2020-07-15 10:20:53 +08: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() {
time.Sleep(1000 * time.Millisecond)
utils.AssertEqual(t, nil, app.Shutdown())
2020-04-13 09:01:27 +02:00
}()
2020-07-18 10:42:22 +08:00
utils.AssertEqual(t, nil, app.Listen("[::]:4010"))
2020-04-13 09:01:27 +02:00
}
2020-07-13 15:41:22 +02:00
// go test -run Test_App_Listener
2020-07-08 21:07:24 +02:00
func Test_App_Listener(t *testing.T) {
2020-04-13 09:01:27 +02:00
app := New(&Settings{
2020-07-15 10:20:53 +08:00
Prefork: true,
2020-04-13 09:01:27 +02:00
})
2020-05-13 02:54:35 +02:00
2020-04-13 09:01:27 +02:00
go func() {
2020-07-14 15:23:41 +08:00
time.Sleep(500 * time.Millisecond)
utils.AssertEqual(t, nil, app.Shutdown())
2020-04-13 09:01:27 +02:00
}()
2020-05-13 02:54:35 +02:00
2020-07-14 15:23:41 +08:00
ln := fasthttputil.NewInmemoryListener()
2020-07-08 21:07:24 +02:00
utils.AssertEqual(t, nil, app.Listener(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)
}
utils.AssertEqual(b, `W/"13-1831710635"`, string(c.Fasthttp.Response.Header.Peek(HeaderETag)))
2020-04-13 09:01:27 +02:00
}
2020-07-14 15:23:41 +08:00
// go test -run Test_NewError
func Test_NewError(t *testing.T) {
e := NewError(StatusForbidden, "permission denied")
utils.AssertEqual(t, StatusForbidden, e.Code)
utils.AssertEqual(t, "permission denied", e.Message)
}
2020-07-15 10:20:53 +08:00
func Test_Test_Timeout(t *testing.T) {
app := New()
app.Settings.DisableStartupMessage = true
app.Get("/", func(_ *Ctx) {})
resp, err := app.Test(httptest.NewRequest("GET", "/", nil), -1)
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
app.Get("timeout", func(c *Ctx) {
time.Sleep(55 * time.Millisecond)
})
_, err = app.Test(httptest.NewRequest("GET", "/timeout", nil), 50)
utils.AssertEqual(t, true, err != nil, "app.Test(req)")
}
2020-07-29 09:14:15 +08:00
type errorReader int
func (errorReader) Read([]byte) (int, error) {
return 0, errors.New("errorReader")
}
func Test_Test_DumpError(t *testing.T) {
app := New()
app.Settings.DisableStartupMessage = true
app.Get("/", func(_ *Ctx) {})
resp, err := app.Test(httptest.NewRequest("GET", "/", errorReader(0)))
utils.AssertEqual(t, true, resp == nil)
utils.AssertEqual(t, "errorReader", err.Error())
}
2020-07-15 10:20:53 +08:00
func Test_App_Handler(t *testing.T) {
h := New().Handler()
utils.AssertEqual(t, "fasthttp.RequestHandler", reflect.TypeOf(h).String())
}
2020-07-16 16:42:36 +08:00
type invalidView struct{}
func (invalidView) Load() error { return errors.New("invalid view") }
func (i invalidView) Render(io.Writer, string, interface{}, ...string) error { panic("implement me") }
func Test_App_Init_Error_View(t *testing.T) {
app := New(&Settings{Views: invalidView{}})
app.init()
defer func() {
if err := recover(); err != nil {
utils.AssertEqual(t, "implement me", fmt.Sprintf("%v", err))
}
}()
_ = app.Settings.Views.Render(nil, "", nil)
}
2020-07-28 16:48:05 +08:00
func Test_App_Stack(t *testing.T) {
app := New()
app.Use("/path0", func(_ *Ctx) {})
app.Get("/path1", func(_ *Ctx) {})
app.Get("/path2", func(_ *Ctx) {})
app.Post("/path3", func(_ *Ctx) {})
stack := app.Stack()
utils.AssertEqual(t, 9, len(stack))
utils.AssertEqual(t, 3, len(stack[methodInt(MethodGet)]))
utils.AssertEqual(t, 3, len(stack[methodInt(MethodHead)]))
utils.AssertEqual(t, 2, len(stack[methodInt(MethodPost)]))
utils.AssertEqual(t, 1, len(stack[methodInt(MethodPut)]))
utils.AssertEqual(t, 1, len(stack[methodInt(MethodPatch)]))
utils.AssertEqual(t, 1, len(stack[methodInt(MethodDelete)]))
utils.AssertEqual(t, 1, len(stack[methodInt(MethodConnect)]))
utils.AssertEqual(t, 1, len(stack[methodInt(MethodOptions)]))
utils.AssertEqual(t, 1, len(stack[methodInt(MethodTrace)]))
}
2020-07-29 08:46:36 +08:00
// go test -run Test_App_ReadTimeout
func Test_App_ReadTimeout(t *testing.T) {
app := New(&Settings{
ReadTimeout: time.Nanosecond,
2020-07-29 13:08:33 +08:00
IdleTimeout: time.Minute,
2020-07-29 08:46:36 +08:00
DisableStartupMessage: true,
2020-07-29 13:08:33 +08:00
DisableKeepalive: true,
2020-07-29 08:46:36 +08:00
})
app.Get("/read-timeout", func(c *Ctx) {
c.SendString("I should not be sent")
})
go func() {
time.Sleep(500 * time.Millisecond)
2020-07-29 13:08:33 +08:00
conn, err := net.Dial("tcp4", "127.0.0.1:4004")
utils.AssertEqual(t, nil, err)
defer conn.Close()
_, err = conn.Write([]byte("HEAD /read-timeout HTTP/1.1\r\n"))
2020-07-29 08:46:36 +08:00
utils.AssertEqual(t, nil, err)
2020-07-29 13:08:33 +08:00
buf := make([]byte, 1024)
var n int
n, err = conn.Read(buf)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, bytes.Contains(buf[:n], []byte("408 Request Timeout")))
2020-07-29 08:46:36 +08:00
utils.AssertEqual(t, nil, app.Shutdown())
}()
utils.AssertEqual(t, nil, app.Listen(4004))
}
// go test -run Test_App_BadRequest
func Test_App_BadRequest(t *testing.T) {
app := New(&Settings{
DisableStartupMessage: true,
})
app.Get("/bad-request", func(c *Ctx) {
c.SendString("I should not be sent")
})
go func() {
time.Sleep(500 * time.Millisecond)
conn, err := net.Dial("tcp4", "127.0.0.1:4004")
utils.AssertEqual(t, nil, err)
defer conn.Close()
_, err = conn.Write([]byte("BadRequest\r\n"))
utils.AssertEqual(t, nil, err)
buf := make([]byte, 1024)
var n int
n, err = conn.Read(buf)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, bytes.Contains(buf[:n], []byte("400 Bad Request")))
utils.AssertEqual(t, nil, app.Shutdown())
}()
utils.AssertEqual(t, nil, app.Listen(4004))
}
// go test -run Test_App_SmallReadBuffer
func Test_App_SmallReadBuffer(t *testing.T) {
app := New(&Settings{
ReadBufferSize: 1,
DisableStartupMessage: true,
})
app.Get("/small-read-buffer", func(c *Ctx) {
c.SendString("I should not be sent")
})
go func() {
time.Sleep(500 * time.Millisecond)
resp, err := http.Get("http://127.0.0.1:4004/small-read-buffer")
if resp != nil {
utils.AssertEqual(t, 431, resp.StatusCode)
}
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, nil, app.Shutdown())
}()
utils.AssertEqual(t, nil, app.Listen(4004))
}