Closed govindndr closed 6 years ago
I get below error when I use HttpCallback class with MockServerClient
error:
2 errors:
- object instance has properties which are not allowed by the schema: ["httpCallback"]
- oneOf of the following must be specified "httpResponse" "httpResponseTemplate" "httpForward" "httpForwardTemplate" "httpClassCallback" "httpError" "httpObjectCallback"
while submitted expectation:
{
"httpRequest" : {
"path" : "metrics",
"queryStringParameters" : [ {
"name" : "MeasureName"
} ]
},
"times" : {
"remainingTimes" : 0,
"unlimited" : true
},
"timeToLive" : {
"unlimited" : true
},
"httpCallback" : { }
}
TestName: test.mock.java, Status: FAIL org.mockserver.client.server.ClientException:
Code:
new MockServerClient ("localhost",1080).when(request().withPath("metrics").withQueryStringParameter("MeasureName")).callback(
new HttpCallback() {
public HttpResponse handle(HttpRequest request) {
if (request.getMethod().getValue().equals("GET")) {
return response()
.withStatusCode(200)
.withHeaders(
header("x-object-callback", "test_object_callback_header")
)
.withBody("an_object_callback_response");
} else {
return notFoundResponse();
}
}
}
);
The first code example you have provided does compile because you are passing the wrong type to callback
function. As shown in the attached image.
The second JSON example is not a valid syntax as shown in the error message you have added.
I suggest you try following the code examples in the documentation: http://mock-server.com/mock_server/getting_started.html#setup_expectations
I am closing this issue as it is not a bug.
I have used example given under the method/closure callback java 7 section in http://mock-server.com/mock_server/getting_started.html#button_method_or_closure_callback. Still I face the same type mismatch error.
new MockServerClient("localhost", 1080) .when( request() .withPath("/some/path") ) .callback( new ExpectationCallback() { @Override public HttpResponse handle(HttpRequest request) { if (request.getMethod().getValue().equals("POST")) { return response() .withStatusCode(ACCEPTED_202.code()) .withHeaders( header("x-object-callback", "test_object_callback_header") ) .withBody("an_object_callback_response"); } else { return notFoundResponse(); } } } );
I get 2 errors:
for below callback as well. new MockServerClient("localhost", 1080).when(request().withQueryStringParameter("MeasureName",".*")) .callback(new HttpCallback().withCallbackClass(metricCallBack.class.getName()));
new MockServerClient ("localhost",1080).when(request().withPath("metrics").withQueryStringParameter("MeasureName")).callback( new ExpectationCallback() { @Override public HttpResponse handle(HttpRequest request) { if (request.getMethod().getValue().equals("GET")) { return response() .withStatusCode(ACCEPTED_202.code()) .withHeaders( header("x-object-callback", "test_object_callback_header") ) .withBody("an_object_callback_response"); } else { return notFoundResponse(); } }
above lines of code throws "anonymous ExpectationCallback can not be converted to Httpscallback" error as callback() accepts only HttpsCallback. I have java 7 and 3.10.2 version of mockserver.
is this version issue? The callback class works fine with class callback for ClientAndServer. Please suggest how to get dynamic response from remote server with MockServerClient .