Merge remote-tracking branch 'upstream/master'

pull/885/head
Fenny 2020-10-05 20:04:40 +02:00
commit 898d210ea2
10 changed files with 93 additions and 46 deletions

3
.github/README.md vendored
View File

@ -606,6 +606,3 @@ Copyright (c) 2019-present [Fenny](https://github.com/fenny) and [Contributors](
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)

2
app.go
View File

@ -752,7 +752,7 @@ func (app *App) startupMessage(addr string, tls bool, pids string) {
if os.Getenv("TERM") == "dumb" || (!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd())) {
out = colorable.NewNonColorable(os.Stdout)
}
fmt.Fprintf(out, logo,
_, _ = fmt.Fprintf(out, logo,
cBlack,
centerValue(" Fiber v"+Version, 49),
center(addr, 49),

View File

@ -121,7 +121,7 @@ func Test_App_ServerErrorHandler_SmallReadBuffer(t *testing.T) {
})
request := httptest.NewRequest("GET", "/", nil)
logHeaderSlice := make([]string, 5000, 5000)
logHeaderSlice := make([]string, 5000)
request.Header.Set("Very-Long-Header", strings.Join(logHeaderSlice, "-"))
_, err := app.Test(request)
@ -736,6 +736,22 @@ func Test_App_Group_Invalid(t *testing.T) {
New().Group("/").Use(1)
}
// go test -run Test_App_Group_Mount
func Test_App_Group_Mount(t *testing.T) {
micro := New()
micro.Get("/doe", func(c *Ctx) error {
return c.SendStatus(StatusOK)
})
app := New()
v1 := app.Group("/v1")
v1.Mount("/john", micro)
resp, err := app.Test(httptest.NewRequest("GET", "/v1/john/doe", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
}
func Test_App_Group(t *testing.T) {
var dummyHandler = testEmptyHandler
@ -1071,3 +1087,9 @@ func Test_App_Master_Process_Show_Startup_Message(t *testing.T) {
New(Config{Prefork: true}).
startupMessage(":3000", true, "")
}
func Test_App_Server(t *testing.T) {
app := New()
utils.AssertEqual(t, false, app.Server() == nil)
}

2
ctx.go
View File

@ -708,7 +708,7 @@ func (c *Ctx) QueryParser(out interface{}) error {
c.fasthttp.QueryArgs().VisitAll(func(key []byte, val []byte) {
k := utils.UnsafeString(key)
v := utils.UnsafeString(val)
if strings.Index(v, ",") > -1 && equalFieldType(out, reflect.Slice, k) {
if strings.Contains(v, ",") && equalFieldType(out, reflect.Slice, k) {
values := strings.Split(v, ",")
for i := 0; i < len(values); i++ {
data[k] = append(data[k], values[i])

View File

@ -746,6 +746,15 @@ func Test_Ctx_IP(t *testing.T) {
utils.AssertEqual(t, "0.0.0.0", c.IP())
}
// go test -run Test_Ctx_IP_ProxyHeader
func Test_Ctx_IP_ProxyHeader(t *testing.T) {
t.Parallel()
app := New(Config{ProxyHeader: "Real-Ip"})
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
utils.AssertEqual(t, "", c.IP())
}
// go test -run Test_Ctx_IPs -parallel
func Test_Ctx_IPs(t *testing.T) {
t.Parallel()
@ -1317,7 +1326,7 @@ func Test_Ctx_SendFile_Immutable(t *testing.T) {
if err := c.SendFile("./.github/" + file + ".html"); err != nil {
utils.AssertEqual(t, nil, err)
}
utils.AssertEqual(t, "index", fmt.Sprintf("%s", file))
utils.AssertEqual(t, "index", file)
return c.SendString(file)
})
// 1st try
@ -1852,6 +1861,17 @@ func Benchmark_Ctx_Write(b *testing.B) {
}
}
// go test -run Test_Ctx_WriteString
func Test_Ctx_WriteString(t *testing.T) {
t.Parallel()
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.WriteString("Hello, ")
c.WriteString("World!")
utils.AssertEqual(t, "Hello, World!", string(c.Response().Body()))
}
// go test -run Test_Ctx_XHR
func Test_Ctx_XHR(t *testing.T) {
t.Parallel()
@ -2030,3 +2050,13 @@ func Benchmark_Ctx_BodyStreamWriter(b *testing.B) {
})
}
}
func Test_Ctx_String(t *testing.T) {
t.Parallel()
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
utils.AssertEqual(t, "#0000000000000000 - 0.0.0.0:0 <-> 0.0.0.0:0 - GET http:///", c.String())
}

View File

@ -23,7 +23,7 @@ func (grp *Group) Mount(prefix string, fiber *App) Router {
for m := range stack {
for r := range stack[m] {
route := grp.app.copyRoute(stack[m][r])
grp.app.addRoute(route.Method, grp.app.addPrefixToRoute(prefix, route))
grp.app.addRoute(route.Method, grp.app.addPrefixToRoute(getGroupPath(grp.prefix, prefix), route))
}
}
return grp

View File

@ -307,10 +307,6 @@ func isEtagStale(etag string, noneMatchBytes []byte) bool {
return !matchEtag(getString(noneMatchBytes[start:end]), etag)
}
func isIPv6(address string) bool {
return strings.Count(address, ":") >= 2
}
func parseAddr(raw string) (host, port string) {
if i := strings.LastIndex(raw, ":"); i != -1 {
return raw[:i], raw[i+1:]

View File

@ -209,30 +209,6 @@ func Benchmark_Utils_Unescape(b *testing.B) {
utils.AssertEqual(b, "/créer", unescaped)
}
func Test_Utils_IPv6(t *testing.T) {
testCases := []struct {
string
bool
}{
{"::FFFF:C0A8:1:3000", true},
{"::FFFF:C0A8:0001:3000", true},
{"0000:0000:0000:0000:0000:FFFF:C0A8:1:3000", true},
{"::FFFF:C0A8:1%1:3000", true},
{"::FFFF:192.168.0.1:3000", true},
{"[::FFFF:C0A8:1]:3000", true},
{"[::FFFF:C0A8:1%1]:3000", true},
{":3000", false},
{"127.0.0.1:3000", false},
{"127.0.0.1:", false},
{"0.0.0.0:3000", false},
{"", false},
}
for _, c := range testCases {
utils.AssertEqual(t, c.bool, isIPv6(c.string))
}
}
func Test_Utils_Parse_Address(t *testing.T) {
testCases := []struct {
addr, host, port string

View File

@ -152,5 +152,30 @@ func Test_Cache_Invalid_Method(t *testing.T) {
body, err = ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "123", string(body))
}
func Test_Cache_NothingToCache(t *testing.T) {
app := fiber.New()
app.Use(New(Config{Expiration: -(time.Second * 1)}))
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString(time.Now().String())
})
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err)
body, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
time.Sleep(500 * time.Millisecond)
respCached, err := app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err)
bodyCached, err := ioutil.ReadAll(respCached.Body)
utils.AssertEqual(t, nil, err)
if bytes.Equal(body, bodyCached) {
t.Errorf("Cache should have expired: %s, %s", body, bodyCached)
}
}

View File

@ -72,18 +72,19 @@ func New(config ...Config) fiber.Handler {
if cfg.ContextKey == "" {
cfg.ContextKey = ConfigDefault.ContextKey
}
if cfg.Cookie == nil {
cfg.Cookie = ConfigDefault.Cookie
if cfg.Cookie.Name == "" {
cfg.Cookie.Name = "_csrf"
}
if cfg.Cookie.SameSite == "" {
cfg.Cookie.SameSite = "Strict"
}
}
if cfg.CookieExpires == 0 {
cfg.CookieExpires = ConfigDefault.CookieExpires
}
if cfg.Cookie != nil {
if cfg.Cookie.Name == "" {
cfg.Cookie.Name = ConfigDefault.Cookie.Name
}
if cfg.Cookie.SameSite == "" {
cfg.Cookie.SameSite = ConfigDefault.Cookie.SameSite
}
} else {
cfg.Cookie = ConfigDefault.Cookie
}
}
// Generate the correct extractor to get the token from the correct location