Skip to content

Commit

Permalink
middleware/adaptor: allow to convert fiber.Ctx to (net/http).Request (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
leonklingele committed May 15, 2023
1 parent eced39c commit c56b4e6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
45 changes: 39 additions & 6 deletions docs/api/middleware/adaptor.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -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()

Expand All @@ -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())
}
```
10 changes: 10 additions & 0 deletions middleware/adaptor/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
25 changes: 25 additions & 0 deletions middleware/adaptor/adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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))
}

0 comments on commit c56b4e6

Please sign in to comment.