Update test & benchmark (#374)

Co-Authored-By: RW <renewerner87@googlemail.com>

* Add nosec for WriteByte

* test persistens for benchmark results

* Add tests & benchmark

* Update app_test.go

* Update benchmark.yml
pull/380/head
Fenny 2020-05-13 20:21:49 +02:00 committed by GitHub
parent f5ad2c0766
commit e0b13d9ca4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 9468 additions and 9378 deletions

1112
.github/README.md vendored

File diff suppressed because it is too large Load Diff

1102
.github/README_de.md vendored

File diff suppressed because it is too large Load Diff

1104
.github/README_es.md vendored

File diff suppressed because it is too large Load Diff

1104
.github/README_fr.md vendored

File diff suppressed because it is too large Load Diff

1424
.github/README_he.md vendored

File diff suppressed because it is too large Load Diff

1108
.github/README_id.md vendored

File diff suppressed because it is too large Load Diff

1112
.github/README_ja.md vendored

File diff suppressed because it is too large Load Diff

1112
.github/README_ko.md vendored

File diff suppressed because it is too large Load Diff

1112
.github/README_nl.md vendored

File diff suppressed because it is too large Load Diff

1104
.github/README_pt.md vendored

File diff suppressed because it is too large Load Diff

1114
.github/README_ru.md vendored

File diff suppressed because it is too large Load Diff

1100
.github/README_tr.md vendored

File diff suppressed because it is too large Load Diff

1110
.github/README_zh-CN.md vendored

File diff suppressed because it is too large Load Diff

View File

@ -22,7 +22,7 @@ jobs:
with:
tool: 'go'
output-file-path: output.txt
github-token: ${{ secrets.PERSONAL_GITHUB_TOKEN }}
github-token: ${{ secrets.BENCHMARK_TOKEN }}
fail-on-alert: true
comment-on-alert: true
auto-push: true

20
app.go
View File

@ -186,6 +186,16 @@ func (app *App) Use(args ...interface{}) *App {
return app
}
// Add : https://fiber.wiki/application#http-methods
func (app *App) Add(method, path string, handlers ...func(*Ctx)) *App {
method = strings.ToUpper(method)
if methodINT[method] == 0 && method != "GET" {
log.Fatalf("Add: Invalid HTTP method %s", method)
}
app.registerMethod(method, path, handlers...)
return app
}
// Connect : https://fiber.wiki/application#http-methods
func (app *App) Connect(path string, handlers ...func(*Ctx)) *App {
app.registerMethod(MethodConnect, path, handlers...)
@ -285,6 +295,16 @@ func (grp *Group) Use(args ...interface{}) *Group {
return grp
}
// Add : https://fiber.wiki/application#http-methods
func (grp *Group) Add(method, path string, handlers ...func(*Ctx)) *Group {
method = strings.ToUpper(method)
if methodINT[method] == 0 && method != "GET" {
log.Fatalf("Add: Invalid HTTP method %s", method)
}
grp.app.registerMethod(method, getGroupPath(grp.prefix, path), handlers...)
return grp
}
// Connect : https://fiber.wiki/application#http-methods
func (grp *Group) Connect(path string, handlers ...func(*Ctx)) *Group {
grp.app.registerMethod(MethodConnect, getGroupPath(grp.prefix, path), handlers...)

View File

@ -1,7 +1,7 @@
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package fiber
// // go test -v ./... -run=^$ -bench=Benchmark_Ctx_Acce -benchmem -count=3
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package fiber
// // go test -v ./... -run=^$ -bench=Benchmark_Ctx_Acce -benchmem -count=3

View File

@ -63,6 +63,24 @@ func Test_App_Use_Params(t *testing.T) {
assertEqual(t, nil, err, "app.Test(req)")
assertEqual(t, 200, resp.StatusCode, "Status code")
}
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) {
assertEqual(t, "john", c.Params("param"))
assertEqual(t, "doe", c.Params("*"))
})
resp, err := app.Test(httptest.NewRequest("GET", "/prefix/john/doe/test", nil))
assertEqual(t, nil, err, "app.Test(req)")
assertEqual(t, 200, resp.StatusCode, "Status code")
}
func Test_App_Order(t *testing.T) {
app := New()
@ -241,16 +259,18 @@ func Test_App_Listen(t *testing.T) {
DisableStartupMessage: true,
})
go func() {
time.Sleep(1 * time.Millisecond)
time.Sleep(1000 * time.Millisecond)
assertEqual(t, nil, app.Shutdown())
}()
assertEqual(t, nil, app.Listen(3002))
assertEqual(t, nil, app.Listen(4003))
go func() {
time.Sleep(500 * time.Millisecond)
time.Sleep(1000 * time.Millisecond)
assertEqual(t, nil, app.Shutdown())
}()
assertEqual(t, nil, app.Listen("3003"))
assertEqual(t, nil, app.Listen("4010"))
}
func Test_App_Serve(t *testing.T) {
@ -258,11 +278,11 @@ func Test_App_Serve(t *testing.T) {
DisableStartupMessage: true,
Prefork: true,
})
ln, err := net.Listen("tcp4", ":3004")
ln, err := net.Listen("tcp4", ":4020")
assertEqual(t, nil, err)
go func() {
time.Sleep(500 * time.Millisecond)
time.Sleep(1000 * time.Millisecond)
assertEqual(t, nil, app.Shutdown())
}()

