From 78ccfbcaa749f3d9aa81003fcfbc57a7f782be3f Mon Sep 17 00:00:00 2001 From: popomore Date: Wed, 26 Oct 2016 15:58:38 +0800 Subject: [PATCH] test: fix unstable test --- .travis.yml | 1 + appveyor.yml | 1 + package.json | 3 +- test/app/extend/agent.test.js | 2 +- test/app/extend/context.test.js | 154 +++--- test/app/extend/request.test.js | 485 +++++++++--------- .../watcher-development-app/app/router.js | 2 +- test/lib/application.test.js | 7 +- test/lib/core/logger.test.js | 5 +- test/lib/core/urllib.test.js | 3 +- test/lib/plugins/multipart.test.js | 5 +- test/lib/plugins/watcher.test.js | 216 ++++---- 12 files changed, 429 insertions(+), 455 deletions(-) diff --git a/.travis.yml b/.travis.yml index 03736ab24c..5ec14674ca 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ language: node_js node_js: - '4' - '6' + - '7' install: - npm i npminstall && npminstall script: diff --git a/appveyor.yml b/appveyor.yml index e4893768a1..2efd0fadf7 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -2,6 +2,7 @@ environment: matrix: - nodejs_version: '4' - nodejs_version: '6' + - nodejs_version: '7' install: - ps: Install-Product node $env:nodejs_version diff --git a/package.json b/package.json index 1f69b61245..af65201aba 100644 --- a/package.json +++ b/package.json @@ -69,6 +69,7 @@ "estraverse": "^4.1.1", "formstream": "^1.0.0", "glob": "^7.1.1", + "ko-sleep": "^1.0.2", "koa": "^1.2.4", "koa-router": "^5.4.0", "merge-descriptors": "^1.0.1", @@ -117,6 +118,6 @@ "node": ">= 4.0.0" }, "ci": { - "version": "4, 6" + "version": "4, 6, 7" } } diff --git a/test/app/extend/agent.test.js b/test/app/extend/agent.test.js index f14e5883b5..0f90399a40 100644 --- a/test/app/extend/agent.test.js +++ b/test/app/extend/agent.test.js @@ -31,7 +31,7 @@ describe('test/app/extend/agent.test.js', () => { let app; afterEach(() => app.close()); - it.skip('should not log in unittest env', function* () { + it('should not log in unittest env', function* () { mm.env('unittest'); app = utils.app('apps/agent-instrument'); yield app.ready(); diff --git a/test/app/extend/context.test.js b/test/app/extend/context.test.js index 56cfc9c7ea..83dd2b68c1 100644 --- a/test/app/extend/context.test.js +++ b/test/app/extend/context.test.js @@ -1,119 +1,102 @@ 'use strict'; -const should = require('should'); const fs = require('fs'); const path = require('path'); const mm = require('egg-mock'); const request = require('supertest'); -const rimraf = require('rimraf'); const utils = require('../../utils'); -describe('test/app/extend/context.test.js', () => { +describe.only('test/app/extend/context.test.js', () => { afterEach(mm.restore); describe('ctx.logger', () => { - const baseDir = utils.getFilepath('apps/demo'); - - beforeEach(() => { - rimraf.sync(path.join(baseDir, 'logs')); - }); let app; afterEach(() => app.close()); - it('env=local: level => debug', done => { + it('env=local: level => debug', function* () { mm.env('local'); mm(process.env, 'EGG_LOG', 'none'); app = utils.app('apps/demo'); + yield app.ready(); const logdir = app.config.logger.dir; - request(app.callback()) + yield request(app.callback()) .get('/logger?message=foo') - .expect('logger', err => { - should.not.exists(err); - - const errorContent = fs.readFileSync(path.join(logdir, 'common-error.log'), 'utf8'); - errorContent.should.containEql('nodejs.Error: error foo'); - errorContent.should.containEql('nodejs.Error: core error foo'); - - const loggerContent = fs.readFileSync(path.join(logdir, 'demo-web.log'), 'utf8'); - loggerContent.should.containEql('debug foo'); - loggerContent.should.containEql('info foo'); - loggerContent.should.containEql('warn foo'); - - const coreLoggerContent = fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8'); - coreLoggerContent.should.containEql('core debug foo'); - coreLoggerContent.should.containEql('core info foo'); - coreLoggerContent.should.containEql('core warn foo'); - done(); - }); - }); - - it('env=unittest: level => info', done => { - mm.env('unittest'); - app = utils.app('apps/demo'); - app.ready(() => { - const logdir = app.config.logger.dir; - - app.mockContext({ - userId: '123123', - tracer: { - traceId: '456456', - }, - }); + .expect('logger'); - request(app.callback()) - .get('/logger?message=foo') - .expect('logger', err => { - should.not.exists(err); + const errorContent = fs.readFileSync(path.join(logdir, 'common-error.log'), 'utf8'); + errorContent.should.containEql('nodejs.Error: error foo'); + errorContent.should.containEql('nodejs.Error: core error foo'); - const errorContent = fs.readFileSync(path.join(logdir, 'common-error.log'), 'utf8'); - errorContent.should.containEql('nodejs.Error: error foo'); - errorContent.should.containEql('nodejs.Error: core error foo'); - errorContent.should.match(/\[123123\/[\d\.]+\/456456\/\d+ms GET \/logger\?message=foo]/); + const loggerContent = fs.readFileSync(path.join(logdir, 'demo-web.log'), 'utf8'); + loggerContent.should.containEql('debug foo'); + loggerContent.should.containEql('info foo'); + loggerContent.should.containEql('warn foo'); - const loggerContent = fs.readFileSync(path.join(logdir, 'demo-web.log'), 'utf8'); - loggerContent.should.not.containEql('debug foo'); - loggerContent.should.containEql('info foo'); - loggerContent.should.containEql('warn foo'); + const coreLoggerContent = fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8'); + coreLoggerContent.should.containEql('core debug foo'); + coreLoggerContent.should.containEql('core info foo'); + coreLoggerContent.should.containEql('core warn foo'); + }); - const coreLoggerContent = fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8'); - coreLoggerContent.should.not.containEql('core debug foo'); - coreLoggerContent.should.containEql('core info foo'); - coreLoggerContent.should.containEql('core warn foo'); + it('env=unittest: level => info', function* () { + mm.env('unittest'); + app = utils.app('apps/demo'); + yield app.ready(); + const logdir = app.config.logger.dir; - done(); - }); + app.mockContext({ + userId: '123123', + tracer: { + traceId: '456456', + }, }); + + yield request(app.callback()) + .get('/logger?message=foo') + .expect('logger'); + + const errorContent = fs.readFileSync(path.join(logdir, 'common-error.log'), 'utf8'); + errorContent.should.containEql('nodejs.Error: error foo'); + errorContent.should.containEql('nodejs.Error: core error foo'); + errorContent.should.match(/\[123123\/[\d\.]+\/456456\/\d+ms GET \/logger\?message=foo]/); + + const loggerContent = fs.readFileSync(path.join(logdir, 'demo-web.log'), 'utf8'); + loggerContent.should.not.containEql('debug foo'); + loggerContent.should.containEql('info foo'); + loggerContent.should.containEql('warn foo'); + + const coreLoggerContent = fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8'); + coreLoggerContent.should.not.containEql('core debug foo'); + coreLoggerContent.should.containEql('core info foo'); + coreLoggerContent.should.containEql('core warn foo'); }); - it('env=prod: level => info', done => { + it('env=prod: level => info', function* () { mm.env('unittest'); app = utils.app('apps/demo'); + yield app.ready(); const logdir = app.config.logger.dir; - request(app.callback()) + yield request(app.callback()) .get('/logger?message=foo') - .expect('logger', err => { - should.not.exists(err); + .expect('logger'); - const errorContent = fs.readFileSync(path.join(logdir, 'common-error.log'), 'utf8'); - errorContent.should.containEql('nodejs.Error: error foo'); - errorContent.should.containEql('nodejs.Error: core error foo'); + const errorContent = fs.readFileSync(path.join(logdir, 'common-error.log'), 'utf8'); + errorContent.should.containEql('nodejs.Error: error foo'); + errorContent.should.containEql('nodejs.Error: core error foo'); - const loggerContent = fs.readFileSync(path.join(logdir, 'demo-web.log'), 'utf8'); - loggerContent.should.not.containEql('debug foo'); - loggerContent.should.containEql('info foo'); - loggerContent.should.containEql('warn foo'); + const loggerContent = fs.readFileSync(path.join(logdir, 'demo-web.log'), 'utf8'); + loggerContent.should.not.containEql('debug foo'); + loggerContent.should.containEql('info foo'); + loggerContent.should.containEql('warn foo'); - const coreLoggerContent = fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8'); - coreLoggerContent.should.not.containEql('core debug foo'); - coreLoggerContent.should.containEql('core info foo'); - coreLoggerContent.should.containEql('core warn foo'); - - done(); - }); + const coreLoggerContent = fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8'); + coreLoggerContent.should.not.containEql('core debug foo'); + coreLoggerContent.should.containEql('core info foo'); + coreLoggerContent.should.containEql('core warn foo'); }); }); @@ -291,20 +274,21 @@ describe('test/app/extend/context.test.js', () => { afterEach(mm.restore); it('should curl ok', function* () { - this.timeout(10000); const context = app.mockContext(); - const res = yield context.curl('https://a.alipayobjects.com/aliBridge/1.0.0/aliBridge.min.js'); + const res = yield context.curl('https://a.alipayobjects.com/aliBridge/1.0.0/aliBridge.min.js', { + timeout: 10000, + }); res.status.should.equal(200); - }); + }).timeout(10000); }); describe('ctx.realStatus', () => { let app; - beforeEach(() => { + before(() => { app = utils.app('apps/demo'); return app.ready(); }); - afterEach(() => app.close()); + after(() => app.close()); afterEach(mm.restore); it('should get from status ok', () => { @@ -323,11 +307,11 @@ describe('test/app/extend/context.test.js', () => { describe('ctx.state', () => { let app; - beforeEach(() => { + before(() => { app = utils.app('apps/demo'); return app.ready(); }); - afterEach(() => app.close()); + after(() => app.close()); afterEach(mm.restore); it('should delegate ctx.locals', () => { diff --git a/test/app/extend/request.test.js b/test/app/extend/request.test.js index ddcd69a786..1197b18137 100644 --- a/test/app/extend/request.test.js +++ b/test/app/extend/request.test.js @@ -8,295 +8,302 @@ const utils = require('../../utils'); describe('test/app/extend/request.test.js', () => { - let app; - let ctx; - let req; - beforeEach(() => { - app = utils.app('apps/demo'); - return app.ready(); - }); - beforeEach(() => { - ctx = app.mockContext(); - req = ctx.request; - }); - afterEach(() => app.close()); - afterEach(mm.restore); - - describe('req.host', () => { - it('should return host with port', function* () { - mm(req.header, 'host', 'foo.com:3000'); - req.hostname.should.equal('foo.com'); + describe('normal', () => { + let app; + let ctx; + let req; + before(() => { + app = utils.app('apps/demo'); + return app.ready(); }); - - it('should return "localhost" when no host present', function* () { - req.host.should.be.a.String; - req.host.should.equal('localhost'); + after(() => app.close()); + beforeEach(() => { + ctx = app.mockContext(); + req = ctx.request; }); + afterEach(mm.restore); - it('should return host from X-Forwarded-Host header', function* () { - mm(req.header, 'x-forwarded-host', 'foo.com'); - req.host.should.be.a.String; - req.host.should.equal('foo.com'); - }); - }); + describe('req.host', () => { + it('should return host with port', function* () { + mm(req.header, 'host', 'foo.com:3000'); + req.hostname.should.equal('foo.com'); + }); - describe('req.hostname', () => { - it('should return hostname with port', function* () { - mm(req.header, 'host', 'foo.com:3000'); - req.hostname.should.equal('foo.com'); - }); + it('should return "localhost" when no host present', function* () { + req.host.should.be.a.String; + req.host.should.equal('localhost'); + }); - it('should return "localhost" when no host present', function* () { - req.hostname.should.be.a.String; - req.hostname.should.equal('localhost'); + it('should return host from X-Forwarded-Host header', function* () { + mm(req.header, 'x-forwarded-host', 'foo.com'); + req.host.should.be.a.String; + req.host.should.equal('foo.com'); + }); }); - }); - describe('req.ip', () => { - it('should return ipv4', function* () { - mm(req.socket, 'remoteAddress', '::ffff:127.0.0.1'); - req.ip.should.equal('127.0.0.1'); - req.ip.should.equal('127.0.0.1'); - }); - }); + describe('req.hostname', () => { + it('should return hostname with port', function* () { + mm(req.header, 'host', 'foo.com:3000'); + req.hostname.should.equal('foo.com'); + }); - describe('req.ips', () => { - it('should used x-forwarded-for', function* () { - mm(req.header, 'x-forwarded-for', '127.0.0.1,127.0.0.2'); - req.ips.should.eql([ '127.0.0.1', '127.0.0.2' ]); + it('should return "localhost" when no host present', function* () { + req.hostname.should.be.a.String; + req.hostname.should.equal('localhost'); + }); }); - it('should used x-real-ip', function* () { - mm(req.header, 'x-forwarded-for', ''); - mm(req.header, 'x-real-ip', '127.0.0.1,127.0.0.2'); - req.ips.should.eql([ '127.0.0.1', '127.0.0.2' ]); + describe('req.ip', () => { + it('should return ipv4', function* () { + mm(req.socket, 'remoteAddress', '::ffff:127.0.0.1'); + req.ip.should.equal('127.0.0.1'); + req.ip.should.equal('127.0.0.1'); + }); }); - it('should return []', function* () { - mm(req.header, 'x-forwarded-for', ''); - mm(req.header, 'x-real-ip', ''); - req.ips.should.eql([]); - }); - }); + describe('req.ips', () => { + it('should used x-forwarded-for', function* () { + mm(req.header, 'x-forwarded-for', '127.0.0.1,127.0.0.2'); + req.ips.should.eql([ '127.0.0.1', '127.0.0.2' ]); + }); - describe('req.protocol', () => { + it('should used x-real-ip', function* () { + mm(req.header, 'x-forwarded-for', ''); + mm(req.header, 'x-real-ip', '127.0.0.1,127.0.0.2'); + req.ips.should.eql([ '127.0.0.1', '127.0.0.2' ]); + }); - it('should return http when it not config and no protocol header', () => { - mm(app.config, 'protocl', null); - return request(app.callback()) - .get('/protocol') - .expect('http'); + it('should return []', function* () { + mm(req.header, 'x-forwarded-for', ''); + mm(req.header, 'x-real-ip', ''); + req.ips.should.eql([]); + }); }); - it('should return value of X-Custom-Proto', () => { - mm(app.config, 'protocolHeaders', 'X-Custom-Proto'); - return request(app.callback()) - .get('/protocol') - .set('X-Custom-Proto', 'https') - .expect('https'); - }); + describe('req.protocol', () => { - it('should ignore X-Client-Scheme', () => { - mm(app.config, 'protocolHeaders', 'X-Forwarded-Proto'); - return request(app.callback()) - .get('/protocol') - .set('X-Client-Scheme', 'https') - .expect('http'); - }); + it('should return http when it not config and no protocol header', () => { + mm(app.config, 'protocl', null); + return request(app.callback()) + .get('/protocol') + .expect('http'); + }); - it('should return value of X-Forwarded-Proto', () => { - return request(app.callback()) - .get('/protocol') - .set('x-forwarded-proto', 'https') - .expect('https'); - }); + it('should return value of X-Custom-Proto', () => { + mm(app.config, 'protocolHeaders', 'X-Custom-Proto'); + return request(app.callback()) + .get('/protocol') + .set('X-Custom-Proto', 'https') + .expect('https'); + }); - it('should ignore X-Forwarded-Proto', () => { - mm(app.config, 'protocolHeaders', ''); - return request(app.callback()) - .get('/protocol') - .set('x-forwarded-proto', 'https') - .expect('http'); - }); + it('should ignore X-Client-Scheme', () => { + mm(app.config, 'protocolHeaders', 'X-Forwarded-Proto'); + return request(app.callback()) + .get('/protocol') + .set('X-Client-Scheme', 'https') + .expect('http'); + }); - it('should return value from config', () => { - mm(app.config, 'protocol', 'https'); - return request(app.callback()) - .get('/protocol') - .expect('https'); - }); - }); + it('should return value of X-Forwarded-Proto', () => { + return request(app.callback()) + .get('/protocol') + .set('x-forwarded-proto', 'https') + .expect('https'); + }); - describe('this.query[key] => String', () => { - function expectQuery(querystring, query) { - mm(req, 'querystring', querystring); - req.query.should.eql(query); - mm.restore(); - } - - it('should get string value', () => { - expectQuery('a=b', { a: 'b' }); - expectQuery('a=&', { a: '' }); - expectQuery('a=b&', { a: 'b' }); - expectQuery('a.=b', { 'a.': 'b' }); - expectQuery('a=b&a=c', { a: 'b' }); - expectQuery('a=&a=c', { a: '' }); - expectQuery('a=c&a=b', { a: 'c' }); - expectQuery('a=c&a=b&b=bb', { a: 'c', b: 'bb' }); - expectQuery('a[=c&a[=b', { 'a[': 'c' }); - expectQuery('a{=c&a{=b', { 'a{': 'c' }); - expectQuery('a[]=c&a[]=b', { 'a[]': 'c' }); - expectQuery('a[]=&a[]=b', { 'a[]': '' }); - expectQuery('a[foo]=c', { 'a[foo]': 'c' }); - expectQuery('a[foo][bar]=c', { 'a[foo][bar]': 'c' }); - expectQuery('a=', { a: '' }); - expectQuery('a[]=a&a=b&a=c', { 'a[]': 'a', a: 'b' }); - }); + it('should ignore X-Forwarded-Proto', () => { + mm(app.config, 'protocolHeaders', ''); + return request(app.callback()) + .get('/protocol') + .set('x-forwarded-proto', 'https') + .expect('http'); + }); - it('should get undefined when key not exists', () => { - expectQuery('a=b', { a: 'b' }); + it('should return value from config', () => { + mm(app.config, 'protocol', 'https'); + return request(app.callback()) + .get('/protocol') + .expect('https'); + }); }); - }); - describe('this.queries[key] => Array', () => { - function expectQueries(querystring, query) { - mm(req, 'querystring', querystring); - req.queries.should.eql(query); - mm.restore(); - } - - it('should get array value', () => { - expectQueries('', { }); - expectQueries('a=', { a: [ '' ] }); - expectQueries('a=&', { a: [ '' ] }); - expectQueries('a=b&', { a: [ 'b' ] }); - expectQueries('a.=', { 'a.': [ '' ] }); - expectQueries('a=&a=&a=&a=&a=&a=&a=&a=', { a: [ '', '', '', '', '', '', '', '' ] }); - expectQueries('a=&a=&a=&a=&a=&a=&a=&a=&', { a: [ '', '', '', '', '', '', '', '' ] }); - expectQueries('a=&a=&a=&a=&a=&a=&a=&a=&&&&', { a: [ '', '', '', '', '', '', '', '' ] }); - expectQueries('a=b', { a: [ 'b' ] }); - expectQueries('a={}', { a: [ '{}' ] }); - expectQueries('a=[]', { a: [ '[]' ] }); - expectQueries('a[]=[]', { 'a[]': [ '[]' ], a: [ '[]' ] }); - expectQueries('a[]=&a[]=', { 'a[]': [ '', '' ], a: [ '', '' ] }); - expectQueries('a[]=[]&a[]=[]', { 'a[]': [ '[]', '[]' ], a: [ '[]', '[]' ] }); - expectQueries('a=b&a=c', { a: [ 'b', 'c' ] }); - expectQueries('a=&a=c', { a: [ '', 'c' ] }); - expectQueries('a=c&a=b', { a: [ 'c', 'b' ] }); - expectQueries('a=c&a=b&b=bb', { a: [ 'c', 'b' ], b: [ 'bb' ] }); - expectQueries('a[=c&a[=b', { 'a[': [ 'c', 'b' ] }); - expectQueries('a{=c&a{=b', { 'a{': [ 'c', 'b' ] }); - expectQueries('a[]=c&a[]=b', { 'a[]': [ 'c', 'b' ], a: [ 'c', 'b' ] }); - expectQueries('a[]=&a[]=b', { 'a[]': [ '', 'b' ], a: [ '', 'b' ] }); - expectQueries('a[]=&a[]=b&a=foo', { 'a[]': [ '', 'b' ], a: [ 'foo' ] }); - expectQueries('a=bar&a[]=&a[]=b&a=foo', { 'a[]': [ '', 'b' ], a: [ 'bar', 'foo' ] }); - - // a[][] 这种不支持自动变化为 a - expectQueries('a[][]=&a[][]=b', { 'a[][]': [ '', 'b' ] }); - expectQueries('a][]=&a][]=b', { 'a][]': [ '', 'b' ] }); - expectQueries('a[[]=&a[[]=b', { 'a[[]': [ '', 'b' ] }); - expectQueries('[]=&[]=b', { '[]': [ '', 'b' ] }); - - // a[], a 混搭的时候,只返回最后一个 a 的值 - expectQueries('a[]=a&a=b&a=c', { 'a[]': [ 'a' ], a: [ 'b', 'c' ] }); - - // object - expectQueries('a[foo]=c', { 'a[foo]': [ 'c' ] }); - expectQueries('a[foo]=c&a=b', { 'a[foo]': [ 'c' ], a: [ 'b' ] }); - expectQueries('a[foo]=c&a=b&b=bb&d=d1&d=d2', { - 'a[foo]': [ 'c' ], - a: [ 'b' ], - b: [ 'bb' ], - d: [ 'd1', 'd2' ], - }); - expectQueries('a[foo]=c&a[]=b&a[]=d', { - 'a[foo]': [ 'c' ], - 'a[]': [ 'b', 'd' ], - a: [ 'b', 'd' ], - }); - expectQueries('a[foo]=c&a[]=b&a[]=d&c=cc&c=c2&c=', { - 'a[foo]': [ 'c' ], - 'a[]': [ 'b', 'd' ], - a: [ 'b', 'd' ], - c: [ 'cc', 'c2', '' ], + describe('this.query[key] => String', () => { + function expectQuery(querystring, query) { + mm(req, 'querystring', querystring); + req.query.should.eql(query); + mm.restore(); + } + + it('should get string value', () => { + expectQuery('a=b', { a: 'b' }); + expectQuery('a=&', { a: '' }); + expectQuery('a=b&', { a: 'b' }); + expectQuery('a.=b', { 'a.': 'b' }); + expectQuery('a=b&a=c', { a: 'b' }); + expectQuery('a=&a=c', { a: '' }); + expectQuery('a=c&a=b', { a: 'c' }); + expectQuery('a=c&a=b&b=bb', { a: 'c', b: 'bb' }); + expectQuery('a[=c&a[=b', { 'a[': 'c' }); + expectQuery('a{=c&a{=b', { 'a{': 'c' }); + expectQuery('a[]=c&a[]=b', { 'a[]': 'c' }); + expectQuery('a[]=&a[]=b', { 'a[]': '' }); + expectQuery('a[foo]=c', { 'a[foo]': 'c' }); + expectQuery('a[foo][bar]=c', { 'a[foo][bar]': 'c' }); + expectQuery('a=', { a: '' }); + expectQuery('a[]=a&a=b&a=c', { 'a[]': 'a', a: 'b' }); }); - expectQueries('a[foo][bar]=c', { - 'a[foo][bar]': [ 'c' ], + + it('should get undefined when key not exists', () => { + expectQuery('a=b', { a: 'b' }); }); }); - it('should get undefined when key not exists', () => { - expectQueries('a=b', { a: [ 'b' ] }); - }); - }); + describe('this.queries[key] => Array', () => { + function expectQueries(querystring, query) { + mm(req, 'querystring', querystring); + req.queries.should.eql(query); + mm.restore(); + } + + it('should get array value', () => { + expectQueries('', { }); + expectQueries('a=', { a: [ '' ] }); + expectQueries('a=&', { a: [ '' ] }); + expectQueries('a=b&', { a: [ 'b' ] }); + expectQueries('a.=', { 'a.': [ '' ] }); + expectQueries('a=&a=&a=&a=&a=&a=&a=&a=', { a: [ '', '', '', '', '', '', '', '' ] }); + expectQueries('a=&a=&a=&a=&a=&a=&a=&a=&', { a: [ '', '', '', '', '', '', '', '' ] }); + expectQueries('a=&a=&a=&a=&a=&a=&a=&a=&&&&', { a: [ '', '', '', '', '', '', '', '' ] }); + expectQueries('a=b', { a: [ 'b' ] }); + expectQueries('a={}', { a: [ '{}' ] }); + expectQueries('a=[]', { a: [ '[]' ] }); + expectQueries('a[]=[]', { 'a[]': [ '[]' ], a: [ '[]' ] }); + expectQueries('a[]=&a[]=', { 'a[]': [ '', '' ], a: [ '', '' ] }); + expectQueries('a[]=[]&a[]=[]', { 'a[]': [ '[]', '[]' ], a: [ '[]', '[]' ] }); + expectQueries('a=b&a=c', { a: [ 'b', 'c' ] }); + expectQueries('a=&a=c', { a: [ '', 'c' ] }); + expectQueries('a=c&a=b', { a: [ 'c', 'b' ] }); + expectQueries('a=c&a=b&b=bb', { a: [ 'c', 'b' ], b: [ 'bb' ] }); + expectQueries('a[=c&a[=b', { 'a[': [ 'c', 'b' ] }); + expectQueries('a{=c&a{=b', { 'a{': [ 'c', 'b' ] }); + expectQueries('a[]=c&a[]=b', { 'a[]': [ 'c', 'b' ], a: [ 'c', 'b' ] }); + expectQueries('a[]=&a[]=b', { 'a[]': [ '', 'b' ], a: [ '', 'b' ] }); + expectQueries('a[]=&a[]=b&a=foo', { 'a[]': [ '', 'b' ], a: [ 'foo' ] }); + expectQueries('a=bar&a[]=&a[]=b&a=foo', { 'a[]': [ '', 'b' ], a: [ 'bar', 'foo' ] }); + + // a[][] 这种不支持自动变化为 a + expectQueries('a[][]=&a[][]=b', { 'a[][]': [ '', 'b' ] }); + expectQueries('a][]=&a][]=b', { 'a][]': [ '', 'b' ] }); + expectQueries('a[[]=&a[[]=b', { 'a[[]': [ '', 'b' ] }); + expectQueries('[]=&[]=b', { '[]': [ '', 'b' ] }); + + // a[], a 混搭的时候,只返回最后一个 a 的值 + expectQueries('a[]=a&a=b&a=c', { 'a[]': [ 'a' ], a: [ 'b', 'c' ] }); + + // object + expectQueries('a[foo]=c', { 'a[foo]': [ 'c' ] }); + expectQueries('a[foo]=c&a=b', { 'a[foo]': [ 'c' ], a: [ 'b' ] }); + expectQueries('a[foo]=c&a=b&b=bb&d=d1&d=d2', { + 'a[foo]': [ 'c' ], + a: [ 'b' ], + b: [ 'bb' ], + d: [ 'd1', 'd2' ], + }); + expectQueries('a[foo]=c&a[]=b&a[]=d', { + 'a[foo]': [ 'c' ], + 'a[]': [ 'b', 'd' ], + a: [ 'b', 'd' ], + }); + expectQueries('a[foo]=c&a[]=b&a[]=d&c=cc&c=c2&c=', { + 'a[foo]': [ 'c' ], + 'a[]': [ 'b', 'd' ], + a: [ 'b', 'd' ], + c: [ 'cc', 'c2', '' ], + }); + expectQueries('a[foo][bar]=c', { + 'a[foo][bar]': [ 'c' ], + }); + }); - describe('this.query = obj', () => { - it('should set query with object', () => { - mm(req, 'querystring', 'a=c'); - req.query.should.eql({ a: 'c' }); - req.query = {}; - req.query.should.eql({}); - req.querystring.should.equal(''); - - req.query = { foo: 'bar' }; - req.query.should.eql({ foo: 'bar' }); - req.querystring.should.equal('foo=bar'); - - req.query = { array: [ 1, 2 ] }; - req.query.should.eql({ array: '1' }); - req.querystring.should.equal('array=1&array=2'); + it('should get undefined when key not exists', () => { + expectQueries('a=b', { a: [ 'b' ] }); + }); }); - }); - describe('request.acceptJSON', () => { - it('should true when isAjax', function* () { - mm(req.req.headers, 'x-requested-with', 'XMLHttpRequest'); - req.acceptJSON.should.equal(true); + describe('this.query = obj', () => { + it('should set query with object', () => { + mm(req, 'querystring', 'a=c'); + req.query.should.eql({ a: 'c' }); + req.query = {}; + req.query.should.eql({}); + req.querystring.should.equal(''); + + req.query = { foo: 'bar' }; + req.query.should.eql({ foo: 'bar' }); + req.querystring.should.equal('foo=bar'); + + req.query = { array: [ 1, 2 ] }; + req.query.should.eql({ array: '1' }); + req.querystring.should.equal('array=1&array=2'); + }); }); - it('should true when response is json', function* () { - const context = app.mockContext({ - headers: { - accept: 'text/html', - }, - url: '/', + describe('request.acceptJSON', () => { + it('should true when isAjax', function* () { + mm(req.req.headers, 'x-requested-with', 'XMLHttpRequest'); + req.acceptJSON.should.equal(true); }); - context.type = 'json'; - context.request.acceptJSON.should.equal(true); - }); - it('should true when accept json', function* () { - const context = app.mockContext({ - headers: { - accept: 'application/json', - }, - url: '/', + it('should true when response is json', function* () { + const context = app.mockContext({ + headers: { + accept: 'text/html', + }, + url: '/', + }); + context.type = 'json'; + context.request.acceptJSON.should.equal(true); }); - context.request.acceptJSON.should.equal(true); - }); - it('should false when do not accept json', function* () { - const context = app.mockContext({ - headers: { - accept: 'text/html', - }, - url: '/', + it('should true when accept json', function* () { + const context = app.mockContext({ + headers: { + accept: 'application/json', + }, + url: '/', + }); + context.request.acceptJSON.should.equal(true); + }); + + it('should false when do not accept json', function* () { + const context = app.mockContext({ + headers: { + accept: 'text/html', + }, + url: '/', + }); + const request = context.request; + request.acceptJSON.should.equal(false); }); - const request = context.request; - request.acceptJSON.should.equal(false); }); + }); describe('work with egg app', () => { let app; let host; - before(done => { + before(() => { app = utils.app('apps/querystring-extended'); + return app.ready(); + }); + before(done => { app.listen(0, function() { host = `http://127.0.0.1:${this.address().port}`; done(); }); }); + after(() => app.close()); it('should return query and queries', done => { urllib.request(`${host}/?p=a,b&p=b,c&a[foo]=bar`, { diff --git a/test/fixtures/apps/watcher-development-app/app/router.js b/test/fixtures/apps/watcher-development-app/app/router.js index 7722b41bec..c9c483ea34 100644 --- a/test/fixtures/apps/watcher-development-app/app/router.js +++ b/test/fixtures/apps/watcher-development-app/app/router.js @@ -8,7 +8,7 @@ const dir_path = utils.getFilepath('apps/watcher-development-app/tmp'); module.exports = function(app) { let fileChangeCount = 0; - function callback() { + function callback(a) { fileChangeCount++; } diff --git a/test/lib/application.test.js b/test/lib/application.test.js index 73b2d79007..8ec88cd629 100644 --- a/test/lib/application.test.js +++ b/test/lib/application.test.js @@ -48,12 +48,13 @@ describe('test/lib/application.test.js', () => { describe('curl()', () => { afterEach(() => app.close()); it('should curl success', function* () { - this.timeout(10000); app = createApplication(); yield app.ready(); - const res = yield app.curl('https://a.alipayobjects.com/aliBridge/1.0.0/aliBridge.min.js'); + const res = yield app.curl('https://a.alipayobjects.com/aliBridge/1.0.0/aliBridge.min.js', { + timeout: 10000, + }); res.status.should.equal(200); - }); + }).timeout(10000); }); describe('dumpConfig()', () => { diff --git a/test/lib/core/logger.test.js b/test/lib/core/logger.test.js index 67e9e47cbd..71851b1ea5 100644 --- a/test/lib/core/logger.test.js +++ b/test/lib/core/logger.test.js @@ -7,6 +7,8 @@ const fs = require('fs'); const mm = require('egg-mock'); const request = require('supertest'); const Logger = require('egg-logger'); +const sleep = require('ko-sleep'); + const utils = require('../../utils'); const Agent = require('../../..').Agent; @@ -14,7 +16,8 @@ describe('test/lib/core/logger.test.js', () => { let app; afterEach(mm.restore); - afterEach(() => app.close()); + afterEach(() => sleep(5000).then(() => app.close())); + it('should got right default config on prod env', () => { mm.env('prod'); diff --git a/test/lib/core/urllib.test.js b/test/lib/core/urllib.test.js index f029d41603..5931d56827 100644 --- a/test/lib/core/urllib.test.js +++ b/test/lib/core/urllib.test.js @@ -23,6 +23,7 @@ describe('test/lib/core/urllib.test.js', () => { it('should request ok with log', done => { const args = { dataType: 'text', + timeout: 10000, }; client.once('response', info => { info.req.options.headers['mock-traceid'].should.equal('mock-traceid'); @@ -31,7 +32,7 @@ describe('test/lib/core/urllib.test.js', () => { }); client.request(url, args); - }); + }).timeout(10000); it('should request callback with log', done => { client.once('response', info => { diff --git a/test/lib/plugins/multipart.test.js b/test/lib/plugins/multipart.test.js index 8cf98d6b81..a535234a86 100644 --- a/test/lib/plugins/multipart.test.js +++ b/test/lib/plugins/multipart.test.js @@ -12,8 +12,11 @@ describe('test/lib/plugins/multipart.test.js', () => { let cookies; let host; let server; - before(done => { + before(() => { app = utils.app('apps/multipart'); + return app.ready(); + }); + before(done => { server = app.listen(); request(server) .get('/') diff --git a/test/lib/plugins/watcher.test.js b/test/lib/plugins/watcher.test.js index b74006a9af..061497fc60 100644 --- a/test/lib/plugins/watcher.test.js +++ b/test/lib/plugins/watcher.test.js @@ -4,142 +4,115 @@ require('should'); const mm = require('egg-mock'); const fs = require('fs'); const request = require('supertest'); +const sleep = require('ko-sleep'); const utils = require('../../utils'); const file_path1 = utils.getFilepath('apps/watcher-development-app/tmp.txt'); const file_path2 = utils.getFilepath('apps/watcher-development-app/tmp/tmp.txt'); const file_path1_agent = utils.getFilepath('apps/watcher-development-app/tmp-agent.txt'); describe('test/lib/plugins/watcher.test.js', () => { + describe('default', () => { let app; beforeEach(() => { app = utils.cluster('apps/watcher-development-app'); + app.coverage(false); return app.ready(); }); afterEach(() => app.close()); afterEach(mm.restore); - it('should app watcher work', done => { + it('should app watcher work', function* () { const server = app.callback(); let count = 0; - request(server) - .get('/app-watch') - .expect(200) - .expect('app watch success') - .end(function(err) { - if (err) { - return done(err); - } - fs.writeFileSync(file_path1, 'aaa'); - setTimeout(function() { - request(server) - .get('/app-msg') - .expect(200) - .expect(function(res) { - const lastCount = count; - count = parseInt(res.text); - count.should.greaterThan(lastCount); - }) - .end(function(err) { - if (err) { - return done(err); - } - fs.writeFileSync(file_path2, 'aaa'); - setTimeout(function() { - request(server) - .get('/app-msg') - .expect(200) - .expect(function(res) { - const lastCount = count; - count = parseInt(res.text); - count.should.greaterThan(lastCount); - }) - .end(function(err) { - if (err) { - return done(err); - } - request(server) - .get('/app-unwatch') - .expect(200) - .expect('app unwatch success') - .end(function(err) { - if (err) return done(err); - setTimeout(() => { - fs.writeFileSync(file_path2, 'aaa'); - fs.writeFileSync(file_path1, 'aaa'); - setTimeout(function() { - request(server) - .get('/app-msg') - .expect(200) - .expect(function(res) { - const lastCount = count; - count = parseInt(res.text); - count.should.equal(lastCount); - }) // unchanged - .end(done); - }, 100); - }, 100); - }); - - }); - }, 100); - }); - }, 100); - }); + + yield request(server) + .get('/app-watch') + .expect(200) + .expect('app watch success'); + + yield sleep(3000); + fs.writeFileSync(file_path1, 'aaa'); + yield sleep(3000); + + yield request(server) + .get('/app-msg') + .expect(200) + .expect(function(res) { + const lastCount = count; + count = parseInt(res.text); + count.should.greaterThan(lastCount); + }); + + fs.writeFileSync(file_path2, 'aaa'); + yield sleep(3000); + + yield request(server) + .get('/app-msg') + .expect(200) + .expect(function(res) { + const lastCount = count; + count = parseInt(res.text); + count.should.greaterThan(lastCount); + }); + + yield request(server) + .get('/app-unwatch') + .expect(200) + .expect('app unwatch success'); + + yield sleep(3000); + fs.writeFileSync(file_path2, 'aaa'); + fs.writeFileSync(file_path1, 'aaa'); + yield sleep(3000); + + // unchanged + yield request(server) + .get('/app-msg') + .expect(200) + .expect(function(res) { + const lastCount = count; + count = parseInt(res.text); + count.should.equal(lastCount); + }); }); - it('should agent watcher work', done => { + it('should agent watcher work', function* () { let count = 0; - request(app.callback()) - .get('/agent-watch') - .expect(200) - .expect('agent watch success') - .end(err => { - if (err) { - return done(err); - } - fs.writeFileSync(file_path1_agent, 'bbb'); - setTimeout(() => { - request(app.callback()) - .get('/agent-msg') - .expect(200) - .expect(res => { - const lastCount = count; - count = parseInt(res.text); - count.should.greaterThan(lastCount); - }) - .end(err => { - if (err) { - return done(err); - } - request(app.callback()) - .get('/agent-unwatch') - .expect(200) - .expect('agent unwatch success') - .end(err => { - if (err) { - return done(err); - } - - setTimeout(() => { - fs.writeFileSync(file_path1_agent, 'bbb'); - setTimeout(() => { - request(app.callback()) - .get('/agent-msg') - .expect(200) - .expect(res => { - const lastCount = count; - count = parseInt(res.text); - count.should.equal(lastCount); - }) - .end(done); - }, 100); - }, 100); - }); - }); - }, 100); - }); + yield request(app.callback()) + .get('/agent-watch') + .expect(200) + .expect('agent watch success'); + + fs.writeFileSync(file_path1_agent, 'bbb'); + yield sleep(3000); + + yield request(app.callback()) + .get('/agent-msg') + .expect(200) + .expect(res => { + const lastCount = count; + count = parseInt(res.text); + count.should.greaterThan(lastCount); + }); + yield request(app.callback()) + .get('/agent-unwatch') + .expect(200) + .expect('agent unwatch success'); + + yield sleep(3000); + fs.writeFileSync(file_path1_agent, 'bbb'); + yield sleep(3000); + + yield request(app.callback()) + .get('/agent-msg') + .expect(200) + .expect(res => { + const lastCount = count; + count = parseInt(res.text); + count.should.equal(lastCount); + }); }); }); @@ -148,18 +121,17 @@ describe('test/lib/plugins/watcher.test.js', () => { let app; before(() => { app = utils.cluster('apps/watcher-type-default'); + app.coverage(false); return app.ready(); }); after(() => app.close()); - it('should warn user', done => { - setTimeout(() => { - const content = fs.readFileSync( - utils.getFilepath('apps/watcher-type-default/logs/watcher-type-default/egg-agent.log')).toString(); - content.should.containEql('defaultEventSource watcher will NOT take effect'); - done(); - }, 1000); + it('should warn user', function* () { + yield sleep(3000); + const logPath = utils.getFilepath('apps/watcher-type-default/logs/watcher-type-default/egg-agent.log'); + const content = fs.readFileSync(logPath, 'utf8'); + content.should.containEql('defaultEventSource watcher will NOT take effect'); }); }); });