Skip to content
This repository has been archived by the owner on Aug 31, 2024. It is now read-only.

WIP: Test tweaks #55

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions .eslintrc.yml

This file was deleted.

6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ language: node_js
node_js:
- 6
- 8
sudo: false
before_install: "npm install --global npm"
script: "npm run validate"
after_script: "npm install coveralls && cat ./coverage/lcov.info | coveralls"
before_install: npm install --global npm
after_script: coveralls < coverage/lcov.info
notifications:
webhooks:
urls:
Expand Down
8 changes: 4 additions & 4 deletions test/basic-auth.test.js → lib/basic-auth.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const koala = require('../lib');
const koala = require('.');
const request = require('supertest');

describe('Basic Auth', () => {
test('should return the value', done => {
test('should return the value', () => {
const app = koala();
app.use(function * (next) {
this.body = this.request.basicAuth;
});

request(app.listen())
return request(app.listen())
.get('/')
.auth('username', 'password')
.expect(200)
.expect({
name: 'username',
pass: 'password'
}, done);
});
});
});
89 changes: 37 additions & 52 deletions test/body-parsing.test.js → lib/body-parsing.test.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,70 @@
const koala = require('../lib');
const koala = require('.');
const request = require('supertest');
const http = require('http');

describe('Body Parsing', () => {
describe('.request.json()', () => {
test('should parse a json body', done => {
test('should parse a json body', () => {
const app = koala();
app.use(function * () {
this.body = yield * this.request.json();
});
request(app.listen())
return request(app.listen())
.post('/')
.send({
message: 'lol'
})
.expect(200)
.expect(/"message"/)
.expect(/"lol"/, done);
.expect(/"lol"/);
});

test('should throw on non-objects in strict mode', done => {
test('should throw on non-objects in strict mode', () => {
const app = koala();
app.use(function * () {
this.body = yield * this.request.json();
});
request(app.listen())
return request(app.listen())
.post('/')
.type('json')
.send('"lol"')
.expect(400, done);
.expect(400);
});

test('should not throw on non-objects in non-strict mode', done => {
test('should not throw on non-objects in non-strict mode', () => {
const app = koala();
app.jsonStrict = false;
app.use(function * () {
this.body = yield * this.request.json();
});
request(app.listen())
return request(app.listen())
.post('/')
.type('json')
.send('"lol"')
.expect(200)
.expect('lol', done);
.expect('lol');
});
});

describe('.request.urlencoded()', () => {
test('should parse a urlencoded body', done => {
test('should parse a urlencoded body', () => {
const app = koala();
app.use(function * () {
this.body = yield * this.request.urlencoded();
});
request(app.listen())
return request(app.listen())
.post('/')
.send('message=lol')
.expect(200)
.expect(/"message"/)
.expect(/"lol"/, done);
.expect(/"lol"/);
});

test('should not support nested query strings by default', done => {
test('should not support nested query strings by default', () => {
const app = koala();
app.use(function * () {
this.body = yield * this.request.urlencoded();
});
request(app.listen())
return request(app.listen())
.post('/')
.type('form')
.send({
Expand All @@ -74,17 +73,17 @@ describe('Body Parsing', () => {
}
})
.expect(200)
.expect(/something\[nested\]/, done);
.expect(/something\[nested\]/);
});

test('should support nested query strings with options.qs=true', done => {
test('should support nested query strings with options.qs=true', () => {
const app = koala({
qs: true
});
app.use(function * () {
this.body = yield * this.request.urlencoded();
});
request(app.listen())
return request(app.listen())
.post('/')
.type('form')
.send({
Expand All @@ -97,89 +96,75 @@ describe('Body Parsing', () => {
something: {
nested: 'true'
}
}, done);
});
});
});

describe('.request.text()', () => {
test('should get the raw text body', done => {
test('should get the raw text body', () => {
const app = koala();
app.use(function * () {
this.body = yield * this.request.text();
expect(typeof this.body).toBe('string');
});
request(app.listen())
return request(app.listen())
.post('/')
.send('message=lol')
.expect(200)
.expect('message=lol', done);
.expect('message=lol');
});

test('should throw if the body is too large', done => {
test('should throw if the body is too large', () => {
const app = koala();
app.use(function * () {
yield * this.request.text('1kb');
this.body = 204;
});
request(app.listen())
return request(app.listen())
.post('/')
.send(Buffer.alloc(2048))
.expect(413, done);
.expect(413);
});
});

describe('.request.buffer()', () => {
test('should get the raw buffer body', done => {
test('should get the raw buffer body', () => {
const app = koala();
app.use(function * () {
this.body = yield * this.request.buffer();
expect(Buffer.isBuffer(this.body)).toBeTruthy();
});
request(app.listen())
return request(app.listen())
.post('/')
.send('message=lol')
.expect(200)
.expect('message=lol', done);
.expect('message=lol');
});

test('should throw if the body is too large', done => {
test('should throw if the body is too large', () => {
const app = koala();
app.use(function * () {
yield * this.request.buffer('1kb');
this.body = 204;
});
request(app.listen())
return request(app.listen())
.post('/')
.send(Buffer.alloc(2048))
.expect(413, done);
.expect(413);
});
});

describe('Expect: 100-continue', () => {
test('should send 100-continue', done => {
test('should send 100-continue', () => {
const app = koala();
app.use(function * () {
this.body = yield * this.request.json();
});
app.listen(function() {
http.request({
port: this.address().port,
path: '/',
headers: {
'expect': '100-continue',
'content-type': 'application/json'
}
})
.once('continue', function() {
this.end(JSON.stringify({
message: 'lol'
}));
})
.once('response', res => {
done();
})
.once('error', done);
});
return request(app.listen())
.get('/')
.set('expect', '100-continue')
.set('content-type', 'application/json')
.send('message=lol');
});
});
});
Loading