Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Aug 6, 2022
1 parent 9c6c7da commit 9e85f0c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 30 deletions.
73 changes: 43 additions & 30 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package fiber
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"net"
Expand All @@ -22,8 +23,6 @@ import (
"sync/atomic"
"time"

"encoding/json"

"github.com/gofiber/fiber/v2/utils"
"github.com/valyala/fasthttp"
)
Expand Down Expand Up @@ -63,17 +62,18 @@ type Storage interface {

// ErrorHandler defines a function that will process all errors
// returned from any handlers in the stack
// cfg := fiber.Config{}
// cfg.ErrorHandler = func(c *Ctx, err error) error {
// code := StatusInternalServerError
// var e *fiber.Error
// if errors.As(err, &e) {
// code = e.Code
// }
// c.Set(HeaderContentType, MIMETextPlainCharsetUTF8)
// return c.Status(code).SendString(err.Error())
// }
// app := fiber.New(cfg)
//
// cfg := fiber.Config{}
// cfg.ErrorHandler = func(c *Ctx, err error) error {
// code := StatusInternalServerError
// var e *fiber.Error
// if errors.As(err, &e) {
// code = e.Code
// }
// c.Set(HeaderContentType, MIMETextPlainCharsetUTF8)
// return c.Status(code).SendString(err.Error())
// }
// app := fiber.New(cfg)
type ErrorHandler = func(*Ctx, error) error

// Error represents an error that occurred while handling a request.
Expand Down Expand Up @@ -438,12 +438,15 @@ var DefaultErrorHandler = func(c *Ctx, err error) error {
}

// New creates a new Fiber named instance.
// app := fiber.New()
//
// app := fiber.New()
//
// You can pass optional configuration options by passing a Config struct:
// app := fiber.New(fiber.Config{
// Prefork: true,
// ServerHeader: "Fiber",
// })
//
// app := fiber.New(fiber.Config{
// Prefork: true,
// ServerHeader: "Fiber",
// })
func New(config ...Config) *App {
// Create a new app
app := &App{
Expand Down Expand Up @@ -609,15 +612,15 @@ func (app *App) GetRoute(name string) Route {
// Use registers a middleware route that will match requests
// with the provided prefix (which is optional and defaults to "/").
//
// app.Use(func(c *fiber.Ctx) error {
// return c.Next()
// })
// app.Use("/api", func(c *fiber.Ctx) error {
// return c.Next()
// })
// app.Use("/api", handler, func(c *fiber.Ctx) error {
// return c.Next()
// })
// app.Use(func(c *fiber.Ctx) error {
// return c.Next()
// })
// app.Use("/api", func(c *fiber.Ctx) error {
// return c.Next()
// })
// app.Use("/api", handler, func(c *fiber.Ctx) error {
// return c.Next()
// })
//
// This method will match all HTTP verbs: GET, POST, PUT, HEAD etc...
func (app *App) Use(args ...interface{}) Router {
Expand Down Expand Up @@ -710,8 +713,9 @@ func (app *App) All(path string, handlers ...Handler) Router {
}

// Group is used for Routes with common prefix to define a new sub-router with optional middleware.
// api := app.Group("/api")
// api.Get("/users", handler)
//
// api := app.Group("/api")
// api.Get("/users", handler)
func (app *App) Group(prefix string, handlers ...Handler) Router {
if len(handlers) > 0 {
app.register(methodUse, prefix, handlers...)
Expand Down Expand Up @@ -845,7 +849,16 @@ func (app *App) Test(req *http.Request, msTimeout ...int) (resp *http.Response,
// Serve conn to server
channel := make(chan error)
go func() {
channel <- app.server.ServeConn(conn)
var returned bool
defer func() {
if !returned {
channel <- fmt.Errorf("runtime.Goexit() called in handler")
}
}()

r := app.server.ServeConn(conn)
returned = true
channel <- r
}()

// Wait for callback
Expand Down
33 changes: 33 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"net/http/httptest"
"reflect"
"regexp"
"runtime"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -1527,3 +1528,35 @@ func Test_App_UseMountedErrorHandlerForBestPrefixMatch(t *testing.T) {
utils.AssertEqual(t, nil, err, "iotuil.ReadAll()")
utils.AssertEqual(t, "hi, i'm a custom sub sub fiber error", string(b), "Third fiber Response body")
}

func Test_App_Test_no_timeout_infinitely(t *testing.T) {
start := time.Now()
var err error
c := make(chan bool)
go func() {
time.Sleep(5 * time.Second)
c <- true
}()
go func() {
defer close(c)
app := New()
app.Get("/", func(c *Ctx) error {
runtime.Goexit()
return nil
})

req := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
_, err = app.Test(req, -1)
}()

<-c

if time.Since(start) >= time.Second {
t.Error("hanging test")
t.FailNow()
}
if err == nil {
t.Error("unexpected success request")
t.FailNow()
}
}

0 comments on commit 9e85f0c

Please sign in to comment.