mockAnyResponse function adds default response headers
Closed this issue · 1 comments
From documentation in code:
https://github.com/jamesdbloom/mockserver-client-node/blob/master/mockServerClient.js#L320
default headers are used to specify the response headers in mockSimpleResponse(...)
(note: if you use mockAnyResponse(...) the default headers are not used)
But the real implementation of mockAnyResponse uses default headers:
https://github.com/jamesdbloom/mockserver-client-node/blob/master/mockServerClient.js#L249
It is a problem when I want to use Content-type: text/xml.
For this situation, I can use setDefaultHeaders, but is it a correct way?
if you do not want the default headers being added please using the following code:
client.setDefaultHeaders([], []);
There is a unit test which confirms that this approach works, as follows:
'should clear default headers': function (test) {
// when
client.mockAnyResponse({
'httpRequest': {
'path': '/somePathOne'
},
'httpResponse': {
'body': JSON.stringify({name: 'one'})
}
}).then(function () {
// then - matching request
sendRequest("GET", "localhost", mockServerPort, "/somePathOne")
.then(function (response) {
test.equal(response.statusCode, 200);
test.equal(response.body, '{"name":"one"}');
test.equal((response.headers["Content-Type"] || response.headers["content-type"]), ["application/json; charset=utf-8"]);
test.equal((response.headers["Cache-Control"] || response.headers["cache-control"]), ["no-cache, no-store"]);
client.setDefaultHeaders([], []);
client.mockAnyResponse({
'httpRequest': {
'path': '/somePathTwo'
},
'httpResponse': {
'body': JSON.stringify({name: 'one'})
}
}).then(function () {
// then - matching request
sendRequest("GET", "localhost", mockServerPort, "/somePathTwo")
.then(function (response) {
test.equal(response.statusCode, 200);
test.equal(response.body, '{"name":"one"}');
test.ok(!(response.headers["Content-Type"] || response.headers["content-type"]));
test.ok(!(response.headers["Cache-Control"] || response.headers["cache-control"]));
test.done();
}, function (error) {
test.ok(false, "failed with the following error \n" + JSON.stringify(error));
test.done();
});
}, function (error) {
test.ok(false, "failed with the following error \n" + JSON.stringify(error));
test.done();
});
}, function (error) {
test.ok(false, "failed with the following error \n" + JSON.stringify(error));
test.done();
});
}, function (error) {
test.ok(false, "failed with the following error \n" + JSON.stringify(error));
test.done();
});
},
I'm closing this ticket and the associated PR because the functionality already exists.