Skip to content

Instantly share code, notes, and snippets.

@omsmith
Created March 3, 2015 02:01
Show Gist options
  • Save omsmith/2951a53d01fce5a34192 to your computer and use it in GitHub Desktop.
Save omsmith/2951a53d01fce5a34192 to your computer and use it in GitHub Desktop.
koa-body-parser-issue-9
/home/omsmith/foo/foo.js:38
if (err) throw err
^
AssertionError: noBody server doesnt return body
at /home/omsmith/foo/foo.js:53:10
at Test.assert (/home/omsmith/foo/node_modules/supertest/lib/test.js:213:13)
at Server.assert (/home/omsmith/foo/node_modules/supertest/lib/test.js:132:12)
at Server.g (events.js:257:16)
at emitNone (events.js:67:13)
at Server.emit (events.js:163:7)
at net.js:1428:10
at process._tickCallback (node.js:337:11)
'use strict';
const
assert = require('assert'),
bodyParser = require('koa-body-parser'),
koa = require('koa'),
request = require('supertest');
function *requestHandler () {
this.type = 'application/json';
this.body = JSON.stringify(this.request.body);
}
const noBodyApp = koa()
.use(function *someProcess (next) {
yield new Promise(function (resolve) {
setTimeout(resolve, 1000);
});
yield* next;
})
.use(bodyParser())
.use(requestHandler);
const bodyApp = koa()
.use(bodyParser())
.use(function *someProcess (next) {
yield new Promise(function (resolve) {
setTimeout(resolve, 1000);
});
yield* next;
})
.use(requestHandler);
function throwTestErrs (err) {
if (err) throw err
}
request(require('http').createServer(bodyApp.callback()))
.post('/')
.send({ "hi": "there" })
.expect(function (res) {
assert.deepEqual(res.body, { "hi": "there" }, 'body server doesnt return body');
})
.end(throwTestErrs);
request(require('http').createServer(noBodyApp.callback()))
.post('/')
.send({ "hi": "there" })
.expect(function (res) {
assert.deepEqual(res.body, { "hi": "there" }, 'noBody server doesnt return body');
})
.end(throwTestErrs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment