Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
Change default status to 404 without affecting Koa default semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
lhorie authored and fusion-bot[bot] committed Jan 26, 2018
1 parent bf92cef commit ad9f34b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
58 changes: 58 additions & 0 deletions src/__tests__/index.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/** Copyright (c) 2018 Uber Technologies, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import test from 'tape-cup';
import App from 'fusion-core';

import {getSimulator} from '../index.js';

test('status is 404 if ctx.body is never updated', async t => {
const app = new App('el', el => el);
const ctx = await getSimulator(app).request('/');
t.equals(ctx.status, 404, 'status defaults to 404');
t.end();
});

test('status is 200 if ctx.body is updated in request', async t => {
const app = new App('el', el => el);
app.middleware((ctx, next) => {
ctx.body = {ok: 1};
return next();
});
const ctx = await getSimulator(app).request('/');
t.equals(ctx.status, 200, 'status defaults to 200');
t.end();
});

test('status is set if ctx.status is updated in request', async t => {
const app = new App('el', () => 'hello');
app.middleware((ctx, next) => {
ctx.status = 500;
ctx.body = {error: 'error'};
return next();
});
const ctx = await getSimulator(app).render('/');
t.equals(ctx.status, 500, 'status is set');
t.end();
});

test('status is 200 if ctx.body is updated in render', async t => {
const app = new App('el', () => 'hello');
const ctx = await getSimulator(app).render('/');
t.equals(ctx.status, 200, 'status defaults to 200');
t.end();
});

test('status is set if ctx.status is updated in render', async t => {
const app = new App('el', () => 'hello');
app.middleware((ctx, next) => {
ctx.status = 500;
return next();
});
const ctx = await getSimulator(app).render('/');
t.equals(ctx.status, 500, 'status is set');
t.end();
});
9 changes: 4 additions & 5 deletions src/mock-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,16 @@ export function mockContext(url: string, options: *): Context {
//$FlowFixMe
req = Object.assign({headers: {}, socket}, Stream.Readable.prototype, req);
//$FlowFixMe
res = Object.assign({_headers: {}, socket}, Stream.Writable.prototype, res);
res = Object.assign({_headers: {}, socket}, Stream.Writable.prototype, res, {
statusCode: 404,
});
req.socket.remoteAddress = req.socket.remoteAddress || '127.0.0.1';
res.getHeader = k => res._headers[k.toLowerCase()];
res.setHeader = (k, v) => (res._headers[k.toLowerCase()] = v);
res.removeHeader = k => delete res._headers[k.toLowerCase()];

const app = new Koa();
//$FlowFixMe
const ctx = app.createContext(req, res);
ctx.status = 404;
return ctx;
return new Koa().createContext(req, res);
}

export function renderContext(url: string, options: any): Context {
Expand Down

0 comments on commit ad9f34b

Please sign in to comment.