88import static java .net .HttpURLConnection .HTTP_UNAVAILABLE ;
99import static org .assertj .core .api .Assertions .assertThat ;
1010
11- import com .retailsvc .http .internal .gson .GsonJsonMapper ;
1211import com .retailsvc .http .spec .HttpMethod ;
1312import java .nio .charset .StandardCharsets ;
1413import java .util .List ;
1918
2019class HealthHandlerTest {
2120
22- private static final TypeMapper JSON = new GsonJsonMapper ();
2321 private static final UnaryOperator <String > NO_HEADERS = name -> null ;
2422
2523 private static Request request (HttpMethod method ) {
2624 return new Request (new byte [0 ], null , null , null , Map .of (), null , NO_HEADERS , Map .of (), method );
2725 }
2826
2927 @ Test
30- void getReturns200AndJsonBodyWhenUp () {
31- HealthOutcome outcome = new HealthOutcome (true , List .of (new Dependency ("jdbc" , true )));
28+ void getReturns200AndJsonBodyWhenAllDependenciesUp () {
29+ HealthOutcome outcome = new HealthOutcome (List .of (new Dependency ("jdbc" , true )));
3230
33- Response resp = Handlers .healthHandler (JSON , () -> outcome ).handle (request (GET ));
31+ Response resp = Handlers .healthHandler (() -> outcome ).handle (request (GET ));
3432
3533 assertThat (resp .status ()).isEqualTo (HTTP_OK );
3634 assertThat (resp .contentType ()).isEqualTo ("application/json" );
@@ -40,19 +38,18 @@ void getReturns200AndJsonBodyWhenUp() {
4038
4139 @ Test
4240 void getReturns200WithEmptyDependencyArrayWhenNoDeps () {
43- Response resp =
44- Handlers .healthHandler (JSON , () -> new HealthOutcome (true , List .of ())).handle (request (GET ));
41+ Response resp = Handlers .healthHandler (() -> new HealthOutcome (List .of ())).handle (request (GET ));
4542
4643 assertThat (resp .status ()).isEqualTo (HTTP_OK );
4744 assertThat (new String ((byte []) resp .body (), StandardCharsets .UTF_8 ))
4845 .isEqualTo ("{\" outcome\" :\" Up\" ,\" dependencies\" :[]}" );
4946 }
5047
5148 @ Test
52- void getReturns503WhenDown () {
53- HealthOutcome outcome = new HealthOutcome (false , List .of (new Dependency ("jdbc" , false )));
49+ void getReturns503WhenAnyDependencyDown () {
50+ HealthOutcome outcome = new HealthOutcome (List .of (new Dependency ("jdbc" , false )));
5451
55- Response resp = Handlers .healthHandler (JSON , () -> outcome ).handle (request (GET ));
52+ Response resp = Handlers .healthHandler (() -> outcome ).handle (request (GET ));
5653
5754 assertThat (resp .status ()).isEqualTo (HTTP_UNAVAILABLE );
5855 assertThat (resp .contentType ()).isEqualTo ("application/json" );
@@ -64,17 +61,15 @@ void getReturns503WhenDown() {
6461 @ Test
6562 void headIsAccepted () {
6663 Response resp =
67- Handlers .healthHandler (JSON , () -> new HealthOutcome (true , List .of ()))
68- .handle (request (HEAD ));
64+ Handlers .healthHandler (() -> new HealthOutcome (List .of ())).handle (request (HEAD ));
6965
7066 assertThat (resp .status ()).isEqualTo (HTTP_OK );
7167 }
7268
7369 @ Test
7470 void postReturns405WithAllowHeader () {
7571 Response resp =
76- Handlers .healthHandler (JSON , () -> new HealthOutcome (true , List .of ()))
77- .handle (request (POST ));
72+ Handlers .healthHandler (() -> new HealthOutcome (List .of ())).handle (request (POST ));
7873
7974 assertThat (resp .status ()).isEqualTo (HTTP_BAD_METHOD );
8075 assertThat (resp .headers ()).containsEntry ("Allow" , "GET, HEAD" );
@@ -87,7 +82,7 @@ void runtimeExceptionFromProbeMapsToDown503() {
8782 throw new IllegalStateException ("boom" );
8883 };
8984
90- Response resp = Handlers .healthHandler (JSON , failing ).handle (request (GET ));
85+ Response resp = Handlers .healthHandler (failing ).handle (request (GET ));
9186
9287 assertThat (resp .status ()).isEqualTo (HTTP_UNAVAILABLE );
9388 assertThat (new String ((byte []) resp .body (), StandardCharsets .UTF_8 ))
@@ -96,10 +91,22 @@ void runtimeExceptionFromProbeMapsToDown503() {
9691
9792 @ Test
9893 void nullReturnFromProbeMapsToDown503 () {
99- Response resp = Handlers .healthHandler (JSON , () -> null ).handle (request (GET ));
94+ Response resp = Handlers .healthHandler (() -> null ).handle (request (GET ));
10095
10196 assertThat (resp .status ()).isEqualTo (HTTP_UNAVAILABLE );
10297 assertThat (new String ((byte []) resp .body (), StandardCharsets .UTF_8 ))
10398 .isEqualTo ("{\" outcome\" :\" Down\" ,\" dependencies\" :[]}" );
10499 }
100+
101+ @ Test
102+ void escapesSpecialCharsInDependencyId () {
103+ HealthOutcome outcome = new HealthOutcome (List .of (new Dependency ("a\" b\\ c\n d" , true )));
104+
105+ Response resp = Handlers .healthHandler (() -> outcome ).handle (request (GET ));
106+
107+ assertThat (new String ((byte []) resp .body (), StandardCharsets .UTF_8 ))
108+ .isEqualTo (
109+ "{\" outcome\" :\" Up\" ,\" dependencies\" :[{\" id\" :\" a\\ \" b\\ \\ c\\ n"
110+ + "d\" ,\" status\" :\" Up\" }]}" );
111+ }
105112}
0 commit comments