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

Allow wildcards for query string parameters #81

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions mockserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ function getDirectoriesRecursive(srcpath) {
* GET--query=string&hello=hella.mock
*/
function getBodyOrQueryString(body, query) {
if (body && query) {
return '_'+ body + '--' + query;
}

Comment on lines +250 to +253
Copy link
Author

@KyleW KyleW Nov 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd appreciate another set of eyes on this.

This is the behavior described in the docs, but I don't think it was actually implemented before now. I added what I think is the right implementation, but I don't want to inadvertently introduce a breaking change.

If it's in doubt, I'm glad to remove it since it's not directly tied to the core purpose of the pr.

if (query) {
return '--' + query;
}
Expand Down Expand Up @@ -288,12 +292,12 @@ function getMockedContent(path, prefix, body, query) {
let content = handleMatch(path, exactName, fs.existsSync);

// Compare params without regard to order
if (!content && query && !body) {
content = testForQuery(path, prefix, query, false);
if (!content && query) {
content = testForQuery(path, prefix, body, query, false);

// Compare params without regard to order and allow wildcards
if (!content) {
content = testForQuery(path, prefix, query, true);
content = testForQuery(path, prefix, body, query, true);
}
}

Expand All @@ -306,11 +310,17 @@ function getMockedContent(path, prefix, body, query) {
return content;
}

function testForQuery(path, prefix, query, allowWildcards) {
function testForQuery(path, prefix, body, query, allowWildcards) {
// Find all files in the directory
return fs
.readdirSync(join(mockserver.directory, path))
.filter(possibleFile => possibleFile.startsWith(prefix) && possibleFile.endsWith('.mock'))
.filter(possibleFile => {
if (body) {
return possibleFile.startsWith(prefix + '_' + body) && possibleFile.endsWith('.mock');
}

return possibleFile.startsWith(prefix) && possibleFile.endsWith('.mock');
})
.filter(possibleFile => possibleFile.match(/--[\s\S]*__/))
.reduce((prev, possibleFile) => {
if (prev) {
Expand Down
4 changes: 4 additions & 0 deletions test/mocks/return-200/POST_Hello=123--a=b.mock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8

Hella
4 changes: 4 additions & 0 deletions test/mocks/return-200/POST_Hello=456--c=__.mock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8

Hello!!!
56 changes: 51 additions & 5 deletions test/mockserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,30 +420,76 @@ describe('mockserver', function() {
});
});

describe("query string parameters", function() {
describe('query string parameters', function() {
it('should be able to handle GET parameters', function() {
processRequest('/test?a=b', 'GET');

assert.equal(res.status, 200);
});

it("should handle a file with wildcards as query params", function() {
processRequest("/wildcard-params?foo=bar&buz=baz", "GET");
it('should handle a file with wildcards as query params', function() {
processRequest('/wildcard-params?foo=bar&buz=baz', 'GET');

assert.equal(res.status, 200);
});

it("should handle a request regardless of the order of the params in the query string", function() {
processRequest("/wildcard-params?buz=baz&foo=bar", "GET");
it('should handle a request regardless of the order of the params in the query string', function() {
processRequest('/wildcard-params?buz=baz&foo=bar', 'GET');

assert.equal(res.status, 200);
});

it('should not handle requests with extra params in the query string', function() {
processRequest('/wildcard-params?buz=baz&foo=bar&biz=bak', 'GET');

assert.equal(res.status, 404);
});

it('should default to GET.mock if no matching parameter file is found', function() {
processRequest('/test?a=c', 'GET');

assert.equal(res.status, 200);
});

it('should be able to include POST bodies and query params', function(done) {
const req = new MockReq({
method: 'POST',
url: '/return-200?a=b',
headers: {
Accept: 'text/plain'
}
});
req.write('Hello=123');
req.end();

mockserver(mocksDirectory, verbose)(req, res);

req.on('end', function() {
assert.equal(res.body, 'Hella');
assert.equal(res.status, 200);
done();
});
});

it('should be able to include POST bodies and query params with wildcards', function(done) {
const req = new MockReq({
method: 'POST',
url: '/return-200?c=d',
headers: {
Accept: 'text/plain'
}
});
req.write('Hello=456');
req.end();

mockserver(mocksDirectory, verbose)(req, res);

req.on('end', function() {
assert.equal(res.body, 'Hello!!!');
assert.equal(res.status, 200);
done();
});
});
});

describe('.getResponseDelay', function() {
Expand Down