Skip to content

Commit

Permalink
test(router): more testcase for router
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore committed Aug 10, 2016
1 parent 5d9bb18 commit a067f98
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/utils/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class Router extends KoaRouter {
} else {
middleware = slice.call(arguments, 1);
prefix = name;
name = null;
name = '';
}

// last argument is Controller object
Expand Down Expand Up @@ -184,7 +184,7 @@ class Router extends KoaRouter {
const args = params;
let url = route.path || route.regexp.source;

const querys = [];
const queries = [];
if (typeof args === 'object' && args !== null) {
const replacedParams = [];
url = url.replace(/:([a-zA-Z_]\w*)/, function($0, key) {
Expand All @@ -195,7 +195,6 @@ class Router extends KoaRouter {
}
return $0;
});

for (const key in args) {
if (replacedParams.indexOf(key) !== -1) {
continue;
Expand All @@ -205,16 +204,16 @@ class Router extends KoaRouter {
const encodedKey = utility.encodeURIComponent(key);
if (Array.isArray(values)) {
for (const val of values) {
querys.push(`${encodedKey}=${utility.encodeURIComponent(val)}`);
queries.push(`${encodedKey}=${utility.encodeURIComponent(val)}`);
}
} else {
querys.push(`${encodedKey}=${utility.encodeURIComponent(values)}`);
queries.push(`${encodedKey}=${utility.encodeURIComponent(values)}`);
}
}
}

if (querys.length > 0) {
const queryStr = querys.join('&');
if (queries.length > 0) {
const queryStr = queries.join('&');
if (url.indexOf('?') === -1) {
url = `${url}?${queryStr}`;
} else {
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/router-app/app/controller/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

// 测试 app.resources 遇到 controller 没有足够的 action 的场景

exports.index = function* () {
this.body = 'index';
};

exports.new = function* () {
this.body = 'new';
};

exports.show = function* () {
this.body = 'show - ' + this.params.id;
};
3 changes: 3 additions & 0 deletions test/fixtures/router-app/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ module.exports = function (app) {
app.get('member_index', '/members/index', 'members.index');
app.resources('posts', '/posts', 'posts');
app.resources('members', '/members', app.controller.members);
app.resources('/comments', app.controller.comments);
app.get('comment_index', '/comments/:id?filter=', app.controller.comments.index);
app.register('/comments', [ 'post' ] , app.controller.comments.new);
};
20 changes: 20 additions & 0 deletions test/utils/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const mm = require('mm');
const utils = require('../../lib/utils');

describe('test/utils/index.test.js', () => {

afterEach(mm.restore);

describe('utils.getHomedir()', () => {
it('should return process.env.HOME', () => {
utils.getHomedir().should.equal(process.env.HOME);
});

it('should return /home/admin when process.env.HOME is not exist', () => {
mm(process.env, 'HOME', '');
utils.getHomedir().should.equal('/home/admin');
});
});
});
21 changes: 21 additions & 0 deletions test/utils/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,25 @@ describe('test/utils/router.test.js', () => {
.expect(404);
});
});

describe('no name', function() {
it('should GET /comments', () => {
return request(app.callback())
.get('/comments')
.expect('index')
.expect(200);
});

it('should POST /comments', () => {
return request(app.callback())
.post('/comments')
.expect('new')
.expect(200);
});
});
});


describe('router.url', () => {
it('should work', () => {
app.router.url('posts').should.equal('/posts');
Expand All @@ -122,6 +139,10 @@ describe('test/utils/router.test.js', () => {
app.router.url('new_post').should.equal('/posts/new');
app.router.url('new_member').should.equal('/members/new');
app.router.url('edit_post', { id: 1 }).should.equal('/posts/1/edit');
// no match params
app.router.url('edit_post', {}).should.equal('/posts/:id/edit');
app.router.url('noname').should.equal('');
app.router.url('comment_index', { id: 1, a: 1 }).should.equal('/comments/1?filter=&a=1');
});

it('should work with unknow params', () => {
Expand Down

0 comments on commit a067f98

Please sign in to comment.