-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
226 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
module Giraffe.Tests.RoutingTests | ||
|
||
open System.IO | ||
open System.Collections.Generic | ||
open Microsoft.AspNetCore.Http | ||
open Xunit | ||
open NSubstitute | ||
open Giraffe | ||
|
||
// --------------------------------- | ||
// subRoutef Tests | ||
// --------------------------------- | ||
|
||
[<Fact>] | ||
let ``subRoutef: GET "/" returns "Not found"`` () = | ||
let ctx = Substitute.For<HttpContext>() | ||
let app = | ||
GET >=> choose [ | ||
subRoutef "/%s/%i" (fun (lang, version) -> | ||
choose [ | ||
route "/foo" >=> text "bar" | ||
routef "/%s" (fun name -> text (sprintf "Hello %s! Lang: %s, Version: %i" name lang version)) | ||
]) | ||
route "/bar" >=> text "foo" | ||
setStatusCode 404 >=> text "Not found" ] | ||
|
||
ctx.Items.Returns (new Dictionary<obj,obj>() :> IDictionary<obj,obj>) |> ignore | ||
ctx.Request.Method.ReturnsForAnyArgs "GET" |> ignore | ||
ctx.Request.Path.ReturnsForAnyArgs (PathString("/")) |> ignore | ||
ctx.Response.Body <- new MemoryStream() | ||
let expected = "Not found" | ||
|
||
task { | ||
let! result = app next ctx | ||
|
||
match result with | ||
| None -> assertFailf "Result was expected to be %s" expected | ||
| Some ctx -> Assert.Equal(expected, getBody ctx) | ||
} | ||
|
||
[<Fact>] | ||
let ``subRoutef: GET "/bar" returns "foo"`` () = | ||
let ctx = Substitute.For<HttpContext>() | ||
let app = | ||
GET >=> choose [ | ||
subRoutef "/%s/%i" (fun (lang, version) -> | ||
choose [ | ||
route "/foo" >=> text "bar" | ||
routef "/%s" (fun name -> text (sprintf "Hello %s! Lang: %s, Version: %i" name lang version)) | ||
]) | ||
route "/bar" >=> text "foo" | ||
setStatusCode 404 >=> text "Not found" ] | ||
|
||
ctx.Items.Returns (new Dictionary<obj,obj>() :> IDictionary<obj,obj>) |> ignore | ||
ctx.Request.Method.ReturnsForAnyArgs "GET" |> ignore | ||
ctx.Request.Path.ReturnsForAnyArgs (PathString("/bar")) |> ignore | ||
ctx.Response.Body <- new MemoryStream() | ||
let expected = "foo" | ||
|
||
task { | ||
let! result = app next ctx | ||
|
||
match result with | ||
| None -> assertFailf "Result was expected to be %s" expected | ||
| Some ctx -> Assert.Equal(expected, getBody ctx) | ||
} | ||
|
||
[<Fact>] | ||
let ``subRoutef: GET "/John/5/foo" returns "bar"`` () = | ||
let ctx = Substitute.For<HttpContext>() | ||
let app = | ||
GET >=> choose [ | ||
subRoutef "/%s/%i" (fun (lang, version) -> | ||
choose [ | ||
route "/foo" >=> text "bar" | ||
routef "/%s" (fun name -> text (sprintf "Hello %s! Lang: %s, Version: %i" name lang version)) | ||
]) | ||
route "/bar" >=> text "foo" | ||
setStatusCode 404 >=> text "Not found" ] | ||
|
||
ctx.Items.Returns (new Dictionary<obj,obj>() :> IDictionary<obj,obj>) |> ignore | ||
ctx.Request.Method.ReturnsForAnyArgs "GET" |> ignore | ||
ctx.Request.Path.ReturnsForAnyArgs (PathString("/John/5/foo")) |> ignore | ||
ctx.Response.Body <- new MemoryStream() | ||
let expected = "bar" | ||
|
||
task { | ||
let! result = app next ctx | ||
|
||
match result with | ||
| None -> assertFailf "Result was expected to be %s" expected | ||
| Some ctx -> Assert.Equal(expected, getBody ctx) | ||
} | ||
|
||
[<Fact>] | ||
let ``subRoutef: GET "/en/10/Julia" returns "Hello Julia! Lang: en, Version: 10"`` () = | ||
let ctx = Substitute.For<HttpContext>() | ||
let app = | ||
GET >=> choose [ | ||
subRoutef "/%s/%i" (fun (lang, version) -> | ||
choose [ | ||
route "/foo" >=> text "bar" | ||
routef "/%s" (fun name -> text (sprintf "Hello %s! Lang: %s, Version: %i" name lang version)) | ||
]) | ||
route "/bar" >=> text "foo" | ||
setStatusCode 404 >=> text "Not found" ] | ||
|
||
ctx.Items.Returns (new Dictionary<obj,obj>() :> IDictionary<obj,obj>) |> ignore | ||
ctx.Request.Method.ReturnsForAnyArgs "GET" |> ignore | ||
ctx.Request.Path.ReturnsForAnyArgs (PathString("/en/10/Julia")) |> ignore | ||
ctx.Response.Body <- new MemoryStream() | ||
let expected = "Hello Julia! Lang: en, Version: 10" | ||
|
||
task { | ||
let! result = app next ctx | ||
|
||
match result with | ||
| None -> assertFailf "Result was expected to be %s" expected | ||
| Some ctx -> Assert.Equal(expected, getBody ctx) | ||
} | ||
|
||
[<Fact>] | ||
let ``subRoutef: GET "/en/10/api/Julia" returns "Hello Julia! Lang: en, Version: 10"`` () = | ||
let ctx = Substitute.For<HttpContext>() | ||
let app = | ||
GET >=> choose [ | ||
subRoutef "/%s/%i/api" (fun (lang, version) -> | ||
choose [ | ||
route "/foo" >=> text "bar" | ||
routef "/%s" (fun name -> text (sprintf "Hello %s! Lang: %s, Version: %i" name lang version)) | ||
]) | ||
route "/bar" >=> text "foo" | ||
setStatusCode 404 >=> text "Not found" ] | ||
|
||
ctx.Items.Returns (new Dictionary<obj,obj>() :> IDictionary<obj,obj>) |> ignore | ||
ctx.Request.Method.ReturnsForAnyArgs "GET" |> ignore | ||
ctx.Request.Path.ReturnsForAnyArgs (PathString("/en/10/api/Julia")) |> ignore | ||
ctx.Response.Body <- new MemoryStream() | ||
let expected = "Hello Julia! Lang: en, Version: 10" | ||
|
||
task { | ||
let! result = app next ctx | ||
|
||
match result with | ||
| None -> assertFailf "Result was expected to be %s" expected | ||
| Some ctx -> Assert.Equal(expected, getBody ctx) | ||
} |