Skip to content

Commit 28bc0a1

Browse files
committed
fixed verfications in MockServerClient
1 parent ec00ffd commit 28bc0a1

File tree

3 files changed

+52
-35
lines changed

3 files changed

+52
-35
lines changed

Diff for: changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Added
1010
- support for custom TLS protocols such as TLSv1.2,TLSv1.3
11+
- better error messages when MockServerClient fails due to TLS or networking errors
1112

1213
### Changed
1314
- removed implicit reliance on internal java-certificate-classes (thanks to @Arkinator)

Diff for: mockserver-client-java/src/main/java/org/mockserver/client/MockServerClient.java

+50-30
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ private NettyHttpClient getNettyHttpClient() {
375375
return nettyHttpClient;
376376
}
377377

378-
private HttpResponse sendRequest(HttpRequest request, boolean ignoreErrors) {
378+
private HttpResponse sendRequest(HttpRequest request, boolean ignoreErrors, boolean throwClientException) {
379379
if (!stopFuture.isDone()) {
380380
try {
381381
if (!request.containsHeader(CONTENT_TYPE.toString())
@@ -419,7 +419,7 @@ && isNotBlank(request.getBody().getContentType())) {
419419
}
420420
}
421421

422-
if (response != null && response.getStatusCode() != null && response.getStatusCode() >= 400) {
422+
if (throwClientException && response != null && response.getStatusCode() != null && response.getStatusCode() >= 400) {
423423
throw new ClientException(formatLogMessage("error:{}while sending request:{}", response, request));
424424
}
425425

@@ -436,8 +436,8 @@ && isNotBlank(request.getBody().getContentType())) {
436436
}
437437
}
438438

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);
441441
}
442442

443443
/**
@@ -517,7 +517,7 @@ public boolean isRunning() {
517517
@Deprecated
518518
public boolean isRunning(int attempts, long timeout, TimeUnit timeUnit) {
519519
try {
520-
HttpResponse httpResponse = sendRequest(request().withMethod("PUT").withPath(calculatePath("status")), true);
520+
HttpResponse httpResponse = sendRequest(request().withMethod("PUT").withPath(calculatePath("status")), true, false);
521521
if (httpResponse != null && httpResponse.getStatusCode() == HttpStatusCode.OK_200.code()) {
522522
return true;
523523
} else if (attempts <= 0) {
@@ -559,7 +559,7 @@ public boolean hasStopped() {
559559
*/
560560
public boolean hasStopped(int attempts, long timeout, TimeUnit timeUnit) {
561561
try {
562-
HttpResponse httpResponse = sendRequest(request().withMethod("PUT").withPath(calculatePath("status")), true);
562+
HttpResponse httpResponse = sendRequest(request().withMethod("PUT").withPath(calculatePath("status")), true, false);
563563
if (httpResponse != null && httpResponse.getStatusCode() == HttpStatusCode.OK_200.code()) {
564564
if (attempts <= 0) {
565565
return false;
@@ -574,7 +574,7 @@ public boolean hasStopped(int attempts, long timeout, TimeUnit timeUnit) {
574574
} else {
575575
return true;
576576
}
577-
} catch (ClientException | SocketConnectionException | IllegalStateException sce) {
577+
} catch (SocketConnectionException | IllegalStateException sce) {
578578
return true;
579579
}
580580
}
@@ -592,7 +592,7 @@ public boolean hasStarted() {
592592
*/
593593
public boolean hasStarted(int attempts, long timeout, TimeUnit timeUnit) {
594594
try {
595-
HttpResponse httpResponse = sendRequest(request().withMethod("PUT").withPath(calculatePath("status")));
595+
HttpResponse httpResponse = sendRequest(request().withMethod("PUT").withPath(calculatePath("status")), false);
596596
if (httpResponse.getStatusCode() == HttpStatusCode.OK_200.code()) {
597597
return true;
598598
} else if (attempts <= 0) {
@@ -605,7 +605,7 @@ public boolean hasStarted(int attempts, long timeout, TimeUnit timeUnit) {
605605
}
606606
return hasStarted(attempts - 1, timeout, timeUnit);
607607
}
608-
} catch (ClientException | SocketConnectionException | IllegalStateException sce) {
608+
} catch (SocketConnectionException | IllegalStateException sce) {
609609
if (attempts <= 0) {
610610
if (MockServerLogger.isEnabled(DEBUG)) {
611611
MOCK_SERVER_LOGGER.logEvent(
@@ -635,7 +635,8 @@ public List<Integer> bind(Integer... ports) {
635635
request()
636636
.withMethod("PUT")
637637
.withPath(calculatePath("bind"))
638-
.withBody(portBindingSerializer.serialize(portBinding(ports)), StandardCharsets.UTF_8)
638+
.withBody(portBindingSerializer.serialize(portBinding(ports)), StandardCharsets.UTF_8),
639+
true
639640
).getBodyAsString();
640641
return portBindingSerializer.deserialize(boundPorts).getPorts();
641642
}
@@ -674,7 +675,7 @@ public CompletableFuture<MockServerClient> stop(boolean ignoreFailure) {
674675
removeMockServerEventBus();
675676
new Scheduler.SchedulerThreadFactory("ClientStop").newThread(() -> {
676677
try {
677-
sendRequest(request().withMethod("PUT").withPath(calculatePath("stop")));
678+
sendRequest(request().withMethod("PUT").withPath(calculatePath("stop")), false);
678679
if (!hasStopped()) {
679680
for (int i = 0; !hasStopped() && i < 50; i++) {
680681
TimeUnit.MILLISECONDS.sleep(5);
@@ -720,7 +721,8 @@ public MockServerClient reset() {
720721
sendRequest(
721722
request()
722723
.withMethod("PUT")
723-
.withPath(calculatePath("reset"))
724+
.withPath(calculatePath("reset")),
725+
true
724726
);
725727
return clientClass.cast(this);
726728
}
@@ -736,7 +738,8 @@ public MockServerClient clear(RequestDefinition requestDefinition) {
736738
.withMethod("PUT")
737739
.withContentType(APPLICATION_JSON_UTF_8)
738740
.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
740743
);
741744
return clientClass.cast(this);
742745
}
@@ -761,7 +764,8 @@ public MockServerClient clear(ExpectationId expectationId) {
761764
.withMethod("PUT")
762765
.withContentType(APPLICATION_JSON_UTF_8)
763766
.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
765769
);
766770
return clientClass.cast(this);
767771
}
@@ -779,7 +783,8 @@ public MockServerClient clear(RequestDefinition requestDefinition, ClearType typ
779783
.withContentType(APPLICATION_JSON_UTF_8)
780784
.withPath(calculatePath("clear"))
781785
.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
783788
);
784789
return clientClass.cast(this);
785790
}
@@ -807,7 +812,8 @@ public MockServerClient clear(ExpectationId expectationId, ClearType type) {
807812
.withContentType(APPLICATION_JSON_UTF_8)
808813
.withPath(calculatePath("clear"))
809814
.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
811817
);
812818
return clientClass.cast(this);
813819
}
@@ -865,7 +871,8 @@ public MockServerClient verify(Integer maximumNumberOfRequestToReturnInVerificat
865871
.withMethod("PUT")
866872
.withContentType(APPLICATION_JSON_UTF_8)
867873
.withPath(calculatePath("verifySequence"))
868-
.withBody(verificationSequenceSerializer.serialize(verificationSequence), StandardCharsets.UTF_8)
874+
.withBody(verificationSequenceSerializer.serialize(verificationSequence), StandardCharsets.UTF_8),
875+
false
869876
).getBodyAsString();
870877

871878
if (result != null && !result.isEmpty()) {
@@ -953,7 +960,8 @@ public MockServerClient verify(Integer maximumNumberOfRequestToReturnInVerificat
953960
.withMethod("PUT")
954961
.withContentType(APPLICATION_JSON_UTF_8)
955962
.withPath(calculatePath("verifySequence"))
956-
.withBody(verificationSequenceSerializer.serialize(verificationSequence), StandardCharsets.UTF_8)
963+
.withBody(verificationSequenceSerializer.serialize(verificationSequence), StandardCharsets.UTF_8),
964+
false
957965
).getBodyAsString();
958966

959967
if (result != null && !result.isEmpty()) {
@@ -1034,7 +1042,8 @@ public MockServerClient verify(RequestDefinition requestDefinition, Verification
10341042
.withMethod("PUT")
10351043
.withContentType(APPLICATION_JSON_UTF_8)
10361044
.withPath(calculatePath("verify"))
1037-
.withBody(verificationSerializer.serialize(verification), StandardCharsets.UTF_8)
1045+
.withBody(verificationSerializer.serialize(verification), StandardCharsets.UTF_8),
1046+
false
10381047
).getBodyAsString();
10391048

10401049
if (result != null && !result.isEmpty()) {
@@ -1141,7 +1150,8 @@ public MockServerClient verify(ExpectationId expectationId, VerificationTimes ti
11411150
.withMethod("PUT")
11421151
.withContentType(APPLICATION_JSON_UTF_8)
11431152
.withPath(calculatePath("verify"))
1144-
.withBody(verificationSerializer.serialize(verification), StandardCharsets.UTF_8)
1153+
.withBody(verificationSerializer.serialize(verification), StandardCharsets.UTF_8),
1154+
false
11451155
).getBodyAsString();
11461156

11471157
if (result != null && !result.isEmpty()) {
@@ -1169,7 +1179,8 @@ public MockServerClient verifyZeroInteractions() throws AssertionError {
11691179
.withMethod("PUT")
11701180
.withContentType(APPLICATION_JSON_UTF_8)
11711181
.withPath(calculatePath("verify"))
1172-
.withBody(verificationSerializer.serialize(verification), StandardCharsets.UTF_8)
1182+
.withBody(verificationSerializer.serialize(verification), StandardCharsets.UTF_8),
1183+
false
11731184
).getBodyAsString();
11741185

11751186
if (result != null && !result.isEmpty()) {
@@ -1213,7 +1224,8 @@ public String retrieveRecordedRequests(RequestDefinition requestDefinition, Form
12131224
.withPath(calculatePath("retrieve"))
12141225
.withQueryStringParameter("type", RetrieveType.REQUESTS.name())
12151226
.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
12171229
);
12181230
return httpResponse.getBodyAsString();
12191231
}
@@ -1248,7 +1260,8 @@ public String retrieveRecordedRequestsAndResponses(RequestDefinition requestDefi
12481260
.withPath(calculatePath("retrieve"))
12491261
.withQueryStringParameter("type", RetrieveType.REQUEST_RESPONSES.name())
12501262
.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
12521265
);
12531266
return httpResponse.getBodyAsString();
12541267
}
@@ -1283,7 +1296,8 @@ public String retrieveRecordedExpectations(RequestDefinition requestDefinition,
12831296
.withPath(calculatePath("retrieve"))
12841297
.withQueryStringParameter("type", RetrieveType.RECORDED_EXPECTATIONS.name())
12851298
.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
12871301
);
12881302
return httpResponse.getBodyAsString();
12891303
}
@@ -1301,7 +1315,8 @@ public String retrieveLogMessages(RequestDefinition requestDefinition) {
13011315
.withContentType(APPLICATION_JSON_UTF_8)
13021316
.withPath(calculatePath("retrieve"))
13031317
.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
13051320
);
13061321
return httpResponse.getBodyAsString();
13071322
}
@@ -1447,7 +1462,8 @@ public Expectation[] upsert(OpenAPIExpectation... openAPIExpectations) {
14471462
.withMethod("PUT")
14481463
.withContentType(APPLICATION_JSON_UTF_8)
14491464
.withPath(calculatePath("openapi"))
1450-
.withBody(openAPIExpectationSerializer.serialize(openAPIExpectations[0]), StandardCharsets.UTF_8)
1465+
.withBody(openAPIExpectationSerializer.serialize(openAPIExpectations[0]), StandardCharsets.UTF_8),
1466+
false
14511467
);
14521468
if (httpResponse != null && httpResponse.getStatusCode() != 201) {
14531469
throw new ClientException(formatLogMessage("error:{}while submitted OpenAPI expectation:{}", httpResponse, openAPIExpectations[0]));
@@ -1459,7 +1475,8 @@ public Expectation[] upsert(OpenAPIExpectation... openAPIExpectations) {
14591475
.withMethod("PUT")
14601476
.withContentType(APPLICATION_JSON_UTF_8)
14611477
.withPath(calculatePath("openapi"))
1462-
.withBody(openAPIExpectationSerializer.serialize(openAPIExpectations), StandardCharsets.UTF_8)
1478+
.withBody(openAPIExpectationSerializer.serialize(openAPIExpectations), StandardCharsets.UTF_8),
1479+
true
14631480
);
14641481
if (httpResponse != null && httpResponse.getStatusCode() != 201) {
14651482
throw new ClientException(formatLogMessage("error:{}while submitted OpenAPI expectations:{}", httpResponse, openAPIExpectations));
@@ -1511,7 +1528,8 @@ public Expectation[] upsert(Expectation... expectations) {
15111528
.withMethod("PUT")
15121529
.withContentType(APPLICATION_JSON_UTF_8)
15131530
.withPath(calculatePath("expectation"))
1514-
.withBody(expectationSerializer.serialize(expectations[0]), StandardCharsets.UTF_8)
1531+
.withBody(expectationSerializer.serialize(expectations[0]), StandardCharsets.UTF_8),
1532+
false
15151533
);
15161534
if (httpResponse != null && httpResponse.getStatusCode() != 201) {
15171535
throw new ClientException(formatLogMessage("error:{}while submitted expectation:{}", httpResponse, expectations[0]));
@@ -1523,7 +1541,8 @@ public Expectation[] upsert(Expectation... expectations) {
15231541
.withMethod("PUT")
15241542
.withContentType(APPLICATION_JSON_UTF_8)
15251543
.withPath(calculatePath("expectation"))
1526-
.withBody(expectationSerializer.serialize(expectations), StandardCharsets.UTF_8)
1544+
.withBody(expectationSerializer.serialize(expectations), StandardCharsets.UTF_8),
1545+
false
15271546
);
15281547
if (httpResponse != null && httpResponse.getStatusCode() != 201) {
15291548
throw new ClientException(formatLogMessage("error:{}while submitted expectations:{}", httpResponse, expectations));
@@ -1594,7 +1613,8 @@ public String retrieveActiveExpectations(RequestDefinition requestDefinition, Fo
15941613
.withPath(calculatePath("retrieve"))
15951614
.withQueryStringParameter("type", RetrieveType.ACTIVE_EXPECTATIONS.name())
15961615
.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
15981618
);
15991619
return httpResponse.getBodyAsString();
16001620
}

Diff for: mockserver-client-java/src/test/java/org/mockserver/client/MockServerClientServerValidationErrorsTest.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,7 @@ public void shouldHandleOtherClientError() {
133133
" }" + NEW_LINE +
134134
" }" + NEW_LINE +
135135
"" + NEW_LINE +
136-
" while sending request:" + NEW_LINE +
137-
"" + NEW_LINE +
138-
" {" + NEW_LINE +
139-
" \"method\" : \"PUT\"," + NEW_LINE +
140-
" \"path\" : \"/mockserver/expectation\",")
136+
" while submitted expectation:")
141137
)
142138
;
143139
} finally {

0 commit comments

Comments
 (0)