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.59k stars 1.07k forks source link

Retrieve expectations are not working with grunt task #237

Closed GiritharanR closed 6 years ago

GiritharanR commented 8 years ago

I have created the grunt task to set an expectations and tried to retrieve the same.

var path = '/somePathOne';
var client = mockServerClient("localhost", 9000);
client.mockSimpleResponse(path, {name: 'one'}, 201);
client.mockSimpleResponse('/somePathTwo', {name: 'two'}, 202);
var expectations = client.retrieveExpectations(path);
console.log(expectations);

but it is not returning the expectations matched for the path. instead it is always returned as below

{ state: 'pending' }

I have verified in the mockserver.log after made the expectations. Expectations can be found at mockserver log, but it is not returning while calling with retrieveExpectations.

mockserver.txt

Advice me how to retrieve expectations with in javascript. I have tried retrieveExpectations for all too. Still the same. For me, it looks like retrieveExpectations didn't made contact at all.

GiritharanR commented 8 years ago

@jamesdbloom - Could you please guide me on this?

jamesdbloom commented 6 years ago

The client returns promises so the following code should fix your issue:

var mockServer = require('mockserver-client'),
  mockServerClient = mockServer.mockServerClient;

var path = '/somePathOne';
var client = mockServerClient("localhost", 9000);
client.mockSimpleResponse(path, {name: 'one'}, 201)
  .then(function () {
      client.mockSimpleResponse('/somePathTwo', {name: 'two'}, 202)
        .then(function () {
            client.retrieveExpectations(path).then(
              function (expectations) {
                console.log(expectations);
              },
              function (error) {
                console.log("Error retrieving expectations " + error);
              }
            );
          }, function (error) {
            console.log("Error creating /somePathTwo " + error);
          }
        );
    },
    function () {
      console.log("Error creating " + path + " " + error);
    }
  );

Please try this and re-open the issue if it is not working for you.