Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

httpError does not work when created from the Javascript API #527

Closed
ghost opened this issue Sep 10, 2018 · 5 comments
Closed

httpError does not work when created from the Javascript API #527

ghost opened this issue Sep 10, 2018 · 5 comments

Comments

@ghost
Copy link

ghost commented Sep 10, 2018

When attempting to create an error expectation using the Javascript client e.g.

var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
    "httpRequest" : {
        "path" : "/some/path"
    },
    "httpError" : {
        "dropConnection" : true
    }
}).then(
    function () {
        console.log("expectation created");
    },
    function (error) {
        console.log(error);
    }
);

MockServer throws an error with the text:

Failed: "1 error:
     - oneOf of the following must be specified \"httpResponseTemplate\" \"httpForward\" \"httpForwardTemplate\" \"httpClassCallback\" \"httpObjectCallback\" "
@ghost
Copy link
Author

ghost commented Sep 10, 2018

I've created the simplest Node project that can reproduce this issue here: https://github.com/hq-mobile/mockserver-httperror

@ghost
Copy link
Author

ghost commented Sep 11, 2018

Interestingly when updating to the latest MockServer (5.4.1) the error is a little more helpful:

Failed: "1 error:
test_1        |      - oneOf of the following must be specified [\"httpResponse\", \"httpResponseTemplate\", \"httpResponseObjectCallback\", \"httpResponseClassCallback\", \"httpForward\", \"httpForwardTemplate\", \"httpForwardObjectCallback\", \"httpForwardClassCallback\", \"httpOverrideForwardedRequest\", \"httpError\"] but 2 found"

Suggesting the Node client may be sending the expectation incorrectly?

@ghost
Copy link
Author

ghost commented Sep 11, 2018

Yep, the request body sent by the node client automatically adds on a httpResponse object:

{"httpRequest":{"path":"/some/path"},"httpError":{"dropConnection":true},"httpResponse":{"headers":[{"name":"Content-Type","values":["application/json; charset=utf-8"]},{"name":"Cache-Control","values":["no-cache, no-store"]}]}}

@ghost
Copy link
Author

ghost commented Sep 11, 2018

Ok I have narrowed this down to the fact that a default httpResponse with some headers is added regardless of whether httpError and other valid objects are specified when the request is being mocked using mockAnyResponse:

        var mockAnyResponse = function (expectation) {
            return makeRequest(host, port, "/expectation", addDefaultExpectationHeaders(expectation));
        };

                var addDefaultExpectationHeaders = function (expectation) {
            if (Array.isArray(expectation)) {
                for (var i = 0; i < expectation.length; i++) {
                    expectation[i].httpRequest = addDefaultRequestMatcherHeaders(expectation[i].httpRequest);
                    expectation[i].httpResponse = addDefaultResponseMatcherHeaders(expectation[i].httpResponse);
                }
            } else {
                expectation.httpRequest = addDefaultRequestMatcherHeaders(expectation.httpRequest);
                expectation.httpResponse = addDefaultResponseMatcherHeaders(expectation.httpResponse);
            }
            return expectation;
        };

It feels like there should be some conditional logic in the implementation to addDefaultExpectationHeaders e.g.

        var addDefaultExpectationHeaders = function (expectation) {
            if (Array.isArray(expectation)) {
                for (var i = 0; i < expectation.length; i++) {
                    expectation[i].httpRequest = addDefaultRequestMatcherHeaders(expectation[i].httpRequest);
                    expectation[i].httpResponse = addDefaultResponseMatcherHeaders(expectation[i].httpResponse);
                }
            } else {
                expectation.httpRequest = addDefaultRequestMatcherHeaders(expectation.httpRequest);
                // PATCH STARTS HERE
                if (!expectation.httpError) { // And others
                  expectation.httpResponse = addDefaultResponseMatcherHeaders(expectation.httpResponse);
                }
                // PATCH ENDS HERE
            }
            return expectation;
        };

Patching the Node SDK with the implementation of addDefaultExpectationHeaders above does successfully work for us. However as I have next to no knowledge of MockServer would

  • This be a good idea? I.e is there any logic MockServer server side that still depends on that httpResponse object if one of the alternatives is supplied
  • Would this be a good approach or is there somewhere better in the Node SDK that this type of logic could be applied?
  • Is a pull request likely to be accepted for the Node SDK?

@jamesdbloom
Copy link
Collaborator

This has now been fixed and will be in the next release of mockserver-client-node

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant