Skip to content

Commit 7f5fe4d

Browse files
committed
Extended redirectTo to accept a boolean flag to distinguish between 301 and 302 redirects.
1 parent 8cb723d commit 7f5fe4d

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -796,15 +796,16 @@ let app =
796796

797797
### redirectTo
798798

799-
`redirectTo` uses a 302 response code to redirect the client to the specified path.
799+
`redirectTo` uses a 302 or 301 (when permanent) HTTP response code to redirect the client to the specified location. It takes in two parameters, the location to redirect to and a boolean flag denoting whether the redirect should be permanent or not.
800800

801801
#### Example:
802802

803803
```fsharp
804804
let app =
805805
choose [
806-
route "/" >=> redirectTo "/foo"
807-
route "/foo" >=> text "Some string"
806+
route "/" >=> redirectTo "/foo" false
807+
route "/example" >=> redirectTo "http://example.org" true
808+
route "/foo" >=> text "Some string"
808809
]
809810
```
810811

src/Giraffe/Giraffe.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<AssemblyName>Giraffe</AssemblyName>
5-
<VersionPrefix>0.1.0-alpha013</VersionPrefix>
5+
<VersionPrefix>0.1.0-alpha014</VersionPrefix>
66
<Description>A native functional ASP.NET Core web framework for F# developers.</Description>
77
<Copyright>Copyright 2017 Dustin Moris Gorski</Copyright>
88
<NeutralLanguage>en-GB</NeutralLanguage>

src/Giraffe/HttpHandlers.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ let negotiate (responseObj : obj) =
451451
// response object
452452
responseObj
453453

454-
///Redirect to a different location with a 302 HTTP status code.
455-
let redirectTo route =
454+
///Redirects to a different location with a 302 or 301 (when permanent) HTTP status code.
455+
let redirectTo (location : string) (permanent : bool) =
456456
fun (ctx:HttpHandlerContext) ->
457-
ctx.HttpContext.Response.Redirect route
458-
ctx |> Some |> async.Return
457+
ctx.HttpContext.Response.Redirect(location, permanent)
458+
ctx |> Some |> async.Return

tests/Giraffe.Tests/HttpHandlerTests.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,8 +1242,8 @@ let ``GET "/redirect" redirect to "/" `` () =
12421242
let ctx, hctx = initNewContext()
12431243
let app =
12441244
GET >=> choose [
1245-
route "/" >=> text "Hello World"
1246-
route "/redirect" >=> redirectTo "/"
1245+
route "/" >=> text "Hello World"
1246+
route "/redirect" >=> redirectTo "/" false
12471247
setStatusCode 404 >=> text "Not found" ]
12481248

12491249
ctx.Request.Method.ReturnsForAnyArgs "GET" |> ignore
@@ -1256,16 +1256,16 @@ let ``GET "/redirect" redirect to "/" `` () =
12561256

12571257
match result with
12581258
| None -> assertFail "It was expected that the request would be redirected"
1259-
| Some ctx -> ctx.HttpContext.Response.Received().Redirect("/")
1259+
| Some ctx -> ctx.HttpContext.Response.Received().Redirect("/", false)
12601260

12611261

12621262
[<Fact>]
12631263
let ``POST "/redirect" redirect to "/" `` () =
12641264
let ctx, hctx = initNewContext()
12651265
let app =
12661266
POST >=> choose [
1267-
route "/" >=> text "Hello World"
1268-
route "/redirect" >=> redirectTo "/"
1267+
route "/" >=> text "Hello World"
1268+
route "/redirect" >=> redirectTo "/" true
12691269
setStatusCode 404 >=> text "Not found" ]
12701270

12711271
ctx.Request.Method.ReturnsForAnyArgs "POST" |> ignore
@@ -1278,4 +1278,4 @@ let ``POST "/redirect" redirect to "/" `` () =
12781278

12791279
match result with
12801280
| None -> assertFail "It was expected that the request would be redirected"
1281-
| Some ctx -> ctx.HttpContext.Response.Received().Redirect("/")
1281+
| Some ctx -> ctx.HttpContext.Response.Received().Redirect("/", true)

0 commit comments

Comments
 (0)