Skip to content

Commit 85538da

Browse files
authored
Fixing nested router urls handling (#23)
* updating dependencies * adding github actions * fixing nested router urls handling
1 parent 8d17e75 commit 85538da

5 files changed

Lines changed: 53 additions & 6 deletions

File tree

.github/workflows/tests.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: tests
2+
on: [push, pull_request]
3+
4+
jobs:
5+
testing:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v1
9+
- name: Setup Environment (Using NodeJS 16.x)
10+
uses: actions/setup-node@v1
11+
with:
12+
node-version: 16.x
13+
14+
- name: Install dependencies
15+
run: npm install
16+
17+
- name: Linting
18+
run: npx standard
19+
20+
- name: Run tests
21+
run: npm run test

lib/next.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ function next (middlewares, req, res, index, routers = {}, defaultRoute, errorHa
2525
req.preRouterPath = req.path
2626

2727
req.url = req.url.replace(pattern, '')
28+
if (!req.url.startsWith('/')) {
29+
req.url = '/' + req.url
30+
}
2831
}
2932

3033
return middleware.lookup(req, res, step)

lib/router/sequential.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const Trouter = require('trouter')
22
const next = require('./../next')
33
const LRU = require('lru-cache')
4-
const parse = require('regexparam')
4+
const { parse } = require('regexparam')
55
const queryparams = require('../utils/queryparams')
66

77
module.exports = (config = {}) => {

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
},
2929
"homepage": "https://github.com/jkyberneees/0http#readme",
3030
"devDependencies": {
31-
"body-parser": "^1.19.0",
32-
"chai": "^4.3.4",
33-
"find-my-way": "^4.3.3",
34-
"mocha": "^9.1.3",
31+
"body-parser": "^1.19.1",
32+
"chai": "^4.3.6",
33+
"find-my-way": "^5.2.0",
34+
"mocha": "^9.2.0",
3535
"nyc": "^15.1.0",
36-
"supertest": "^6.1.6"
36+
"supertest": "^6.2.2"
3737
},
3838
"files": [
3939
"LICENSE",
@@ -43,6 +43,7 @@
4343
],
4444
"dependencies": {
4545
"lru-cache": "^6.0.0",
46+
"regexparam": "^2.0.0",
4647
"trouter": "^3.2.0"
4748
}
4849
}

tests/nested-routers.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ describe('0http Web Framework - Nested Routers', () => {
1515
req.body = req.url
1616
next()
1717
})
18+
router1.get('/', (req, res, next) => {
19+
req.body = req.url
20+
next()
21+
})
1822

1923
router.use(async (req, res, next) => {
2024
try {
@@ -63,6 +67,24 @@ describe('0http Web Framework - Nested Routers', () => {
6367
})
6468
})
6569

70+
it('should hit GET /url?format=json on nested router /r1', async () => {
71+
await request(baseUrl)
72+
.get('/r1/url?format=json')
73+
.expect(200)
74+
.then((response) => {
75+
expect(response.text).to.equal('/r1/url?format=json:/url?format=json')
76+
})
77+
})
78+
79+
it('should hit GET /?format=json on nested router /r1', async () => {
80+
await request(baseUrl)
81+
.get('/r1/?format=json')
82+
.expect(200)
83+
.then((response) => {
84+
expect(response.text).to.equal('/r1/?format=json:/?format=json')
85+
})
86+
})
87+
6688
it('should hit GET /url/:age on nested router /r2/:name', async () => {
6789
await request(baseUrl)
6890
.get('/r2/rolando/url/33?var=value')

0 commit comments

Comments
 (0)