mock-server / mockserver

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding).
http://mock-server.com
Apache License 2.0
4.57k stars 1.07k forks source link

MockServerClient callback issue #411

Closed govindndr closed 6 years ago

govindndr commented 6 years ago

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 .

govindndr commented 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(); } }

            }

    );
jamesdbloom commented 6 years ago

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.

screen shot 2018-01-13 at 22 51 53

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.

govindndr commented 6 years ago

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(); } } } );

govindndr commented 6 years ago

I get 2 errors:

for below callback as well. new MockServerClient("localhost", 1080).when(request().withQueryStringParameter("MeasureName",".*")) .callback(new HttpCallback().withCallbackClass(metricCallBack.class.getName()));