@@ -375,7 +375,7 @@ private NettyHttpClient getNettyHttpClient() {
375
375
return nettyHttpClient ;
376
376
}
377
377
378
- private HttpResponse sendRequest (HttpRequest request , boolean ignoreErrors ) {
378
+ private HttpResponse sendRequest (HttpRequest request , boolean ignoreErrors , boolean throwClientException ) {
379
379
if (!stopFuture .isDone ()) {
380
380
try {
381
381
if (!request .containsHeader (CONTENT_TYPE .toString ())
@@ -419,7 +419,7 @@ && isNotBlank(request.getBody().getContentType())) {
419
419
}
420
420
}
421
421
422
- if (response != null && response .getStatusCode () != null && response .getStatusCode () >= 400 ) {
422
+ if (throwClientException && response != null && response .getStatusCode () != null && response .getStatusCode () >= 400 ) {
423
423
throw new ClientException (formatLogMessage ("error:{}while sending request:{}" , response , request ));
424
424
}
425
425
@@ -436,8 +436,8 @@ && isNotBlank(request.getBody().getContentType())) {
436
436
}
437
437
}
438
438
439
- private HttpResponse sendRequest (HttpRequest request ) {
440
- return sendRequest (request , false );
439
+ private HttpResponse sendRequest (HttpRequest request , boolean throwClientException ) {
440
+ return sendRequest (request , false , throwClientException );
441
441
}
442
442
443
443
/**
@@ -517,7 +517,7 @@ public boolean isRunning() {
517
517
@ Deprecated
518
518
public boolean isRunning (int attempts , long timeout , TimeUnit timeUnit ) {
519
519
try {
520
- HttpResponse httpResponse = sendRequest (request ().withMethod ("PUT" ).withPath (calculatePath ("status" )), true );
520
+ HttpResponse httpResponse = sendRequest (request ().withMethod ("PUT" ).withPath (calculatePath ("status" )), true , false );
521
521
if (httpResponse != null && httpResponse .getStatusCode () == HttpStatusCode .OK_200 .code ()) {
522
522
return true ;
523
523
} else if (attempts <= 0 ) {
@@ -559,7 +559,7 @@ public boolean hasStopped() {
559
559
*/
560
560
public boolean hasStopped (int attempts , long timeout , TimeUnit timeUnit ) {
561
561
try {
562
- HttpResponse httpResponse = sendRequest (request ().withMethod ("PUT" ).withPath (calculatePath ("status" )), true );
562
+ HttpResponse httpResponse = sendRequest (request ().withMethod ("PUT" ).withPath (calculatePath ("status" )), true , false );
563
563
if (httpResponse != null && httpResponse .getStatusCode () == HttpStatusCode .OK_200 .code ()) {
564
564
if (attempts <= 0 ) {
565
565
return false ;
@@ -574,7 +574,7 @@ public boolean hasStopped(int attempts, long timeout, TimeUnit timeUnit) {
574
574
} else {
575
575
return true ;
576
576
}
577
- } catch (ClientException | SocketConnectionException | IllegalStateException sce ) {
577
+ } catch (SocketConnectionException | IllegalStateException sce ) {
578
578
return true ;
579
579
}
580
580
}
@@ -592,7 +592,7 @@ public boolean hasStarted() {
592
592
*/
593
593
public boolean hasStarted (int attempts , long timeout , TimeUnit timeUnit ) {
594
594
try {
595
- HttpResponse httpResponse = sendRequest (request ().withMethod ("PUT" ).withPath (calculatePath ("status" )));
595
+ HttpResponse httpResponse = sendRequest (request ().withMethod ("PUT" ).withPath (calculatePath ("status" )), false );
596
596
if (httpResponse .getStatusCode () == HttpStatusCode .OK_200 .code ()) {
597
597
return true ;
598
598
} else if (attempts <= 0 ) {
@@ -605,7 +605,7 @@ public boolean hasStarted(int attempts, long timeout, TimeUnit timeUnit) {
605
605
}
606
606
return hasStarted (attempts - 1 , timeout , timeUnit );
607
607
}
608
- } catch (ClientException | SocketConnectionException | IllegalStateException sce ) {
608
+ } catch (SocketConnectionException | IllegalStateException sce ) {
609
609
if (attempts <= 0 ) {
610
610
if (MockServerLogger .isEnabled (DEBUG )) {
611
611
MOCK_SERVER_LOGGER .logEvent (
@@ -635,7 +635,8 @@ public List<Integer> bind(Integer... ports) {
635
635
request ()
636
636
.withMethod ("PUT" )
637
637
.withPath (calculatePath ("bind" ))
638
- .withBody (portBindingSerializer .serialize (portBinding (ports )), StandardCharsets .UTF_8 )
638
+ .withBody (portBindingSerializer .serialize (portBinding (ports )), StandardCharsets .UTF_8 ),
639
+ true
639
640
).getBodyAsString ();
640
641
return portBindingSerializer .deserialize (boundPorts ).getPorts ();
641
642
}
@@ -674,7 +675,7 @@ public CompletableFuture<MockServerClient> stop(boolean ignoreFailure) {
674
675
removeMockServerEventBus ();
675
676
new Scheduler .SchedulerThreadFactory ("ClientStop" ).newThread (() -> {
676
677
try {
677
- sendRequest (request ().withMethod ("PUT" ).withPath (calculatePath ("stop" )));
678
+ sendRequest (request ().withMethod ("PUT" ).withPath (calculatePath ("stop" )), false );
678
679
if (!hasStopped ()) {
679
680
for (int i = 0 ; !hasStopped () && i < 50 ; i ++) {
680
681
TimeUnit .MILLISECONDS .sleep (5 );
@@ -720,7 +721,8 @@ public MockServerClient reset() {
720
721
sendRequest (
721
722
request ()
722
723
.withMethod ("PUT" )
723
- .withPath (calculatePath ("reset" ))
724
+ .withPath (calculatePath ("reset" )),
725
+ true
724
726
);
725
727
return clientClass .cast (this );
726
728
}
@@ -736,7 +738,8 @@ public MockServerClient clear(RequestDefinition requestDefinition) {
736
738
.withMethod ("PUT" )
737
739
.withContentType (APPLICATION_JSON_UTF_8 )
738
740
.withPath (calculatePath ("clear" ))
739
- .withBody (requestDefinition != null ? requestDefinitionSerializer .serialize (requestDefinition ) : "" , StandardCharsets .UTF_8 )
741
+ .withBody (requestDefinition != null ? requestDefinitionSerializer .serialize (requestDefinition ) : "" , StandardCharsets .UTF_8 ),
742
+ true
740
743
);
741
744
return clientClass .cast (this );
742
745
}
@@ -761,7 +764,8 @@ public MockServerClient clear(ExpectationId expectationId) {
761
764
.withMethod ("PUT" )
762
765
.withContentType (APPLICATION_JSON_UTF_8 )
763
766
.withPath (calculatePath ("clear" ))
764
- .withBody (expectationId != null ? expectationIdSerializer .serialize (expectationId ) : "" , StandardCharsets .UTF_8 )
767
+ .withBody (expectationId != null ? expectationIdSerializer .serialize (expectationId ) : "" , StandardCharsets .UTF_8 ),
768
+ true
765
769
);
766
770
return clientClass .cast (this );
767
771
}
@@ -779,7 +783,8 @@ public MockServerClient clear(RequestDefinition requestDefinition, ClearType typ
779
783
.withContentType (APPLICATION_JSON_UTF_8 )
780
784
.withPath (calculatePath ("clear" ))
781
785
.withQueryStringParameter ("type" , type .name ().toLowerCase ())
782
- .withBody (requestDefinition != null ? requestDefinitionSerializer .serialize (requestDefinition ) : "" , StandardCharsets .UTF_8 )
786
+ .withBody (requestDefinition != null ? requestDefinitionSerializer .serialize (requestDefinition ) : "" , StandardCharsets .UTF_8 ),
787
+ true
783
788
);
784
789
return clientClass .cast (this );
785
790
}
@@ -807,7 +812,8 @@ public MockServerClient clear(ExpectationId expectationId, ClearType type) {
807
812
.withContentType (APPLICATION_JSON_UTF_8 )
808
813
.withPath (calculatePath ("clear" ))
809
814
.withQueryStringParameter ("type" , type .name ().toLowerCase ())
810
- .withBody (expectationId != null ? expectationIdSerializer .serialize (expectationId ) : "" , StandardCharsets .UTF_8 )
815
+ .withBody (expectationId != null ? expectationIdSerializer .serialize (expectationId ) : "" , StandardCharsets .UTF_8 ),
816
+ true
811
817
);
812
818
return clientClass .cast (this );
813
819
}
@@ -865,7 +871,8 @@ public MockServerClient verify(Integer maximumNumberOfRequestToReturnInVerificat
865
871
.withMethod ("PUT" )
866
872
.withContentType (APPLICATION_JSON_UTF_8 )
867
873
.withPath (calculatePath ("verifySequence" ))
868
- .withBody (verificationSequenceSerializer .serialize (verificationSequence ), StandardCharsets .UTF_8 )
874
+ .withBody (verificationSequenceSerializer .serialize (verificationSequence ), StandardCharsets .UTF_8 ),
875
+ false
869
876
).getBodyAsString ();
870
877
871
878
if (result != null && !result .isEmpty ()) {
@@ -953,7 +960,8 @@ public MockServerClient verify(Integer maximumNumberOfRequestToReturnInVerificat
953
960
.withMethod ("PUT" )
954
961
.withContentType (APPLICATION_JSON_UTF_8 )
955
962
.withPath (calculatePath ("verifySequence" ))
956
- .withBody (verificationSequenceSerializer .serialize (verificationSequence ), StandardCharsets .UTF_8 )
963
+ .withBody (verificationSequenceSerializer .serialize (verificationSequence ), StandardCharsets .UTF_8 ),
964
+ false
957
965
).getBodyAsString ();
958
966
959
967
if (result != null && !result .isEmpty ()) {
@@ -1034,7 +1042,8 @@ public MockServerClient verify(RequestDefinition requestDefinition, Verification
1034
1042
.withMethod ("PUT" )
1035
1043
.withContentType (APPLICATION_JSON_UTF_8 )
1036
1044
.withPath (calculatePath ("verify" ))
1037
- .withBody (verificationSerializer .serialize (verification ), StandardCharsets .UTF_8 )
1045
+ .withBody (verificationSerializer .serialize (verification ), StandardCharsets .UTF_8 ),
1046
+ false
1038
1047
).getBodyAsString ();
1039
1048
1040
1049
if (result != null && !result .isEmpty ()) {
@@ -1141,7 +1150,8 @@ public MockServerClient verify(ExpectationId expectationId, VerificationTimes ti
1141
1150
.withMethod ("PUT" )
1142
1151
.withContentType (APPLICATION_JSON_UTF_8 )
1143
1152
.withPath (calculatePath ("verify" ))
1144
- .withBody (verificationSerializer .serialize (verification ), StandardCharsets .UTF_8 )
1153
+ .withBody (verificationSerializer .serialize (verification ), StandardCharsets .UTF_8 ),
1154
+ false
1145
1155
).getBodyAsString ();
1146
1156
1147
1157
if (result != null && !result .isEmpty ()) {
@@ -1169,7 +1179,8 @@ public MockServerClient verifyZeroInteractions() throws AssertionError {
1169
1179
.withMethod ("PUT" )
1170
1180
.withContentType (APPLICATION_JSON_UTF_8 )
1171
1181
.withPath (calculatePath ("verify" ))
1172
- .withBody (verificationSerializer .serialize (verification ), StandardCharsets .UTF_8 )
1182
+ .withBody (verificationSerializer .serialize (verification ), StandardCharsets .UTF_8 ),
1183
+ false
1173
1184
).getBodyAsString ();
1174
1185
1175
1186
if (result != null && !result .isEmpty ()) {
@@ -1213,7 +1224,8 @@ public String retrieveRecordedRequests(RequestDefinition requestDefinition, Form
1213
1224
.withPath (calculatePath ("retrieve" ))
1214
1225
.withQueryStringParameter ("type" , RetrieveType .REQUESTS .name ())
1215
1226
.withQueryStringParameter ("format" , format .name ())
1216
- .withBody (requestDefinition != null ? requestDefinitionSerializer .serialize (requestDefinition ) : "" , StandardCharsets .UTF_8 )
1227
+ .withBody (requestDefinition != null ? requestDefinitionSerializer .serialize (requestDefinition ) : "" , StandardCharsets .UTF_8 ),
1228
+ true
1217
1229
);
1218
1230
return httpResponse .getBodyAsString ();
1219
1231
}
@@ -1248,7 +1260,8 @@ public String retrieveRecordedRequestsAndResponses(RequestDefinition requestDefi
1248
1260
.withPath (calculatePath ("retrieve" ))
1249
1261
.withQueryStringParameter ("type" , RetrieveType .REQUEST_RESPONSES .name ())
1250
1262
.withQueryStringParameter ("format" , format .name ())
1251
- .withBody (requestDefinition != null ? requestDefinitionSerializer .serialize (requestDefinition ) : "" , StandardCharsets .UTF_8 )
1263
+ .withBody (requestDefinition != null ? requestDefinitionSerializer .serialize (requestDefinition ) : "" , StandardCharsets .UTF_8 ),
1264
+ true
1252
1265
);
1253
1266
return httpResponse .getBodyAsString ();
1254
1267
}
@@ -1283,7 +1296,8 @@ public String retrieveRecordedExpectations(RequestDefinition requestDefinition,
1283
1296
.withPath (calculatePath ("retrieve" ))
1284
1297
.withQueryStringParameter ("type" , RetrieveType .RECORDED_EXPECTATIONS .name ())
1285
1298
.withQueryStringParameter ("format" , format .name ())
1286
- .withBody (requestDefinition != null ? requestDefinitionSerializer .serialize (requestDefinition ) : "" , StandardCharsets .UTF_8 )
1299
+ .withBody (requestDefinition != null ? requestDefinitionSerializer .serialize (requestDefinition ) : "" , StandardCharsets .UTF_8 ),
1300
+ true
1287
1301
);
1288
1302
return httpResponse .getBodyAsString ();
1289
1303
}
@@ -1301,7 +1315,8 @@ public String retrieveLogMessages(RequestDefinition requestDefinition) {
1301
1315
.withContentType (APPLICATION_JSON_UTF_8 )
1302
1316
.withPath (calculatePath ("retrieve" ))
1303
1317
.withQueryStringParameter ("type" , RetrieveType .LOGS .name ())
1304
- .withBody (requestDefinition != null ? requestDefinitionSerializer .serialize (requestDefinition ) : "" , StandardCharsets .UTF_8 )
1318
+ .withBody (requestDefinition != null ? requestDefinitionSerializer .serialize (requestDefinition ) : "" , StandardCharsets .UTF_8 ),
1319
+ true
1305
1320
);
1306
1321
return httpResponse .getBodyAsString ();
1307
1322
}
@@ -1447,7 +1462,8 @@ public Expectation[] upsert(OpenAPIExpectation... openAPIExpectations) {
1447
1462
.withMethod ("PUT" )
1448
1463
.withContentType (APPLICATION_JSON_UTF_8 )
1449
1464
.withPath (calculatePath ("openapi" ))
1450
- .withBody (openAPIExpectationSerializer .serialize (openAPIExpectations [0 ]), StandardCharsets .UTF_8 )
1465
+ .withBody (openAPIExpectationSerializer .serialize (openAPIExpectations [0 ]), StandardCharsets .UTF_8 ),
1466
+ false
1451
1467
);
1452
1468
if (httpResponse != null && httpResponse .getStatusCode () != 201 ) {
1453
1469
throw new ClientException (formatLogMessage ("error:{}while submitted OpenAPI expectation:{}" , httpResponse , openAPIExpectations [0 ]));
@@ -1459,7 +1475,8 @@ public Expectation[] upsert(OpenAPIExpectation... openAPIExpectations) {
1459
1475
.withMethod ("PUT" )
1460
1476
.withContentType (APPLICATION_JSON_UTF_8 )
1461
1477
.withPath (calculatePath ("openapi" ))
1462
- .withBody (openAPIExpectationSerializer .serialize (openAPIExpectations ), StandardCharsets .UTF_8 )
1478
+ .withBody (openAPIExpectationSerializer .serialize (openAPIExpectations ), StandardCharsets .UTF_8 ),
1479
+ true
1463
1480
);
1464
1481
if (httpResponse != null && httpResponse .getStatusCode () != 201 ) {
1465
1482
throw new ClientException (formatLogMessage ("error:{}while submitted OpenAPI expectations:{}" , httpResponse , openAPIExpectations ));
@@ -1511,7 +1528,8 @@ public Expectation[] upsert(Expectation... expectations) {
1511
1528
.withMethod ("PUT" )
1512
1529
.withContentType (APPLICATION_JSON_UTF_8 )
1513
1530
.withPath (calculatePath ("expectation" ))
1514
- .withBody (expectationSerializer .serialize (expectations [0 ]), StandardCharsets .UTF_8 )
1531
+ .withBody (expectationSerializer .serialize (expectations [0 ]), StandardCharsets .UTF_8 ),
1532
+ false
1515
1533
);
1516
1534
if (httpResponse != null && httpResponse .getStatusCode () != 201 ) {
1517
1535
throw new ClientException (formatLogMessage ("error:{}while submitted expectation:{}" , httpResponse , expectations [0 ]));
@@ -1523,7 +1541,8 @@ public Expectation[] upsert(Expectation... expectations) {
1523
1541
.withMethod ("PUT" )
1524
1542
.withContentType (APPLICATION_JSON_UTF_8 )
1525
1543
.withPath (calculatePath ("expectation" ))
1526
- .withBody (expectationSerializer .serialize (expectations ), StandardCharsets .UTF_8 )
1544
+ .withBody (expectationSerializer .serialize (expectations ), StandardCharsets .UTF_8 ),
1545
+ false
1527
1546
);
1528
1547
if (httpResponse != null && httpResponse .getStatusCode () != 201 ) {
1529
1548
throw new ClientException (formatLogMessage ("error:{}while submitted expectations:{}" , httpResponse , expectations ));
@@ -1594,7 +1613,8 @@ public String retrieveActiveExpectations(RequestDefinition requestDefinition, Fo
1594
1613
.withPath (calculatePath ("retrieve" ))
1595
1614
.withQueryStringParameter ("type" , RetrieveType .ACTIVE_EXPECTATIONS .name ())
1596
1615
.withQueryStringParameter ("format" , format .name ())
1597
- .withBody (requestDefinition != null ? requestDefinitionSerializer .serialize (requestDefinition ) : "" , StandardCharsets .UTF_8 )
1616
+ .withBody (requestDefinition != null ? requestDefinitionSerializer .serialize (requestDefinition ) : "" , StandardCharsets .UTF_8 ),
1617
+ false
1598
1618
);
1599
1619
return httpResponse .getBodyAsString ();
1600
1620
}
0 commit comments