mirror of https://github.com/gofiber/fiber.git
middleware/adaptor: allow to convert fiber.Ctx to (net/http).Request (#2461)
parent
eced39c47e
commit
c56b4e66a0
|
@ -14,7 +14,8 @@ Converter for net/http handlers to/from Fiber request handlers, special thanks t
|
|||
| FiberHandler | `FiberHandler(h fiber.Handler) http.Handler` | fiber.Handler -> http.Handler
|
||||
| FiberHandlerFunc | `FiberHandlerFunc(h fiber.Handler) http.HandlerFunc` | fiber.Handler -> http.HandlerFunc
|
||||
| FiberApp | `FiberApp(app *fiber.App) http.HandlerFunc` | Fiber app -> http.HandlerFunc
|
||||
| CopyContextToFiberContex | `CopyContextToFiberContext(context interface{}, requestContext *fasthttp.RequestCtx)` | context.Context -> fasthttp.RequestCtx
|
||||
| ConvertRequest | `ConvertRequest(c *fiber.Ctx, forServer bool) (*http.Request, error)` | fiber.Ctx -> http.Request
|
||||
| CopyContextToFiberContext | `CopyContextToFiberContext(context interface{}, requestContext *fasthttp.RequestCtx)` | context.Context -> fasthttp.RequestCtx
|
||||
|
||||
## Examples
|
||||
|
||||
|
@ -26,8 +27,8 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/v2/middleware/adaptor"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/adaptor"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -61,8 +62,8 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/v2/middleware/adaptor"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/adaptor"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -91,8 +92,8 @@ package main
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/v2/middleware/adaptor"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/adaptor"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -116,10 +117,12 @@ func greet(c *fiber.Ctx) error {
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/gofiber/v2/middleware/adaptor"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/adaptor"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := fiber.New()
|
||||
|
||||
|
@ -133,3 +136,33 @@ func greet(c *fiber.Ctx) error {
|
|||
return c.SendString("Hello World!")
|
||||
}
|
||||
```
|
||||
|
||||
### Fiber Context to (net/http).Request
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/adaptor"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := fiber.New()
|
||||
|
||||
app.Get("/greet", greetWithHTTPReq)
|
||||
|
||||
// Listen on port 3000
|
||||
http.ListenAndServe(":3000", adaptor.FiberApp(app))
|
||||
}
|
||||
|
||||
func greetWithHTTPReq(c *fiber.Ctx) error {
|
||||
httpReq, err := adaptor.ConvertRequest(c, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.SendString("Request URL: " + httpReq.URL.String())
|
||||
}
|
||||
```
|
||||
|
|
|
@ -27,6 +27,16 @@ func HTTPHandler(h http.Handler) fiber.Handler {
|
|||
}
|
||||
}
|
||||
|
||||
// ConvertRequest converts a fiber.Ctx to an http.Request.
|
||||
// forServer should be set to true when the http.Request is going to be passed to a http.Handler.
|
||||
func ConvertRequest(c *fiber.Ctx, forServer bool) (*http.Request, error) {
|
||||
var req http.Request
|
||||
if err := fasthttpadaptor.ConvertRequest(c.Context(), &req, forServer); err != nil {
|
||||
return nil, err //nolint:wrapcheck // This must not be wrapped
|
||||
}
|
||||
return &req, nil
|
||||
}
|
||||
|
||||
// CopyContextToFiberContext copies the values of context.Context to a fasthttp.RequestCtx
|
||||
func CopyContextToFiberContext(context interface{}, requestContext *fasthttp.RequestCtx) {
|
||||
contextValues := reflect.ValueOf(context).Elem()
|
||||
|
|
|
@ -7,11 +7,13 @@ import (
|
|||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/utils"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
|
@ -460,3 +462,26 @@ func (w *netHTTPResponseWriter) Write(p []byte) (int, error) {
|
|||
w.body = append(w.body, p...)
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func Test_ConvertRequest(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
app := fiber.New()
|
||||
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
httpReq, err := ConvertRequest(c, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.SendString("Request URL: " + httpReq.URL.String())
|
||||
})
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/test?hello=world&another=test", http.NoBody))
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, http.StatusOK, resp.StatusCode, "Status code")
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, "Request URL: /test?hello=world&another=test", string(body))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue