Skip to content

Commit

Permalink
test: add test codes (eggjs#20)
Browse files Browse the repository at this point in the history
closes eggjs#16
  • Loading branch information
fengmk2 authored Jul 18, 2016
1 parent c87a3f6 commit 53a9bf4
Show file tree
Hide file tree
Showing 425 changed files with 7,886 additions and 56 deletions.
4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
test/fixtures
test/benchmark
examples/**/app/public
logs
run
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ run
.tmp
docs/CONTRIBUTING.md
docs/README.md

!test/fixtures/apps/loader-plugin/node_modules
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ install:
script:
- npm run ci
after_script:
- npm i codecov && codecov
- npminstall codecov && codecov
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ environment:

install:
- ps: Install-Product node $env:nodejs_version
- npm i npminstall && npminstall
- npm i npminstall && node_modules\.bin\npminstall

test_script:
- node --version
Expand Down
13 changes: 0 additions & 13 deletions docs/plugins.puml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,6 @@
@startuml
digraph world {
"onerror";
"userservice";
"userrole";
"session";
"i18n";
"validate";
"watcher";
"multipart";
"security" -> "session";
"development" -> "watcher";
"rest";
"static";
"cors" -> "security";
"logrotater";
"schedule";
}
@enduml
47 changes: 47 additions & 0 deletions examples/cookie/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const path = require('path');
const request = require('supertest-as-promised');
const mm = require('egg-mock');

describe('example cookie test', () => {
let app;

before(() => {
const baseDir = path.dirname(__dirname);
const customEgg = path.join(baseDir, '../..');
app = mm.app({
baseDir,
customEgg,
});
return app.ready();
});

after(() => app.close());

it('should GET / show "remember me" checkbox when cookie.remember not exists', () => {
return request(app.callback())
.get('/')
.expect(200)
.expect(/<input type="checkbox" name="remember"\/> remember me<\/label>/);
});

it('should POST /remember to set cookie.remember = 1', () => {
return request(app.callback())
.post('/remember')
.send({
remember: 'true',
})
.expect(302)
.expect('Location', '/')
.expect('Set-Cookie', /^remember=1; path=\/; expires=[^;]+; httponly,remember\.sig=[^;]+; path=\/; expires=[^;]+; httponly$/);
});

it('should GET /forget to delete cookie.remember', () => {
return request(app.callback())
.get('/forget')
.expect(302)
.expect('Location', '/')
.expect('Set-Cookie', /^remember=; path=\/; expires=[^;]+; httponly$/);
});
});
43 changes: 43 additions & 0 deletions examples/cookie_session/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

const path = require('path');
const request = require('supertest-as-promised');
const mm = require('egg-mock');

describe('example cookie_session test', () => {
let app;
let cookie;

before(() => {
const baseDir = path.dirname(__dirname);
const customEgg = path.join(baseDir, '../..');
app = mm.app({
baseDir,
customEgg,
});
return app.ready();
});

after(() => app.close());

it('should GET / first time', () => {
return request(app.callback())
.get('/')
.expect(200)
.expect(/^1 times/)
.expect('Set-Cookie', /^EGG_SESS=[^;]+; path=\/; expires=[^;]+; httponly$/)
.expect(res => {
cookie = res.headers['set-cookie'][0].split(';')[0];
});
});

it('should GET / second time', () => {
return request(app.callback())
.get('/')
.set('Cookie', cookie)
.expect(200)
.expect(/^2 times/)
// session.count change
.expect('Set-Cookie', /^EGG_SESS=[^;]+; path=\/; expires=[^;]+; httponly$/);
});
});
35 changes: 35 additions & 0 deletions examples/helloworld/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const path = require('path');
const request = require('supertest-as-promised');
const mm = require('egg-mock');

describe('example helloworld test', () => {
let app;

before(() => {
const baseDir = path.dirname(__dirname);
const customEgg = path.join(baseDir, '../..');
app = mm.app({
baseDir,
customEgg,
});
return app.ready();
});

after(() => app.close());

it('should GET / 200', () => {
return request(app.callback())
.get('/')
.expect(200)
.expect('Hello World');
});

it('should GET /foo', () => {
return request(app.callback())
.get('/foo')
.expect(200)
.expect('Hello foo');
});
});
3 changes: 3 additions & 0 deletions examples/multipart/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'my keys';
63 changes: 63 additions & 0 deletions examples/multipart/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

const assert = require('assert');
const path = require('path');
const request = require('supertest-as-promised');
const mm = require('egg-mock');
const formstream = require('formstream');
const urllib = require('urllib');

describe.skip('example multipart test', () => {
let app;
let csrfToken;
let cookies;
let host;
let server;

before(() => {
const baseDir = path.dirname(__dirname);
const customEgg = path.join(baseDir, '../..');
app = mm.app({
baseDir,
customEgg,
});
server = app.listen();
});

after(() => app.close());

it('should GET / show upload form', () => {
return request(server)
.get('/')
.expect(200)
.expect(/<p>Image: <input type="file" name="image" \/><\/p>/)
.expect(res => {
console.log(res.headers, res.text);
csrfToken = res.headers['x-csrf'];
cookies = res.headers['set-cookie'].join(';');
host = `http://127.0.0.1:${server.address().port}`;
});
});

it('should POST /upload success', done => {
const form = formstream();
form.file('file', __filename);
// other form fields
form.field('title', 'fengmk2 test title')
.field('love', 'egg');

const headers = form.headers();
headers.Cookie = cookies;
urllib.request(`${host}/upload?_csrf=${csrfToken}`, {
method: 'POST',
headers,
stream: form,
dataType: 'json',
}, (err, data, res) => {
assert(!err, err && err.message);
assert.equal(res.statusCode, 200);
console.log(data);
done();
});
});
});
2 changes: 1 addition & 1 deletion examples/static/app/router.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

module.exports = app => {
app.get('/', 'home');
app.get('/', app.controller.home);
};
3 changes: 3 additions & 0 deletions examples/static/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'my keys';
35 changes: 35 additions & 0 deletions examples/static/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const path = require('path');
const request = require('supertest-as-promised');
const mm = require('egg-mock');

describe('example static test', () => {
let app;

before(() => {
const baseDir = path.dirname(__dirname);
const customEgg = path.join(baseDir, '../..');
app = mm.app({
baseDir,
customEgg,
});
return app.ready();
});

after(() => app.close());

it('should GET / 200', () => {
return request(app.callback())
.get('/')
.expect(200)
.expect(/<li>Download <a href="\/public\/hi\.txt">hi\.txt<\/a>\.<\/li>/);
});

it('should GET /public/hi.txt', () => {
return request(app.callback())
.get('/public/hi.txt')
.expect(200)
.expect('hi egg.\n你好,蛋蛋。\n');
});
});
12 changes: 6 additions & 6 deletions lib/core/app/middleware/meta.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/**
* meta 中间件,放在最前面
* meta middleware, should be the first middleware
*/

'use strict';

module.exports = function() {
module.exports = () => {
let serverId = process.env.HOSTNAME || '';
if (serverId.indexOf('-') > 0) {
// appname-90-1 => 90-1
// appname-1-1 => 1-1
serverId = serverId.split('-').slice(1).join('-');
}

return function* (next) {
return function* meta(next) {
/**
* 开始处理当前请求的时间戳,单位 `ms`,方便做一些时间计算。
* Request start time
* @member {Number} Context#starttime
*/
this.starttime = Date.now();
Expand All @@ -28,7 +28,7 @@ module.exports = function() {
this.set('X-Server-Id', serverId);
}

// 设置一个 x-readtime 头, 供 nginx access log 使用, 也方便调试
// total response time header
this.set('X-Readtime', Date.now() - this.starttime);
};
};
2 changes: 1 addition & 1 deletion lib/core/app/middleware/notfound.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

module.exports = function(options) {
module.exports = options => {
return function* notfound(next) {
yield next;

Expand Down
4 changes: 2 additions & 2 deletions lib/core/app/middleware/site_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
const path = require('path');
const MAX_AGE = 30 * 24 * 68 * 60;

module.exports = function fixture(options) {
return function* (next) {
module.exports = options => {
return function* siteFile(next) {
if (this.method !== 'HEAD' && this.method !== 'GET') return yield next;

if (!options.hasOwnProperty(this.path)) return yield next;
Expand Down
Loading

0 comments on commit 53a9bf4

Please sign in to comment.