Skip to content

Commit

Permalink
Added support for complex routes.
Browse files Browse the repository at this point in the history
  • Loading branch information
evangelion1204 committed Jan 10, 2016
1 parent be11637 commit 5e405fe
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/resolver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ export default class Resolver {
this.addRawRoute(route, 'ANY')
}

addRawRoute(route, path) {
addRawRoute(route, path = 'ANY') {
if (route.matcher.path && route.matcher.path.type === STRICT) {
path = route.matcher.path.match
}

let result = this.routes.find(path)

if (!result) {
Expand Down
10 changes: 10 additions & 0 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,20 @@ export default class Router {

addRoute(path, endpoint, id = '', method = null, filters = []) {
this.resolver.addRoute(path, endpoint, id, method, filters)

return this
}

addRegexRoute(regex, endpoint, id = '', method = null, filters = []) {
this.resolver.addRegexRoute(regex, endpoint, id, method, filters)

return this
}

addComplexRoute(route) {
this.resolver.addRawRoute(route)

return this
}

listen(port) {
Expand Down
14 changes: 7 additions & 7 deletions test/resolver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ describe('Resolver', function() {
expect(resolver.match({url: '/test', method: 'POST'}).endpoint).to.be.equal('http://domain.tld/new')
})

//it('Resolver should resolve STRICT header route', function () {
// let resolver = new Resolver(new Builder())
//
// resolver.init(exampleConfig.strictHeaderAjaxDefinition)
//
// expect(resolver.match({url: '/', method: 'GET', 'headers': {HTTP_X_REQUESTED_WITH: 'xmlhttprequest'}}).endpoint).to.be.equal('http://domain.tld/ajax')
//})
it('Resolver should resolve STRICT header route', function () {
let resolver = new Resolver()

resolver.addRawRoute(exampleConfig.strictHeaderAjaxDefinition.strict)

expect(resolver.match({url: '/', method: 'GET', 'headers': {HTTP_X_REQUESTED_WITH: 'xmlhttprequest'}}).endpoint).to.be.equal('http://domain.tld/ajax')
})
})
26 changes: 26 additions & 0 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,30 @@ describe('Router', function() {
})
})

it('a http request with a complex route should return the 200 result of the proxied endpoint', function (done) {
let server = http.createServer(function (request, response) {
response.writeHead(200)
response.end()
}).listen(configs.routerPort)


let router = new Router()
router.addComplexRoute({
matcher: {
path: {
match: '^/abc',
type: 'REGEX'
}
},
endpoint: `http://localhost:${configs.routerPort}`
})

request(router.listen())
.get('/abcdef')
.expect(200, function () {
server.close()
done.apply(this, arguments)
})
})

})

0 comments on commit 5e405fe

Please sign in to comment.