-
-
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.
- Loading branch information
Showing
158 changed files
with
16,073 additions
and
5,477 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
version: 2 | ||
updates: | ||
- package-ecosystem: npm | ||
directory: "/" | ||
schedule: | ||
interval: daily | ||
open-pull-requests-limit: 5 | ||
versioning-strategy: increase-if-necessary |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Node.js CI | ||
|
||
on: | ||
push: | ||
branch: master | ||
pull_request: | ||
branch: master | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
strategy: | ||
matrix: | ||
node-version: [12.x, 14.x, 16.x] | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v1 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- run: npm ci | ||
- run: npm run lint | ||
- run: npm test -- --coverage --maxWorkers 2 | ||
- run: npx codecov |
This file was deleted.
Oops, something went wrong.
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
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
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,5 +1,5 @@ | ||
env: | ||
mocha: true | ||
jest: true | ||
|
||
rules: | ||
space-before-blocks: [2, {functions: never, keywords: always}] | ||
|
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
'use strict' | ||
|
||
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() | ||
|
||
it('should merge properties', () => { | ||
app1.use((ctx, next) => { | ||
assert.strictEqual(ctx.msg, 'hello') | ||
ctx.status = 204 | ||
}) | ||
|
||
return request(app1.listen()) | ||
.get('/') | ||
.expect(204) | ||
}) | ||
|
||
it('should not affect the original prototype', () => { | ||
app2.use((ctx, next) => { | ||
assert.strictEqual(ctx.msg, undefined) | ||
ctx.status = 204 | ||
}) | ||
|
||
return request(app2.listen()) | ||
.get('/') | ||
.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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
|
||
'use strict' | ||
|
||
const request = require('supertest') | ||
const assert = require('assert') | ||
const Koa = require('../..') | ||
|
||
describe('app', () => { | ||
it('should handle socket errors', done => { | ||
const app = new Koa() | ||
|
||
app.use((ctx, next) => { | ||
// triggers ctx.socket.writable == false | ||
ctx.socket.emit('error', new Error('boom')) | ||
}) | ||
|
||
app.on('error', err => { | ||
assert.strictEqual(err.message, 'boom') | ||
done() | ||
}) | ||
|
||
request(app.callback()) | ||
.get('/') | ||
.end(() => {}) | ||
}) | ||
|
||
it('should not .writeHead when !socket.writable', done => { | ||
const app = new Koa() | ||
|
||
app.use((ctx, next) => { | ||
// set .writable to false | ||
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') | ||
} | ||
}) | ||
|
||
// hackish, but the response should occur in a single tick | ||
setImmediate(done) | ||
|
||
request(app.callback()) | ||
.get('/') | ||
.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') | ||
}) | ||
|
||
it('should set env from the constructor', () => { | ||
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) | ||
}) | ||
|
||
it('should set signed cookie keys from the constructor', () => { | ||
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) | ||
}) | ||
|
||
it('should have a static property exporting `HttpError` from http-errors library', () => { | ||
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) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
'use strict' | ||
|
||
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) | ||
}) | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
|
||
'use strict' | ||
|
||
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() | ||
|
||
assert.throws(() => { | ||
app.onerror('foo') | ||
}, TypeError, 'non-error thrown: foo') | ||
}) | ||
|
||
it('should accept errors coming from other scopes', () => { | ||
const ExternError = require('vm').runInNewContext('Error') | ||
|
||
const app = new Koa() | ||
const error = Object.assign(new ExternError('boom'), { | ||
status: 418, | ||
expose: true | ||
}) | ||
|
||
assert.doesNotThrow(() => app.onerror(error)) | ||
}) | ||
|
||
it('should do nothing if status is 404', () => { | ||
const app = new Koa() | ||
const err = new Error() | ||
|
||
err.status = 404 | ||
|
||
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 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() | ||
}) | ||
}) |
Oops, something went wrong.