-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
76 changed files
with
3,277 additions
and
3,294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
extends: koa | ||
extends: standard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
|
||
'use strict'; | ||
'use strict' | ||
|
||
const request = require('supertest'); | ||
const assert = require('assert'); | ||
const Koa = require('../..'); | ||
const request = require('supertest') | ||
const assert = require('assert') | ||
const Koa = require('../..') | ||
|
||
describe('app.context', () => { | ||
const app1 = new Koa(); | ||
app1.context.msg = 'hello'; | ||
const app2 = new Koa(); | ||
const app1 = new Koa() | ||
app1.context.msg = 'hello' | ||
const app2 = new Koa() | ||
|
||
it('should merge properties', () => { | ||
app1.use((ctx, next) => { | ||
assert.strictEqual(ctx.msg, 'hello'); | ||
ctx.status = 204; | ||
}); | ||
assert.strictEqual(ctx.msg, 'hello') | ||
ctx.status = 204 | ||
}) | ||
|
||
return request(app1.listen()) | ||
.get('/') | ||
.expect(204); | ||
}); | ||
.expect(204) | ||
}) | ||
|
||
it('should not affect the original prototype', () => { | ||
app2.use((ctx, next) => { | ||
assert.strictEqual(ctx.msg, undefined); | ||
ctx.status = 204; | ||
}); | ||
assert.strictEqual(ctx.msg, undefined) | ||
ctx.status = 204 | ||
}) | ||
|
||
return request(app2.listen()) | ||
.get('/') | ||
.expect(204); | ||
}); | ||
}); | ||
.expect(204) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,88 @@ | ||
|
||
'use strict'; | ||
'use strict' | ||
|
||
const request = require('supertest'); | ||
const assert = require('assert'); | ||
const Koa = require('../..'); | ||
const request = require('supertest') | ||
const assert = require('assert') | ||
const Koa = require('../..') | ||
|
||
describe('app', () => { | ||
it('should handle socket errors', done => { | ||
const app = new Koa(); | ||
const app = new Koa() | ||
|
||
app.use((ctx, next) => { | ||
// triggers ctx.socket.writable == false | ||
ctx.socket.emit('error', new Error('boom')); | ||
}); | ||
ctx.socket.emit('error', new Error('boom')) | ||
}) | ||
|
||
app.on('error', err => { | ||
assert.strictEqual(err.message, 'boom'); | ||
done(); | ||
}); | ||
assert.strictEqual(err.message, 'boom') | ||
done() | ||
}) | ||
|
||
request(app.callback()) | ||
.get('/') | ||
.end(() => {}); | ||
}); | ||
.end(() => {}) | ||
}) | ||
|
||
it('should not .writeHead when !socket.writable', done => { | ||
const app = new Koa(); | ||
const app = new Koa() | ||
|
||
app.use((ctx, next) => { | ||
// set .writable to false | ||
ctx.socket.writable = false; | ||
ctx.status = 204; | ||
ctx.socket.writable = false | ||
ctx.status = 204 | ||
// throw if .writeHead or .end is called | ||
ctx.res.writeHead = | ||
ctx.res.end = () => { | ||
throw new Error('response sent'); | ||
}; | ||
}); | ||
throw new Error('response sent') | ||
} | ||
}) | ||
|
||
// hackish, but the response should occur in a single tick | ||
setImmediate(done); | ||
setImmediate(done) | ||
|
||
request(app.callback()) | ||
.get('/') | ||
.end(() => {}); | ||
}); | ||
.end(() => {}) | ||
}) | ||
|
||
it('should set development env when NODE_ENV missing', () => { | ||
const NODE_ENV = process.env.NODE_ENV; | ||
process.env.NODE_ENV = ''; | ||
const app = new Koa(); | ||
process.env.NODE_ENV = NODE_ENV; | ||
assert.strictEqual(app.env, 'development'); | ||
}); | ||
const NODE_ENV = process.env.NODE_ENV | ||
process.env.NODE_ENV = '' | ||
const app = new Koa() | ||
process.env.NODE_ENV = NODE_ENV | ||
assert.strictEqual(app.env, 'development') | ||
}) | ||
|
||
it('should set env from the constructor', () => { | ||
const env = 'custom'; | ||
const app = new Koa({ env }); | ||
assert.strictEqual(app.env, env); | ||
}); | ||
const env = 'custom' | ||
const app = new Koa({ env }) | ||
assert.strictEqual(app.env, env) | ||
}) | ||
|
||
it('should set proxy flag from the constructor', () => { | ||
const proxy = true; | ||
const app = new Koa({ proxy }); | ||
assert.strictEqual(app.proxy, proxy); | ||
}); | ||
const proxy = true | ||
const app = new Koa({ proxy }) | ||
assert.strictEqual(app.proxy, proxy) | ||
}) | ||
|
||
it('should set signed cookie keys from the constructor', () => { | ||
const keys = ['customkey']; | ||
const app = new Koa({ keys }); | ||
assert.strictEqual(app.keys, keys); | ||
}); | ||
const keys = ['customkey'] | ||
const app = new Koa({ keys }) | ||
assert.strictEqual(app.keys, keys) | ||
}) | ||
|
||
it('should set subdomainOffset from the constructor', () => { | ||
const subdomainOffset = 3; | ||
const app = new Koa({ subdomainOffset }); | ||
assert.strictEqual(app.subdomainOffset, subdomainOffset); | ||
}); | ||
const subdomainOffset = 3 | ||
const app = new Koa({ subdomainOffset }) | ||
assert.strictEqual(app.subdomainOffset, subdomainOffset) | ||
}) | ||
|
||
it('should have a static property exporting `HttpError` from http-errors library', () => { | ||
const CreateError = require('http-errors'); | ||
const CreateError = require('http-errors') | ||
|
||
assert.notEqual(Koa.HttpError, undefined); | ||
assert.deepStrictEqual(Koa.HttpError, CreateError.HttpError); | ||
assert.throws(() => { throw new CreateError(500, 'test error'); }, Koa.HttpError); | ||
}); | ||
}); | ||
assert.notEqual(Koa.HttpError, undefined) | ||
assert.deepStrictEqual(Koa.HttpError, CreateError.HttpError) | ||
assert.throws(() => { throw new CreateError(500, 'test error') }, Koa.HttpError) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
|
||
'use strict'; | ||
'use strict' | ||
|
||
const assert = require('assert'); | ||
const util = require('util'); | ||
const Koa = require('../..'); | ||
const app = new Koa(); | ||
const assert = require('assert') | ||
const util = require('util') | ||
const Koa = require('../..') | ||
const app = new Koa() | ||
|
||
describe('app.inspect()', () => { | ||
it('should work', () => { | ||
const str = util.inspect(app); | ||
assert.strictEqual("{ subdomainOffset: 2, proxy: false, env: 'test' }", str); | ||
}); | ||
const str = util.inspect(app) | ||
assert.strictEqual("{ subdomainOffset: 2, proxy: false, env: 'test' }", str) | ||
}) | ||
|
||
it('should return a json representation', () => { | ||
assert.deepStrictEqual( | ||
{ subdomainOffset: 2, proxy: false, env: 'test' }, | ||
app.inspect() | ||
); | ||
}); | ||
}); | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,63 @@ | ||
|
||
'use strict'; | ||
'use strict' | ||
|
||
const assert = require('assert'); | ||
const Koa = require('../..'); | ||
const assert = require('assert') | ||
const Koa = require('../..') | ||
|
||
describe('app.onerror(err)', () => { | ||
it('should throw an error if a non-error is given', () => { | ||
const app = new Koa(); | ||
const app = new Koa() | ||
|
||
assert.throws(() => { | ||
app.onerror('foo'); | ||
}, TypeError, 'non-error thrown: foo'); | ||
}); | ||
app.onerror('foo') | ||
}, TypeError, 'non-error thrown: foo') | ||
}) | ||
|
||
it('should accept errors coming from other scopes', () => { | ||
const ExternError = require('vm').runInNewContext('Error'); | ||
const ExternError = require('vm').runInNewContext('Error') | ||
|
||
const app = new Koa(); | ||
const app = new Koa() | ||
const error = Object.assign(new ExternError('boom'), { | ||
status: 418, | ||
expose: true | ||
}); | ||
}) | ||
|
||
assert.doesNotThrow(() => app.onerror(error)); | ||
}); | ||
assert.doesNotThrow(() => app.onerror(error)) | ||
}) | ||
|
||
it('should do nothing if status is 404', () => { | ||
const app = new Koa(); | ||
const err = new Error(); | ||
const app = new Koa() | ||
const err = new Error() | ||
|
||
err.status = 404; | ||
err.status = 404 | ||
|
||
const spy = jest.spyOn(console, 'error'); | ||
app.onerror(err); | ||
expect(spy).not.toHaveBeenCalled(); | ||
spy.mockRestore(); | ||
}); | ||
const spy = jest.spyOn(console, 'error') | ||
app.onerror(err) | ||
expect(spy).not.toHaveBeenCalled() | ||
spy.mockRestore() | ||
}) | ||
|
||
it('should do nothing if .silent', () => { | ||
const app = new Koa(); | ||
app.silent = true; | ||
const err = new Error(); | ||
const app = new Koa() | ||
app.silent = true | ||
const err = new Error() | ||
|
||
const spy = jest.spyOn(console, 'error'); | ||
app.onerror(err); | ||
expect(spy).not.toHaveBeenCalled(); | ||
spy.mockRestore(); | ||
}); | ||
const spy = jest.spyOn(console, 'error') | ||
app.onerror(err) | ||
expect(spy).not.toHaveBeenCalled() | ||
spy.mockRestore() | ||
}) | ||
|
||
it('should log the error to stderr', () => { | ||
const app = new Koa(); | ||
app.env = 'dev'; | ||
|
||
const err = new Error(); | ||
err.stack = 'Foo'; | ||
|
||
const spy = jest.spyOn(console, 'error'); | ||
app.onerror(err); | ||
expect(spy).toHaveBeenCalled(); | ||
spy.mockRestore(); | ||
}); | ||
}); | ||
const app = new Koa() | ||
app.env = 'dev' | ||
|
||
const err = new Error() | ||
err.stack = 'Foo' | ||
|
||
const spy = jest.spyOn(console, 'error') | ||
app.onerror(err) | ||
expect(spy).toHaveBeenCalled() | ||
spy.mockRestore() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
|
||
'use strict'; | ||
'use strict' | ||
|
||
const request = require('supertest'); | ||
const assert = require('assert'); | ||
const Koa = require('../..'); | ||
const request = require('supertest') | ||
const assert = require('assert') | ||
const Koa = require('../..') | ||
|
||
describe('app.request', () => { | ||
const app1 = new Koa(); | ||
app1.request.message = 'hello'; | ||
const app2 = new Koa(); | ||
const app1 = new Koa() | ||
app1.request.message = 'hello' | ||
const app2 = new Koa() | ||
|
||
it('should merge properties', () => { | ||
app1.use((ctx, next) => { | ||
assert.strictEqual(ctx.request.message, 'hello'); | ||
ctx.status = 204; | ||
}); | ||
assert.strictEqual(ctx.request.message, 'hello') | ||
ctx.status = 204 | ||
}) | ||
|
||
return request(app1.listen()) | ||
.get('/') | ||
.expect(204); | ||
}); | ||
.expect(204) | ||
}) | ||
|
||
it('should not affect the original prototype', () => { | ||
app2.use((ctx, next) => { | ||
assert.strictEqual(ctx.request.message, undefined); | ||
ctx.status = 204; | ||
}); | ||
assert.strictEqual(ctx.request.message, undefined) | ||
ctx.status = 204 | ||
}) | ||
|
||
return request(app2.listen()) | ||
.get('/') | ||
.expect(204); | ||
}); | ||
}); | ||
.expect(204) | ||
}) | ||
}) |
Oops, something went wrong.