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" );
@@ -41,18 +39,18 @@ void getReturns200AndJsonBodyWhenUp() {
4139 @ Test
4240 void getReturns200WithEmptyDependencyArrayWhenNoDeps () {
4341 Response resp =
44- Handlers .healthHandler (JSON , () -> new HealthOutcome (true , List .of ())).handle (request (GET ));
42+ Handlers .healthHandler (() -> new HealthOutcome (List .of ())).handle (request (GET ));
4543
4644 assertThat (resp .status ()).isEqualTo (HTTP_OK );
4745 assertThat (new String ((byte []) resp .body (), StandardCharsets .UTF_8 ))
4846 .isEqualTo ("{\" outcome\" :\" Up\" ,\" dependencies\" :[]}" );
4947 }
5048
5149 @ Test
52- void getReturns503WhenDown () {
53- HealthOutcome outcome = new HealthOutcome (false , List .of (new Dependency ("jdbc" , false )));
50+ void getReturns503WhenAnyDependencyDown () {
51+ HealthOutcome outcome = new HealthOutcome (List .of (new Dependency ("jdbc" , false )));
5452
55- Response resp = Handlers .healthHandler (JSON , () -> outcome ).handle (request (GET ));
53+ Response resp = Handlers .healthHandler (() -> outcome ).handle (request (GET ));
5654
5755 assertThat (resp .status ()).isEqualTo (HTTP_UNAVAILABLE );
5856 assertThat (resp .contentType ()).isEqualTo ("application/json" );
@@ -64,17 +62,15 @@ void getReturns503WhenDown() {
6462 @ Test
6563 void headIsAccepted () {
6664 Response resp =
67- Handlers .healthHandler (JSON , () -> new HealthOutcome (true , List .of ()))
68- .handle (request (HEAD ));
65+ Handlers .healthHandler (() -> new HealthOutcome (List .of ())).handle (request (HEAD ));
6966
7067 assertThat (resp .status ()).isEqualTo (HTTP_OK );
7168 }
7269
7370 @ Test
7471 void postReturns405WithAllowHeader () {
7572 Response resp =
76- Handlers .healthHandler (JSON , () -> new HealthOutcome (true , List .of ()))
77- .handle (request (POST ));
73+ Handlers .healthHandler (() -> new HealthOutcome (List .of ())).handle (request (POST ));
7874
7975 assertThat (resp .status ()).isEqualTo (HTTP_BAD_METHOD );
8076 assertThat (resp .headers ()).containsEntry ("Allow" , "GET, HEAD" );
@@ -87,7 +83,7 @@ void runtimeExceptionFromProbeMapsToDown503() {
8783 throw new IllegalStateException ("boom" );
8884 };
8985
90- Response resp = Handlers .healthHandler (JSON , failing ).handle (request (GET ));
86+ Response resp = Handlers .healthHandler (failing ).handle (request (GET ));
9187
9288 assertThat (resp .status ()).isEqualTo (HTTP_UNAVAILABLE );
9389 assertThat (new String ((byte []) resp .body (), StandardCharsets .UTF_8 ))
@@ -96,10 +92,21 @@ void runtimeExceptionFromProbeMapsToDown503() {
9692
9793 @ Test
9894 void nullReturnFromProbeMapsToDown503 () {
99- Response resp = Handlers .healthHandler (JSON , () -> null ).handle (request (GET ));
95+ Response resp = Handlers .healthHandler (() -> null ).handle (request (GET ));
10096
10197 assertThat (resp .status ()).isEqualTo (HTTP_UNAVAILABLE );
10298 assertThat (new String ((byte []) resp .body (), StandardCharsets .UTF_8 ))
10399 .isEqualTo ("{\" outcome\" :\" Down\" ,\" dependencies\" :[]}" );
104100 }
101+
102+ @ Test
103+ void escapesSpecialCharsInDependencyId () {
104+ HealthOutcome outcome = new HealthOutcome (List .of (new Dependency ("a\" b\\ c\n d" , true )));
105+
106+ Response resp = Handlers .healthHandler (() -> outcome ).handle (request (GET ));
107+
108+ assertThat (new String ((byte []) resp .body (), StandardCharsets .UTF_8 ))
109+ .isEqualTo (
110+ "{\" outcome\" :\" Up\" ,\" dependencies\" :[{\" id\" :\" a\\ \" b\\ \\ c\\ nd\" ,\" status\" :\" Up\" }]}" );
111+ }
105112}
0 commit comments