Skip to content

Commit 1bf136d

Browse files
committed
Merge branch 'develop' of https://github.com/dustinmoris/Giraffe into develop
2 parents 25f5e04 + 383ea35 commit 1bf136d

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ The old NuGet package has been unlisted and will not receive any updates any mor
5555
- [xml](#xml)
5656
- [negotiate](#negotiate)
5757
- [negotiateWith](#negotiatewith)
58+
- [redirectTo](#redirectTo)
5859
- [htmlFile](#htmlfile)
5960
- [dotLiquid](#dotliquid)
6061
- [dotLiquidTemplate](#dotliquidtemplate)
@@ -622,6 +623,20 @@ let app =
622623
]
623624
```
624625

626+
### redirectTo
627+
628+
`redirectTo` uses a 302 response code to redirect the client to the specified path.
629+
630+
#### Example:
631+
632+
```fsharp
633+
let app =
634+
choose [
635+
route "/" >=> redirectTo "/foo"
636+
route "/foo" >=> text "Some string"
637+
]
638+
```
639+
625640
### htmlFile
626641

627642
`htmlFile` sets or modifies the body of the `HttpResponse` with the contents of a physical html file. This http handler triggers a response to the client and other http handlers will not be able to modify the HTTP headers afterwards any more.

src/Giraffe/HttpHandlers.fs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,4 +449,10 @@ let negotiate (responseObj : obj) =
449449
|> text)
450450
<| ctx)
451451
// response object
452-
responseObj
452+
responseObj
453+
454+
///Redirect to a different location with a 302 HTTP status code.
455+
let redirectTo route =
456+
fun (ctx:HttpHandlerContext) ->
457+
ctx.HttpContext.Response.Redirect route
458+
ctx |> Some |> async.Return

tests/Giraffe.Tests/HttpHandlerTests.fs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1235,4 +1235,47 @@ let ``Warbler function should execute inner function each time`` () =
12351235
|> Async.RunSynchronously
12361236
|> (fun res -> getBody res.Value)
12371237

1238-
Assert.False(result3.Equals result4)
1238+
Assert.False(result3.Equals result4)
1239+
1240+
[<Fact>]
1241+
let ``GET "/redirect" redirect to "/" `` () =
1242+
let ctx, hctx = initNewContext()
1243+
let app =
1244+
GET >=> choose [
1245+
route "/" >=> text "Hello World"
1246+
route "/redirect" >=> redirectTo "/"
1247+
setStatusCode 404 >=> text "Not found" ]
1248+
1249+
ctx.Request.Method.ReturnsForAnyArgs "GET" |> ignore
1250+
ctx.Request.Path.ReturnsForAnyArgs (PathString("/redirect")) |> ignore
1251+
1252+
let result =
1253+
hctx
1254+
|> app
1255+
|> Async.RunSynchronously
1256+
1257+
match result with
1258+
| None -> assertFail "It was expected that the request would be redirected"
1259+
| Some ctx -> ctx.HttpContext.Response.Received().Redirect("/")
1260+
1261+
1262+
[<Fact>]
1263+
let ``POST "/redirect" redirect to "/" `` () =
1264+
let ctx, hctx = initNewContext()
1265+
let app =
1266+
POST >=> choose [
1267+
route "/" >=> text "Hello World"
1268+
route "/redirect" >=> redirectTo "/"
1269+
setStatusCode 404 >=> text "Not found" ]
1270+
1271+
ctx.Request.Method.ReturnsForAnyArgs "POST" |> ignore
1272+
ctx.Request.Path.ReturnsForAnyArgs (PathString("/redirect")) |> ignore
1273+
1274+
let result =
1275+
hctx
1276+
|> app
1277+
|> Async.RunSynchronously
1278+
1279+
match result with
1280+
| None -> assertFail "It was expected that the request would be redirected"
1281+
| Some ctx -> ctx.HttpContext.Response.Received().Redirect("/")

0 commit comments

Comments
 (0)