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

How to test parameters supplied to call? #186

Closed SemyenSoldatenko closed 8 years ago

SemyenSoldatenko commented 8 years ago

How to test which parameters was supplied by code under the test?

it('should use propper QS parameter', function(done) {
    mockClient.mockSimpleResponse('/test', {hello: 'world'}, 200);
    webDriver
        .url('http://localhost:4792/codeUnderTest.html')
        .pause(1000)
        .then(function() {
            // codeUnderTest.html call (like /test?foo=bar
            // I need to test:
            // 1. the cal was actually made (one times)
            expect(howManyTimesWasRequested('/test')).toBe(1);
            // 2. the call had specific query string parameter
            expect(actualQueryString['foo']).toBe('bar');
        })
        .call(done)
        .end();
});

Please don't offer to use query string matcher when i configure responst in mockSimpleResponse. Because i need clear answer when test fail. Like expected 'undefined' to be 'bar'. Not like request blah-blah-blah expected called 1 times but was called 0 times. Thats not true.

jamesdbloom commented 8 years ago

There is a retrieve endpoint that can be used to retrieve a list of the requests that have been submitted. It takes a request matcher so you can retrieve only the requests you are interested in then interrogate them in any way you like. This is the only existing alternative to the verify endpoint. However I am currently extending the javascript client to support the retrieve endpoint. It should be done in the next couple of days. Currently this endpoint only exists on the server side.

In addition longer term I will support function callbacks when a request is matched on the server side so you will be able to perform any complex logic you want for each request including verifying the request parameters. This is however a bigger piece of work that involves using websockets and so will take a bit longer before it is available. I've done some work on this but I want to re-write the ruby client first.

jamesdbloom commented 8 years ago

Which client are you using? The browser javascript client or the node javascript client?

SemyenSoldatenko commented 8 years ago

It is "A node client for the MockServer" "1.0.6"

jamesdbloom commented 8 years ago

ok I have finished implementing this for the browser javascript client, and I'm halfway through doing this for the node client. The node client takes a little longer as the unit tests are more painful with all the promise callbacks.

jamesdbloom commented 8 years ago

If you want to retrieve the exact requests that were sent so that you can make any assertion you can use the retrieveRequests function, for example in nodeunit the code might look as follows:

var client = mockServerClient("localhost", 1080);
client.retrieveRequests("/somePathOne").then(function (requests) {
    // then
    test.equals(requests.length, 4);
    // first request
    test.equals(requests[0].path, '/somePathOne');
    test.equals(requests[0].method, 'POST');
    test.equals(requests[0].body, 'someBody');
    // second request
    test.equals(requests[1].path, '/somePathOne');
    test.equals(requests[1].method, 'GET');
    test.done();
}, function () {
    test.ok(false, "should have sent correct request");
    test.done();
});

The retrieveRequests method takes a request matcher object, a string path matcher or no parameter to retrieve all.

As follows:

// object matcher
client.retrieveRequests({
    "httpRequest": {
        "path": "/.*"
    }
}).then(function (requests) {
    // do something with requests
});

// path matcher
client.retrieveRequests("/.*").then(function (requests) {
    // do something with requests
});

// no parameters
client.retrieveRequests().then(function (requests) {
    // do something with requests
});

This was fixed for the node client in in https://github.com/jamesdbloom/mockserver-client-node/commit/531a8e5608e7230baf5d01e222de5257367fc073

SemyenSoldatenko commented 8 years ago

:+1: Thank you.