2
ctx.go
View File

@ -24,7 +24,7 @@ import (
"time"
schema "github.com/gorilla/schema"
"github.com/valyala/bytebufferpool"
bytebufferpool "github.com/valyala/bytebufferpool"
fasthttp "github.com/valyala/fasthttp"
)

View File

@ -13,7 +13,7 @@ import (
"github.com/valyala/fasthttp"
)
// go test -v ./... -run=^$ -bench=Benchmark_Ctx_Params -benchmem -count=3
// go test -v ./... -run=^$ -bench=Benchmark_Ctx -benchmem -count=3
func Benchmark_Ctx_Accepts(b *testing.B) {
c := AcquireCtx(&fasthttp.RequestCtx{})

File diff suppressed because it is too large Load Diff

18
go.mod
View File

@ -1,9 +1,9 @@
module github.com/gofiber/fiber
go 1.11
require (
github.com/gorilla/schema v1.1.0
github.com/valyala/bytebufferpool v1.0.0
github.com/valyala/fasthttp v1.12.0
)
module github.com/gofiber/fiber
go 1.11
require (
github.com/gorilla/schema v1.1.0
github.com/valyala/bytebufferpool v1.0.0
github.com/valyala/fasthttp v1.12.0
)

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,8 @@
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 📝 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
// ⚠️ This path parser was based on urlpath by @ucarion (MIT License).
// 💖 Modified for the Fiber router by @renanbastos93 & @renewerner87
// 🤖 ucarion/urlpath - renanbastos93/fastpath - renewerner87/fastpath
package fiber
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 📝 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
// ⚠️ This path parser was based on urlpath by @ucarion (MIT License).
// 💖 Modified for the Fiber router by @renanbastos93 & @renewerner87
// 🤖 ucarion/urlpath - renanbastos93/fastpath - renewerner87/fastpath
package fiber

View File

@ -209,11 +209,11 @@ var methodINT = map[string]int{
MethodHead: 1,
MethodPost: 2,
MethodPut: 3,
MethodPatch: 4,
MethodDelete: 5,
MethodConnect: 6,
MethodOptions: 7,
MethodTrace: 8,
MethodDelete: 4,
MethodConnect: 5,
MethodOptions: 6,
MethodTrace: 7,
MethodPatch: 8,
}
// HTTP status codes were copied from net/http.

View File

@ -1,122 +1,122 @@
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 📝 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package fiber
import (
"testing"
)
// go test -v ./... -run=^$ -bench=Benchmark_CC_ -benchmem -count=3
// func Benchmark_Utils_assertEqual(b *testing.B) {
// // TODO
// }
func Benchmark_Utils_getGroupPath(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = getGroupPath("/v1", "/")
_ = getGroupPath("/v1", "/api")
_ = getGroupPath("/v1", "/api/register/:project")
_ = getGroupPath("/v1/long/path/john/doe", "/why/this/name/is/so/awesome")
}
}
func Benchmark_Utils_getMIME(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = getMIME(".json")
_ = getMIME(".xml")
_ = getMIME("xml")
_ = getMIME("json")
}
}
// func Benchmark_Utils_getArgument(b *testing.B) {
// // TODO
// }
// func Benchmark_Utils_parseTokenList(b *testing.B) {
// // TODO
// }
func Benchmark_Utils_getString(b *testing.B) {
raw := []byte("Hello, World!")
for n := 0; n < b.N; n++ {
_ = getString(raw)
}
}
func Benchmark_Utils_getStringImmutable(b *testing.B) {
raw := []byte("Hello, World!")
for n := 0; n < b.N; n++ {
_ = getStringImmutable(raw)
}
}
func Benchmark_Utils_getBytes(b *testing.B) {
raw := "Hello, World!"
for n := 0; n < b.N; n++ {
_ = getBytes(raw)
}
}
func Benchmark_Utils_getBytesImmutable(b *testing.B) {
raw := "Hello, World!"
for n := 0; n < b.N; n++ {
_ = getBytesImmutable(raw)
}
}
func Benchmark_Utils_methodINT(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = methodINT[MethodGet]
_ = methodINT[MethodHead]
_ = methodINT[MethodPost]
_ = methodINT[MethodPut]
_ = methodINT[MethodPatch]
_ = methodINT[MethodDelete]
_ = methodINT[MethodConnect]
_ = methodINT[MethodOptions]
_ = methodINT[MethodTrace]
}
}
func Benchmark_Utils_statusMessage(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = statusMessage[100]
_ = statusMessage[304]
_ = statusMessage[423]
_ = statusMessage[507]
}
}
func Benchmark_Utils_extensionMIME(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = extensionMIME[".json"]
_ = extensionMIME["json"]
_ = extensionMIME["xspf"]
_ = extensionMIME[".xspf"]
_ = extensionMIME["avi"]
_ = extensionMIME[".avi"]
}
}
// func Benchmark_Utils_getParams(b *testing.B) {
// // TODO
// }
// func Benchmark_Utils_matchParams(b *testing.B) {
// // TODO
// }
func Benchmark_Utils_getTrimmedParam(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = getTrimmedParam(":param")
_ = getTrimmedParam(":param?")
}
}
// func Benchmark_Utils_getCharPos(b *testing.B) {
// // TODO
// }
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 📝 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package fiber
import (
"testing"
)
// go test -v ./... -run=^$ -bench=Benchmark_CC_ -benchmem -count=3
// func Benchmark_Utils_assertEqual(b *testing.B) {
// // TODO
// }
func Benchmark_Utils_getGroupPath(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = getGroupPath("/v1", "/")
_ = getGroupPath("/v1", "/api")
_ = getGroupPath("/v1", "/api/register/:project")
_ = getGroupPath("/v1/long/path/john/doe", "/why/this/name/is/so/awesome")
}
}
func Benchmark_Utils_getMIME(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = getMIME(".json")
_ = getMIME(".xml")
_ = getMIME("xml")
_ = getMIME("json")
}
}
// func Benchmark_Utils_getArgument(b *testing.B) {
// // TODO
// }
// func Benchmark_Utils_parseTokenList(b *testing.B) {
// // TODO
// }
func Benchmark_Utils_getString(b *testing.B) {
raw := []byte("Hello, World!")
for n := 0; n < b.N; n++ {
_ = getString(raw)
}
}
func Benchmark_Utils_getStringImmutable(b *testing.B) {
raw := []byte("Hello, World!")
for n := 0; n < b.N; n++ {
_ = getStringImmutable(raw)
}
}
func Benchmark_Utils_getBytes(b *testing.B) {
raw := "Hello, World!"
for n := 0; n < b.N; n++ {
_ = getBytes(raw)
}
}
func Benchmark_Utils_getBytesImmutable(b *testing.B) {
raw := "Hello, World!"
for n := 0; n < b.N; n++ {
_ = getBytesImmutable(raw)
}
}
func Benchmark_Utils_methodINT(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = methodINT[MethodGet]
_ = methodINT[MethodHead]
_ = methodINT[MethodPost]
_ = methodINT[MethodPut]
_ = methodINT[MethodPatch]
_ = methodINT[MethodDelete]
_ = methodINT[MethodConnect]
_ = methodINT[MethodOptions]
_ = methodINT[MethodTrace]
}
}
func Benchmark_Utils_statusMessage(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = statusMessage[100]
_ = statusMessage[304]
_ = statusMessage[423]
_ = statusMessage[507]
}
}
func Benchmark_Utils_extensionMIME(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = extensionMIME[".json"]
_ = extensionMIME["json"]
_ = extensionMIME["xspf"]
_ = extensionMIME[".xspf"]
_ = extensionMIME["avi"]
_ = extensionMIME[".avi"]
}
}
// func Benchmark_Utils_getParams(b *testing.B) {
// // TODO
// }
// func Benchmark_Utils_matchParams(b *testing.B) {
// // TODO
// }
func Benchmark_Utils_getTrimmedParam(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = getTrimmedParam(":param")
_ = getTrimmedParam(":param?")
}
}
// func Benchmark_Utils_getCharPos(b *testing.B) {
// // TODO
// }

View File

@ -1,255 +1,255 @@
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 📝 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package fiber
import (
"fmt"
"testing"
)
// func Test_Utils_assertEqual(t *testing.T) {
// // TODO
// }
// func Test_Utils_setETag(t *testing.T) {
// // TODO
// }
func Test_Utils_getGroupPath(t *testing.T) {
res := getGroupPath("/v1", "/")
assertEqual(t, "/v1", res)
res = getGroupPath("/v1", "/")
assertEqual(t, "/v1", res)
res = getGroupPath("/", "/")
assertEqual(t, "/", res)
res = getGroupPath("/v1/api/", "/")
assertEqual(t, "/v1/api/", res)
}
func Test_Utils_getMIME(t *testing.T) {
res := getMIME(".json")
assertEqual(t, "application/json", res)
res = getMIME(".xml")
assertEqual(t, "application/xml", res)
res = getMIME("xml")
assertEqual(t, "application/xml", res)
res = getMIME("json")
assertEqual(t, "application/json", res)
}
// func Test_Utils_getArgument(t *testing.T) {
// // TODO
// }
// func Test_Utils_parseTokenList(t *testing.T) {
// // TODO
// }
func Test_Utils_getString(t *testing.T) {
res := getString([]byte("Hello, World!"))
assertEqual(t, "Hello, World!", res)
}
func Test_Utils_getStringImmutable(t *testing.T) {
res := getStringImmutable([]byte("Hello, World!"))
assertEqual(t, "Hello, World!", res)
}
func Test_Utils_getBytes(t *testing.T) {
res := getBytes("Hello, World!")
assertEqual(t, []byte("Hello, World!"), res)
}
func Test_Utils_getBytesImmutable(t *testing.T) {
res := getBytesImmutable("Hello, World!")
assertEqual(t, []byte("Hello, World!"), res)
}
func Test_Utils_methodINT(t *testing.T) {
res := methodINT[MethodGet]
assertEqual(t, 0, res)
res = methodINT[MethodHead]
assertEqual(t, 1, res)
res = methodINT[MethodPost]
assertEqual(t, 2, res)
res = methodINT[MethodPut]
assertEqual(t, 3, res)
res = methodINT[MethodPatch]
assertEqual(t, 4, res)
res = methodINT[MethodDelete]
assertEqual(t, 5, res)
res = methodINT[MethodConnect]
assertEqual(t, 6, res)
res = methodINT[MethodOptions]
assertEqual(t, 7, res)
res = methodINT[MethodTrace]
assertEqual(t, 8, res)
}
func Test_Utils_statusMessage(t *testing.T) {
res := statusMessage[102]
assertEqual(t, "Processing", res)
res = statusMessage[303]
assertEqual(t, "See Other", res)
res = statusMessage[404]
assertEqual(t, "Not Found", res)
res = statusMessage[507]
assertEqual(t, "Insufficient Storage", res)
}
func Test_Utils_extensionMIME(t *testing.T) {
res := extensionMIME[".html"]
assertEqual(t, "text/html", res)
res = extensionMIME["html"]
assertEqual(t, "text/html", res)
res = extensionMIME[".msp"]
assertEqual(t, "application/octet-stream", res)
res = extensionMIME["msp"]
assertEqual(t, "application/octet-stream", res)
}
// func Test_Utils_getParams(t *testing.T) {
// // TODO
// }
func Test_Utils_matchParams(t *testing.T) {
type testparams struct {
url string
params []string
match bool
}
testCase := func(r string, cases []testparams) {
parser := getParams(r)
for _, c := range cases {
params, match := parser.getMatch(c.url, false)
assertEqual(t, c.params, params, fmt.Sprintf("route: '%s', url: '%s'", r, c.url))
assertEqual(t, c.match, match, fmt.Sprintf("route: '%s', url: '%s'", r, c.url))
}
}
testCase("/api/v1/:param/*", []testparams{
{url: "/api/v1/entity", params: []string{"entity", ""}, match: true},
{url: "/api/v1/entity/", params: []string{"entity", ""}, match: true},
{url: "/api/v1/entity/1", params: []string{"entity", "1"}, match: true},
{url: "/api/v", params: nil, match: false},
{url: "/api/v2", params: nil, match: false},
{url: "/api/v1/", params: nil, match: false},
})
testCase("/api/v1/:param?", []testparams{
{url: "/api/v1", params: []string{""}, match: true},
{url: "/api/v1/", params: []string{""}, match: true},
{url: "/api/v1/optional", params: []string{"optional"}, match: true},
{url: "/api/v", params: nil, match: false},
{url: "/api/v2", params: nil, match: false},
{url: "/api/xyz", params: nil, match: false},
})
testCase("/api/v1/*", []testparams{
{url: "/api/v1", params: []string{""}, match: true},
{url: "/api/v1/", params: []string{""}, match: true},
{url: "/api/v1/entity", params: []string{"entity"}, match: true},
{url: "/api/v1/entity/1/2", params: []string{"entity/1/2"}, match: true},
{url: "/api/v", params: nil, match: false},
{url: "/api/v2", params: nil, match: false},
{url: "/api/abc", params: nil, match: false},
})
testCase("/api/v1/:param", []testparams{
{url: "/api/v1/entity", params: []string{"entity"}, match: true},
{url: "/api/v1/entity/8728382", params: nil, match: false},
{url: "/api/v1", params: nil, match: false},
{url: "/api/v1/", params: nil, match: false},
})
testCase("/api/v1/const", []testparams{
{url: "/api/v1/const", params: []string{}, match: true},
{url: "/api/v1", params: nil, match: false},
{url: "/api/v1/", params: nil, match: false},
{url: "/api/v1/something", params: nil, match: false},
})
testCase("/api/v1/:param/abc/*", []testparams{
{url: "/api/v1/well/abc/wildcard", params: []string{"well", "wildcard"}, match: true},
{url: "/api/v1/well/abc/", params: []string{"well", ""}, match: true},
{url: "/api/v1/well/abc", params: []string{"well", ""}, match: true},
{url: "/api/v1/well/ttt", params: nil, match: false},
})
testCase("/api/:day/:month?/:year?", []testparams{
{url: "/api/1", params: []string{"1", "", ""}, match: true},
{url: "/api/1/", params: []string{"1", "", ""}, match: true},
{url: "/api/1/2", params: []string{"1", "2", ""}, match: true},
{url: "/api/1/2/3", params: []string{"1", "2", "3"}, match: true},
{url: "/api/", params: nil, match: false},
})
testCase("/api/*", []testparams{
{url: "/api/", params: []string{""}, match: true},
{url: "/api/joker", params: []string{"joker"}, match: true},
{url: "/api", params: []string{""}, match: true},
{url: "/api/v1/entity", params: []string{"v1/entity"}, match: true},
{url: "/api2/v1/entity", params: nil, match: false},
{url: "/api_ignore/v1/entity", params: nil, match: false},
})
testCase("/api/*/:param?", []testparams{
{url: "/api/", params: []string{"", ""}, match: true},
{url: "/api/joker", params: []string{"joker", ""}, match: true},
{url: "/api/joker/batman", params: []string{"joker", "batman"}, match: true},
{url: "/api/joker/batman/robin", params: []string{"joker/batman", "robin"}, match: true},
{url: "/api/joker/batman/robin/1", params: []string{"joker/batman/robin", "1"}, match: true},
{url: "/api", params: []string{"", ""}, match: true},
})
testCase("/api/*/:param", []testparams{
{url: "/api/test/abc", params: []string{"test", "abc"}, match: true},
{url: "/api/joker/batman", params: []string{"joker", "batman"}, match: true},
{url: "/api/joker/batman/robin", params: []string{"joker/batman", "robin"}, match: true},
{url: "/api/joker/batman/robin/1", params: []string{"joker/batman/robin", "1"}, match: true},
{url: "/api", params: nil, match: false},
})
testCase("/api/*/:param/:param2", []testparams{
{url: "/api/test/abc", params: nil, match: false},
{url: "/api/joker/batman", params: nil, match: false},
{url: "/api/joker/batman/robin", params: []string{"joker", "batman", "robin"}, match: true},
{url: "/api/joker/batman/robin/1", params: []string{"joker/batman", "robin", "1"}, match: true},
{url: "/api/joker/batman/robin/1/2", params: []string{"joker/batman/robin", "1", "2"}, match: true},
{url: "/api", params: nil, match: false},
})
testCase("/", []testparams{
{url: "/api", params: nil, match: false},
{url: "", params: []string{}, match: true},
{url: "/", params: []string{}, match: true},
})
testCase("/config/abc.json", []testparams{
{url: "/config/abc.json", params: []string{}, match: true},
{url: "config/abc.json", params: nil, match: false},
{url: "/config/efg.json", params: nil, match: false},
{url: "/config", params: nil, match: false},
})
testCase("/config/*.json", []testparams{
{url: "/config/abc.json", params: []string{"abc.json"}, match: true},
{url: "/config/efg.json", params: []string{"efg.json"}, match: true},
//{url: "/config/efg.csv", params: nil, match: false},// doesn`t work, current: params: "efg.csv", true
{url: "config/abc.json", params: nil, match: false},
{url: "/config", params: nil, match: false},
})
testCase("/xyz", []testparams{
{url: "xyz", params: nil, match: false},
{url: "xyz/", params: nil, match: false},
})
}
// func Test_Utils_getTrimmedParam(t *testing.T) {
// // TODO
// }
// func Test_Utils_getCharPos(t *testing.T) {
// // TODO
// }
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 📝 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package fiber
import (
"fmt"
"testing"
)
// func Test_Utils_assertEqual(t *testing.T) {
// // TODO
// }
// func Test_Utils_setETag(t *testing.T) {
// // TODO
// }
func Test_Utils_getGroupPath(t *testing.T) {
res := getGroupPath("/v1", "/")
assertEqual(t, "/v1", res)
res = getGroupPath("/v1", "/")
assertEqual(t, "/v1", res)
res = getGroupPath("/", "/")
assertEqual(t, "/", res)
res = getGroupPath("/v1/api/", "/")
assertEqual(t, "/v1/api/", res)
}
func Test_Utils_getMIME(t *testing.T) {
res := getMIME(".json")
assertEqual(t, "application/json", res)
res = getMIME(".xml")
assertEqual(t, "application/xml", res)
res = getMIME("xml")
assertEqual(t, "application/xml", res)
res = getMIME("json")
assertEqual(t, "application/json", res)
}
// func Test_Utils_getArgument(t *testing.T) {
// // TODO
// }
// func Test_Utils_parseTokenList(t *testing.T) {
// // TODO
// }
func Test_Utils_getString(t *testing.T) {
res := getString([]byte("Hello, World!"))
assertEqual(t, "Hello, World!", res)
}
func Test_Utils_getStringImmutable(t *testing.T) {
res := getStringImmutable([]byte("Hello, World!"))
assertEqual(t, "Hello, World!", res)
}
func Test_Utils_getBytes(t *testing.T) {
res := getBytes("Hello, World!")
assertEqual(t, []byte("Hello, World!"), res)
}
func Test_Utils_getBytesImmutable(t *testing.T) {
res := getBytesImmutable("Hello, World!")
assertEqual(t, []byte("Hello, World!"), res)
}
func Test_Utils_methodINT(t *testing.T) {
res := methodINT[MethodGet]
assertEqual(t, 0, res)
res = methodINT[MethodHead]
assertEqual(t, 1, res)
res = methodINT[MethodPost]
assertEqual(t, 2, res)
res = methodINT[MethodPut]
assertEqual(t, 3, res)
res = methodINT[MethodDelete]
assertEqual(t, 4, res)
res = methodINT[MethodConnect]
assertEqual(t, 5, res)
res = methodINT[MethodOptions]
assertEqual(t, 6, res)
res = methodINT[MethodTrace]
assertEqual(t, 7, res)
res = methodINT[MethodPatch]
assertEqual(t, 8, res)
}
func Test_Utils_statusMessage(t *testing.T) {
res := statusMessage[102]
assertEqual(t, "Processing", res)
res = statusMessage[303]
assertEqual(t, "See Other", res)
res = statusMessage[404]
assertEqual(t, "Not Found", res)
res = statusMessage[507]
assertEqual(t, "Insufficient Storage", res)
}
func Test_Utils_extensionMIME(t *testing.T) {
res := extensionMIME[".html"]
assertEqual(t, "text/html", res)
res = extensionMIME["html"]
assertEqual(t, "text/html", res)
res = extensionMIME[".msp"]
assertEqual(t, "application/octet-stream", res)
res = extensionMIME["msp"]
assertEqual(t, "application/octet-stream", res)
}
// func Test_Utils_getParams(t *testing.T) {
// // TODO
// }
func Test_Utils_matchParams(t *testing.T) {
type testparams struct {
url string
params []string
match bool
}
testCase := func(r string, cases []testparams) {
parser := getParams(r)
for _, c := range cases {
params, match := parser.getMatch(c.url, false)
assertEqual(t, c.params, params, fmt.Sprintf("route: '%s', url: '%s'", r, c.url))
assertEqual(t, c.match, match, fmt.Sprintf("route: '%s', url: '%s'", r, c.url))
}
}
testCase("/api/v1/:param/*", []testparams{
{url: "/api/v1/entity", params: []string{"entity", ""}, match: true},
{url: "/api/v1/entity/", params: []string{"entity", ""}, match: true},
{url: "/api/v1/entity/1", params: []string{"entity", "1"}, match: true},
{url: "/api/v", params: nil, match: false},
{url: "/api/v2", params: nil, match: false},
{url: "/api/v1/", params: nil, match: false},
})
testCase("/api/v1/:param?", []testparams{
{url: "/api/v1", params: []string{""}, match: true},
{url: "/api/v1/", params: []string{""}, match: true},
{url: "/api/v1/optional", params: []string{"optional"}, match: true},
{url: "/api/v", params: nil, match: false},
{url: "/api/v2", params: nil, match: false},
{url: "/api/xyz", params: nil, match: false},
})
testCase("/api/v1/*", []testparams{
{url: "/api/v1", params: []string{""}, match: true},
{url: "/api/v1/", params: []string{""}, match: true},
{url: "/api/v1/entity", params: []string{"entity"}, match: true},
{url: "/api/v1/entity/1/2", params: []string{"entity/1/2"}, match: true},
{url: "/api/v", params: nil, match: false},
{url: "/api/v2", params: nil, match: false},
{url: "/api/abc", params: nil, match: false},
})
testCase("/api/v1/:param", []testparams{
{url: "/api/v1/entity", params: []string{"entity"}, match: true},
{url: "/api/v1/entity/8728382", params: nil, match: false},
{url: "/api/v1", params: nil, match: false},
{url: "/api/v1/", params: nil, match: false},
})
testCase("/api/v1/const", []testparams{
{url: "/api/v1/const", params: []string{}, match: true},
{url: "/api/v1", params: nil, match: false},
{url: "/api/v1/", params: nil, match: false},
{url: "/api/v1/something", params: nil, match: false},
})
testCase("/api/v1/:param/abc/*", []testparams{
{url: "/api/v1/well/abc/wildcard", params: []string{"well", "wildcard"}, match: true},
{url: "/api/v1/well/abc/", params: []string{"well", ""}, match: true},
{url: "/api/v1/well/abc", params: []string{"well", ""}, match: true},
{url: "/api/v1/well/ttt", params: nil, match: false},
})
testCase("/api/:day/:month?/:year?", []testparams{
{url: "/api/1", params: []string{"1", "", ""}, match: true},
{url: "/api/1/", params: []string{"1", "", ""}, match: true},
{url: "/api/1/2", params: []string{"1", "2", ""}, match: true},
{url: "/api/1/2/3", params: []string{"1", "2", "3"}, match: true},
{url: "/api/", params: nil, match: false},
})
testCase("/api/*", []testparams{
{url: "/api/", params: []string{""}, match: true},
{url: "/api/joker", params: []string{"joker"}, match: true},
{url: "/api", params: []string{""}, match: true},
{url: "/api/v1/entity", params: []string{"v1/entity"}, match: true},
{url: "/api2/v1/entity", params: nil, match: false},
{url: "/api_ignore/v1/entity", params: nil, match: false},
})
testCase("/api/*/:param?", []testparams{
{url: "/api/", params: []string{"", ""}, match: true},
{url: "/api/joker", params: []string{"joker", ""}, match: true},
{url: "/api/joker/batman", params: []string{"joker", "batman"}, match: true},
{url: "/api/joker/batman/robin", params: []string{"joker/batman", "robin"}, match: true},
{url: "/api/joker/batman/robin/1", params: []string{"joker/batman/robin", "1"}, match: true},
{url: "/api", params: []string{"", ""}, match: true},
})
testCase("/api/*/:param", []testparams{
{url: "/api/test/abc", params: []string{"test", "abc"}, match: true},
{url: "/api/joker/batman", params: []string{"joker", "batman"}, match: true},
{url: "/api/joker/batman/robin", params: []string{"joker/batman", "robin"}, match: true},
{url: "/api/joker/batman/robin/1", params: []string{"joker/batman/robin", "1"}, match: true},
{url: "/api", params: nil, match: false},
})
testCase("/api/*/:param/:param2", []testparams{
{url: "/api/test/abc", params: nil, match: false},
{url: "/api/joker/batman", params: nil, match: false},
{url: "/api/joker/batman/robin", params: []string{"joker", "batman", "robin"}, match: true},
{url: "/api/joker/batman/robin/1", params: []string{"joker/batman", "robin", "1"}, match: true},
{url: "/api/joker/batman/robin/1/2", params: []string{"joker/batman/robin", "1", "2"}, match: true},
{url: "/api", params: nil, match: false},
})
testCase("/", []testparams{
{url: "/api", params: nil, match: false},
{url: "", params: []string{}, match: true},
{url: "/", params: []string{}, match: true},
})
testCase("/config/abc.json", []testparams{
{url: "/config/abc.json", params: []string{}, match: true},
{url: "config/abc.json", params: nil, match: false},
{url: "/config/efg.json", params: nil, match: false},
{url: "/config", params: nil, match: false},
})
testCase("/config/*.json", []testparams{
{url: "/config/abc.json", params: []string{"abc.json"}, match: true},
{url: "/config/efg.json", params: []string{"efg.json"}, match: true},
//{url: "/config/efg.csv", params: nil, match: false},// doesn`t work, current: params: "efg.csv", true
{url: "config/abc.json", params: nil, match: false},
{url: "/config", params: nil, match: false},
})
testCase("/xyz", []testparams{
{url: "xyz", params: nil, match: false},
{url: "xyz/", params: nil, match: false},
})
}
// func Test_Utils_getTrimmedParam(t *testing.T) {
// // TODO
// }
// func Test_Utils_getCharPos(t *testing.T) {
// // TODO
// }