Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a81a4f7

Browse files
committed
feat(request): add match method to match an array of patterns to current url
match method will be helpful when, we want to find whether the current url belongs to a given route pattern. It can used to ingore some filters on array of routes
1 parent ff6d94f commit a81a4f7

File tree

2 files changed

+96
-9
lines changed

2 files changed

+96
-9
lines changed

src/Request/index.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
const nodeReq = require('node-req')
1010
const nodeCookie = require('node-cookie')
1111
const File = require('../File')
12+
const pathToRegexp = require('path-to-regexp')
1213
const _ = require('lodash')
1314
const CatLog = require('cat-log')
1415
const log = new CatLog('adonis:framework')
@@ -352,8 +353,8 @@ class Request {
352353
* @description converts a file object to file instance
353354
* if already is not an instance
354355
* @method _toFileInstance
355-
* @param {Object} file [description]
356-
* @return {Object} [description]
356+
* @param {Object} file
357+
* @return {Object}
357358
* @private
358359
*/
359360
_toFileInstance (file) {
@@ -367,8 +368,8 @@ class Request {
367368
* @description converts an uploaded file to file
368369
* instance
369370
* @method file
370-
* @param {String} key [description]
371-
* @return {Object} [description]
371+
* @param {String} key
372+
* @return {Object}
372373
* @public
373374
*/
374375
file (key) {
@@ -415,8 +416,8 @@ class Request {
415416
* @description flash an object of messages to upcoming
416417
* request
417418
* @method flash
418-
* @param {Object} values [description]
419-
* @return {void} [description]
419+
* @param {Object} values
420+
* @return {void}
420421
* @public
421422
*/
422423
* flash (values) {
@@ -430,9 +431,9 @@ class Request {
430431
* @description return values set via flash from
431432
* request session
432433
* @method old
433-
* @param {String} key [description]
434-
* @param {Mixed} defaultValue [description]
435-
* @return {Mixed} [description]
434+
* @param {String} key
435+
* @param {Mixed} defaultValue
436+
* @return {Mixed}
436437
* @public
437438
*/
438439
old (key, defaultValue) {
@@ -478,6 +479,22 @@ class Request {
478479
const args = _.isArray(arguments[0]) ? arguments[0] : _.toArray(arguments)
479480
yield this.flash(this.except(args))
480481
}
482+
483+
/**
484+
* @description tells whether a given pattern matches the
485+
* current url or not
486+
* @method match
487+
* @param {String} pattern
488+
* @return {Boolean}
489+
* @public
490+
*/
491+
match () {
492+
const args = _.isArray(arguments[0]) ? arguments[0] : _.toArray(arguments)
493+
const url = this.url()
494+
const pattern = pathToRegexp(args, [])
495+
return pattern.test(url)
496+
}
497+
481498
}
482499

483500
module.exports = Request

test/unit/request.spec.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,76 @@ describe('Request', function () {
566566
expect(res.body.file).to.equal(true)
567567
})
568568

569+
it('should return true when a pattern matches the current route url', function * () {
570+
const server = http.createServer(function (req, res) {
571+
const request = new Request(req, res, Config)
572+
const matches = request.match('/user/:id/profile')
573+
res.writeHead(200, {"Content-type":"application/json"})
574+
res.end(JSON.stringify({matches}),'utf8')
575+
})
576+
577+
const res = yield supertest(server).get("/user/1/profile").expect(200).end()
578+
expect(res.body.matches).to.equal(true)
579+
})
580+
581+
it('should return false when a pattern does not matches the current route url', function * () {
582+
const server = http.createServer(function (req, res) {
583+
const request = new Request(req, res, Config)
584+
const matches = request.match('/user')
585+
res.writeHead(200, {"Content-type":"application/json"})
586+
res.end(JSON.stringify({matches}),'utf8')
587+
})
588+
589+
const res = yield supertest(server).get("/user/1/profile").expect(200).end()
590+
expect(res.body.matches).to.equal(false)
591+
})
592+
593+
it('should return true when any of the paths inside array matches the current route url', function * () {
594+
const server = http.createServer(function (req, res) {
595+
const request = new Request(req, res, Config)
596+
const matches = request.match(['/user', '/user/1/profile'])
597+
res.writeHead(200, {"Content-type":"application/json"})
598+
res.end(JSON.stringify({matches}),'utf8')
599+
})
600+
const res = yield supertest(server).get("/user/1/profile").expect(200).end()
601+
expect(res.body.matches).to.equal(true)
602+
})
603+
604+
it('should return false when none of the paths inside array matches the current route url', function * () {
605+
const server = http.createServer(function (req, res) {
606+
const request = new Request(req, res, Config)
607+
const matches = request.match(['/user', '/user/1', '/1/profile'])
608+
res.writeHead(200, {"Content-type":"application/json"})
609+
res.end(JSON.stringify({matches}),'utf8')
610+
})
611+
const res = yield supertest(server).get("/user/1/profile").expect(200).end()
612+
expect(res.body.matches).to.equal(false)
613+
})
614+
615+
it('should return true when any of the paths from any of the arguments matches the current route url', function * () {
616+
const server = http.createServer(function (req, res) {
617+
const request = new Request(req, res, Config)
618+
const matches = request.match('/user', '/user/1/profile')
619+
res.writeHead(200, {"Content-type":"application/json"})
620+
res.end(JSON.stringify({matches}),'utf8')
621+
})
622+
623+
const res = yield supertest(server).get("/user/1/profile").expect(200).end()
624+
expect(res.body.matches).to.equal(true)
625+
})
626+
627+
it('should return false when any of the paths from any of the arguments does not matches the current route url', function * () {
628+
const server = http.createServer(function (req, res) {
629+
const request = new Request(req, res, Config)
630+
const matches = request.match('/user', '/user/1', '/user/profile')
631+
res.writeHead(200, {"Content-type":"application/json"})
632+
res.end(JSON.stringify({matches}),'utf8')
633+
})
634+
635+
const res = yield supertest(server).get("/user/1/profile").expect(200).end()
636+
expect(res.body.matches).to.equal(false)
637+
})
638+
569639
it('should return an empty file instance when file is not uploaded', function * () {
570640
const server = http.createServer(function (req, res) {
571641
var form = new formidable.IncomingForm();

0 commit comments

Comments
 (0)