@@ -2,32 +2,43 @@ module ControllerTests
22
33open Expecto
44open Saturn
5- open Giraffe
65open Giraffe.GiraffeViewEngine
76open FSharp.Control .Tasks .V2 .ContextInsensitive
87open Microsoft.Extensions .Primitives
98open Microsoft.AspNetCore .Http
109open System
1110
12- let createAction =
13- fun ctx ->
14- " Create" |> Controller.text ctx
15-
16- let updateAction =
17- fun ctx id -> ( sprintf " Update %i " id) |> Controller.text ctx
11+ let createAction id ctx =
12+ match id with
13+ | Some id -> sprintf " Create %i " id
14+ | None -> " Create"
15+ |> Controller.text ctx
16+
17+ let updateAction id ctx subId =
18+ match id with
19+ | Some id -> sprintf " Update %i %i " id subId
20+ | None -> sprintf " Update %i " subId
21+ |> Controller.text ctx
22+
23+ let testSubController ( id : int ) = controller {
24+ create ( createAction ( Some id))
25+ update ( updateAction ( Some id))
26+ }
1827
1928let testController = controller {
20- create createAction
21- update updateAction
29+ subController " /sub" testSubController
30+
31+ create ( createAction None)
32+ update ( updateAction None)
2233}
2334
2435let basicTemplate =
25- html [] [
26- head [] []
27- body [] [
28- h1 [] [ encodedText " Hello, world!" ]
29- ]
30- ]
36+ html [] [
37+ head [] []
38+ body [] [
39+ h1 [] [ encodedText " Hello, world!" ]
40+ ]
41+ ]
3142
3243let implicitNodeToHtmlTestController = controller {
3344 index ( fun _ -> task { return basicTemplate })
@@ -38,44 +49,42 @@ let explicitNodeToHtmlTestController = controller {
3849}
3950
4051let implicitStringToHtmlTestController = controller {
41- index ( fun _ -> task { return GiraffeViewEngine. renderHtmlNode basicTemplate})
52+ index ( fun _ -> task { return renderHtmlNode basicTemplate})
4253}
4354
55+ let responseTestCase = responseTestCase testController
56+
4457[<Tests>]
4558let tests =
4659 testList " Controller Tests" [
47- testCase " create works" <| fun _ ->
48- let ctx = getEmptyContext " POST" " "
49- let expected = " Create"
60+ testCase " subController Update works" <|
61+ responseTestCase " PUT" " /1/sub/2" " Update 1 2"
5062
51- try
52- let result = testController next ctx |> runTask
53- match result with
54- | None -> failtestf " Result was expected to be %s , but was %A " expected result
55- | Some ctx ->
56- Expect.equal ( getBody ctx) expected " Result should be equal"
57- with ex -> failtestf " failed because %A " ex
63+ testCase " subController Create trailing slash works" <|
64+ responseTestCase " POST" " /1/sub/" " Create 1"
5865
59- testCase " update works" <| fun _ ->
60- let ctx = getEmptyContext " PUT" " /1"
61- let expected = " Update 1"
66+ testCase " subController Create no trailing slash works" <|
67+ responseTestCase " POST" " /1/sub" " Create 1"
6268
63- try
64- let result = testController next ctx |> runTask
65- match result with
66- | None -> failtestf " Result was expected to be %s , but was %A " expected result
67- | Some ctx ->
68- Expect.equal ( getBody ctx) expected " Result should be equal"
69+ testCase " Create trailing slash works" <|
70+ responseTestCase " POST" " /" " Create"
6971
70- with ex -> failtestf " failed because %A " ex
72+ testCase " Create no trailing slash works" <|
73+ responseTestCase " POST" " " " Create"
74+
75+ testCase " Update POST works" <|
76+ responseTestCase " POST" " /1" " Update 1"
77+
78+ testCase " Update PUT works" <|
79+ responseTestCase " PUT" " /1" " Update 1"
7180
7281 testCase " deleteAll works" <| fun _ ->
7382 let expectedStatusCode = 204
7483 let expectedString = " deleted"
7584 let mutable plugged = " "
7685 let deleteAll = fun ( ctx : HttpContext ) ->
7786 task {
78- do ctx.SetStatusCode 204
87+ ctx.Response.StatusCode <- 204
7988 return ( Some ctx)
8089 }
8190 let deleteController = controller {
@@ -98,7 +107,7 @@ let tests =
98107 let mutable plugged = " "
99108 let deleteAll = fun ( ctx : HttpContext ) ->
100109 task {
101- do ctx.SetStatusCode 204
110+ ctx.Response.StatusCode <- 204
102111 return ( Some ctx)
103112 }
104113 let deleteController = controller {
@@ -118,33 +127,27 @@ let tests =
118127 testCase " plugs should only fire once" <| fun _ ->
119128 let deleteAll = fun ( ctx : HttpContext ) ->
120129 task {
121- do ctx.SetStatusCode 204
130+ ctx.Response.StatusCode <- 204
122131 return ( Some ctx)
123132 }
124133 let mutable count = 0
125134 let controllerWithPlugs =
126135 controller {
127- create createAction
128- update updateAction
136+ create ( createAction None )
137+ update ( updateAction None )
129138 delete_ all deleteAll
130139 plug [ All] ( fun next ctx -> count <- count + 1 ; next ctx)
131140 }
132141 try
133142 let postEmpty = getEmptyContext " POST" " " |> controllerWithPlugs next |> runTask
134143 Expect.equal count 1 " Count should be 1"
135- match postEmpty with
136- | None -> failtestf " Result was expected to be %s , but was %A " " Create" postEmpty
137- | Some ctx ->
138- Expect.equal ( getBody ctx) " Create" " Result should be equal"
144+ expectResponse " Create" postEmpty
139145 getEmptyContext " POST" " /" |> controllerWithPlugs next |> runTask |> ignore
140146 Expect.equal count 2 " Count should be 2"
141147 getEmptyContext " POST" " /1" |> controllerWithPlugs next |> runTask |> ignore
142148 Expect.equal count 3 " Count should be 3"
143149 let putResult = getEmptyContext " PUT" " /1" |> controllerWithPlugs next |> runTask
144- match putResult with
145- | None -> failtestf " Result was expected to be %s , but was %A " " Create" postEmpty
146- | Some ctx ->
147- Expect.equal ( getBody ctx) " Update 1" " Result should be equal"
150+ expectResponse " Update 1" putResult
148151 Expect.equal count 4 " Count should be 4"
149152 let deleteAllResult = getEmptyContext " DELETE" " " |> controllerWithPlugs next |> runTask
150153 match deleteAllResult with
0 commit comments