Skip to content

Commit 398d6cb

Browse files
authored
Merge pull request #59 from fossa-app/add-status-code-helpers
Add status code helpers and make ProblemDetails status non-nullable
2 parents 7139189 + 752136d commit 398d6cb

6 files changed

Lines changed: 96 additions & 5 deletions

File tree

src/Bridge/Bridge.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<Compile Include="Models\ApiModels\PayloadModels.fs" />
1414
<Compile Include="Services\Endpoints.fs" />
1515
<Compile Include="Services\UrlHelpers.fs" />
16+
<Compile Include="Services\StatusCodeHelpers.fs" />
1617
<Compile Include="Services\IHttpRequestSender.fs" />
1718
<Compile Include="Services\IJsonSerializer.fs" />
1819
<Compile Include="Services\JsonSerializer.fs" />

src/Bridge/Models/ApiModels/SharedModels.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ open System
66
type ProblemDetailsModel =
77
{ Type: string | null
88
Title: string | null
9-
Status: Nullable<int>
9+
Status: int
1010
Detail: string | null
1111
Instance: string | null }
1212

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Fossa.Bridge.Services.StatusCodeHelpers
2+
3+
open Fossa.Bridge.Models.ApiModels
4+
5+
let isStatusCodeClientError (statusCode: int) : bool = statusCode >= 400 && statusCode <= 499
6+
7+
let isStatusCodeServerError (statusCode: int) : bool = statusCode >= 500 && statusCode <= 599
8+
9+
let isClientProblem (problem: ProblemDetailsModel) : bool = isStatusCodeClientError problem.Status
10+
11+
let isServerProblem (problem: ProblemDetailsModel) : bool = isStatusCodeServerError problem.Status

tests/Bridge.Tests/Bridge.Tests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<ItemGroup>
88
<Compile Include="JsonSerializerTests.fs" />
99
<Compile Include="UrlHelpersTests.fs" />
10+
<Compile Include="StatusCodeHelpersTests.fs" />
1011
<Compile Include="Main.fs" />
1112
</ItemGroup>
1213
<ItemGroup>

tests/Bridge.Tests/JsonSerializerTests.fs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ let tests =
8383
let model =
8484
{ Type = "https://example.com/problems/conflict"
8585
Title = "Conflict"
86-
Status = Nullable 409
86+
Status = 409
8787
Detail = "The requested change conflicts with current state."
8888
Instance = "/companies/42" }
8989

@@ -103,7 +103,7 @@ let tests =
103103
let model =
104104
{ Type = null
105105
Title = null
106-
Status = Nullable 404
106+
Status = 404
107107
Detail = null
108108
Instance = null }
109109

@@ -125,6 +125,17 @@ let tests =
125125

126126
Expect.equal model.Type "https://example.com/problems/not-found" "Type should deserialize"
127127
Expect.equal model.Title "Not Found" "Title should deserialize"
128-
Expect.equal model.Status (Nullable 404) "Status should deserialize"
128+
Expect.equal model.Status 404 "Status should deserialize"
129129
Expect.equal model.Detail "Missing." "Detail should deserialize"
130-
Expect.equal model.Instance "/companies/99" "Instance should deserialize" ]
130+
Expect.equal model.Instance "/companies/99" "Instance should deserialize"
131+
132+
testCase "ProblemDetailsModel deserializes missing status as default int"
133+
<| fun _ ->
134+
let serializer = JsonSerializer() :> IJsonSerializer
135+
136+
let json =
137+
"{\"type\":\"https://example.com/problems/not-found\",\"title\":\"Not Found\",\"detail\":\"Missing.\",\"instance\":\"/companies/99\"}"
138+
139+
let model = serializer.Deserialize<ProblemDetailsModel>(json)
140+
141+
Expect.equal model.Status 0 "Missing status should deserialize to default int value" ]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
module StatusCodeHelpersTests
2+
3+
open Expecto
4+
open Fossa.Bridge.Models.ApiModels
5+
open Fossa.Bridge.Services.StatusCodeHelpers
6+
7+
let private problemWithStatus status =
8+
{ Type = null
9+
Title = null
10+
Status = status
11+
Detail = null
12+
Instance = null }
13+
14+
[<Tests>]
15+
let tests =
16+
testList
17+
"StatusCodeHelpersTests"
18+
[ testList
19+
"isStatusCodeClientError"
20+
[ testCase "returns false below 400"
21+
<| fun _ -> Expect.isFalse (isStatusCodeClientError 399) "399 should not be a client error"
22+
23+
testCase "returns true at 400"
24+
<| fun _ -> Expect.isTrue (isStatusCodeClientError 400) "400 should be a client error"
25+
26+
testCase "returns true at 499"
27+
<| fun _ -> Expect.isTrue (isStatusCodeClientError 499) "499 should be a client error"
28+
29+
testCase "returns false at 500"
30+
<| fun _ -> Expect.isFalse (isStatusCodeClientError 500) "500 should not be a client error" ]
31+
32+
testList
33+
"isStatusCodeServerError"
34+
[ testCase "returns false at 499"
35+
<| fun _ -> Expect.isFalse (isStatusCodeServerError 499) "499 should not be a server error"
36+
37+
testCase "returns true at 500"
38+
<| fun _ -> Expect.isTrue (isStatusCodeServerError 500) "500 should be a server error"
39+
40+
testCase "returns true at 599"
41+
<| fun _ -> Expect.isTrue (isStatusCodeServerError 599) "599 should be a server error"
42+
43+
testCase "returns false at 600"
44+
<| fun _ -> Expect.isFalse (isStatusCodeServerError 600) "600 should not be a server error" ]
45+
46+
testList
47+
"ProblemDetailsModel helpers"
48+
[ testCase "client problem returns client true and server false"
49+
<| fun _ ->
50+
let problem = problemWithStatus 404
51+
52+
Expect.isTrue (isClientProblem problem) "4xx problem should be a client problem"
53+
Expect.isFalse (isServerProblem problem) "4xx problem should not be a server problem"
54+
55+
testCase "server problem returns server true and client false"
56+
<| fun _ ->
57+
let problem = problemWithStatus 500
58+
59+
Expect.isTrue (isServerProblem problem) "5xx problem should be a server problem"
60+
Expect.isFalse (isClientProblem problem) "5xx problem should not be a client problem"
61+
62+
testCase "non-error problem returns false for both helpers"
63+
<| fun _ ->
64+
let problem = problemWithStatus 200
65+
66+
Expect.isFalse (isClientProblem problem) "Non-error problem should not be a client problem"
67+
Expect.isFalse (isServerProblem problem) "Non-error problem should not be a server problem" ] ]

0 commit comments

Comments
 (0)