Skip to content

Commit

Permalink
Add feature to map multiple/all methods to single mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
amitguptagwl committed Feb 20, 2017
1 parent af30390 commit 8e08578
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
16 changes: 16 additions & 0 deletions functional-tests/assets/mappings/response.yaml
Expand Up @@ -56,3 +56,19 @@
response:
latency: 2000
file: notexist.xml

#multiple methods
- request:
method: ["GET", "HEAD","POST"]
url: /stubs/methods/multiple

response:
body: "multiple methods are supported"

#all methods
- request:
method: "*"
url: /stubs/methods/all

response:
body: "You can bind all methods to single mapping."
58 changes: 56 additions & 2 deletions functional-tests/general_spec.js
Expand Up @@ -111,7 +111,6 @@ describe('FT', function () {


it('should send deflated response', function (done) {
var time = new Date();
chai.request("http://localhost:9999")
.get('/stubs/healthcheck')
.set('Accept-Encoding','deflate')
Expand All @@ -127,7 +126,6 @@ describe('FT', function () {
});

it('should send gzipped response', function (done) {
var time = new Date();
chai.request("http://localhost:9999")
.get('/stubs/healthcheck')
.set('Accept-Encoding','gzip')
Expand All @@ -142,6 +140,62 @@ describe('FT', function () {
});
});

it('should support multiple methods', function (done) {
chai.request("http://localhost:9999")
.get('/stubs/methods/multiple')
.then(res => {
expect(res.status).toBe(200);
expect(res.text).toBe("multiple methods are supported");
}).catch( err => {
fail("not expected");
done();
});

chai.request("http://localhost:9999")
.post('/stubs/methods/multiple')
.then(res => {
expect(res.status).toBe(200);
expect(res.text).toBe("multiple methods are supported");
done();
}).catch( err => {
fail("not expected");
done();
});

chai.request("http://localhost:9999")
.put('/stubs/methods/multiple')
.then(res => {
fail("not expected");
done();
}).catch( err => {
expect(err.status).toBe(404);
done();
});
});

it('should support single mapping for all methods', function (done) {
chai.request("http://localhost:9999")
.get('/stubs/methods/all')
.then(res => {
expect(res.status).toBe(200);
expect(res.text).toBe("You can bind all methods to single mapping.");
}).catch( err => {
fail("not expected");
done();
});

chai.request("http://localhost:9999")
.post('/stubs/methods/all')
.then(res => {
expect(res.status).toBe(200);
expect(res.text).toBe("You can bind all methods to single mapping.");
done();
}).catch( err => {
fail("not expected");
done();
});

});

});

Expand Down
8 changes: 7 additions & 1 deletion lib/request_resolver.js
Expand Up @@ -38,7 +38,13 @@ return undefined if any property of mapping doesn't match with request
function matchAndCapture(mapped_request, http_request){
var matched = {}, i=0;

if(mapped_request.method !== http_request.method) return;
if(Array.isArray(mapped_request.method)){
if(mapped_request.method.indexOf(http_request.method) === -1) return;
}else if(mapped_request.method === "*"){
//do nothing
}else{
if(mapped_request.method !== http_request.method) return;
}

if(mapped_request.url){
var urlmatch = util.getMatches(http_request.url,'^'+mapped_request.url+'$');
Expand Down

0 comments on commit 8e08578

Please sign in to comment.