mockserver-client-node icon indicating copy to clipboard operation
mockserver-client-node copied to clipboard

Missing return of embedded promise into retrieveRecordedRequestAndResponses

Open brcros opened this issue 2 years ago • 0 comments

Describe the issue When calling retrieveRecordedRequestsAndResponses and trying to out the result from the promise, the data remains unchanged, due to inverted execution code ( classic promise issue ).

What you are trying to do Trying to out the result from the promise...

MockServer version 5.15.0

To Reproduce try this code :

  let records: HttpRequestAndHttpResponse[] = [];
  await mockServerClient("localhost", 1080)
    .retrieveRecordedRequestsAndResponses({})
    .then((rs: HttpRequestAndHttpResponse[]) => {
      console.log('Inside, my data are there : ' + JSON.stringify(rs));
      records = rs;
    });
  console.log('Outside, my data array is empty :-( ' + JSON.stringify(records));
  1. How you are running MockServer (i.e maven plugin, docker, etc) in an express instance (but not related to the bug)
  2. Code you used to create expectations there is no expectations, only recording requests and trying to get them after
  3. What error you saw no error ( but .catch does related to the missing return )

Expected behaviour The recordedRequestAndResponses behaves as the others functions, regarding promise execution. Then the result to be passed outside the promise of my call.

** Fix proposed ** Simply add return into retrieveRecordedRequestsAndResponses for the makeRequest call, as the other functions have.

      var retrieveRecordedRequestsAndResponses = function (pathOrRequestMatcher) {
            return {
                then: function (sucess, error) {
                    return makeRequest(host, port, "/mockserver/retrieve?type=REQUEST_RESPONSES&format=JSON", addDefaultRequestMatcherHeaders(pathOrRequestMatcher))
                        .then(function (result) {
                            sucess(result.body && JSON.parse(result.body));
                        }, function (err) {
                            error(err);
                        });
                }
            };
        };```


Thank you

brcros avatar May 26 '23 10:05 brcros