Skip to content

Commit

Permalink
Merge pull request #93 from bananaappletw/master
Browse files Browse the repository at this point in the history
Add eslint
  • Loading branch information
hemanth authored Oct 20, 2016
2 parents 829c2f5 + 76f5551 commit 5d8404f
Show file tree
Hide file tree
Showing 46 changed files with 285 additions and 259 deletions.
15 changes: 15 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
env:
mocha: true
extends: standard
plugins:
- standard
- promise
rules:
arrow-parens: 0
eqeqeq: 0
no-return-assign: 0 # fails for arrow functions
semi: [2, always]
space-before-function-paren: [2, never]
yoda: 0
arrow-spacing: 2
dot-location: [2, "property"]
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
node_js:
- "4"
- "6"
- "4"
- "6"
language: node_js
script:
make lint
4 changes: 2 additions & 2 deletions 404/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var koa = require('koa');

var app = module.exports = koa();

app.use(function *pageNotFound(next){
app.use(function *pageNotFound(next) {
yield next;

if (404 != this.status) return;
Expand All @@ -25,6 +25,6 @@ app.use(function *pageNotFound(next){
this.type = 'text';
this.body = 'Page Not Found';
}
})
});

if (!module.parent) app.listen(3000);
12 changes: 6 additions & 6 deletions 404/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
var app = require('./app');
var request = require('supertest').agent(app.listen());

describe('404', function(){
describe('when GET /', function(){
it('should return the 404 page', function(done){
describe('404', function() {
describe('when GET /', function() {
it('should return the 404 page', function(done) {
request
.get('/')
.expect(404)
.expect(/Page Not Found/, done);
})
})
})
});
});
});
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
lint:
@./node_modules/.bin/eslint .

test:
@NODE_ENV=test ./node_modules/.bin/mocha \
--harmony \
--reporter spec \
--require should \
*/test.js

.PHONY: test
.PHONY: lint test
7 changes: 7 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

A repository containing small examples to illustrate the use of Koa
for creating web applications and other HTTP servers.

# Running tests

```bash
make test
make lint
```

## Included Examples

Expand Down
4 changes: 2 additions & 2 deletions base-auth/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var app = module.exports = koa();

// custom 401 handling

app.use(function* (next){
app.use(function* (next) {
try {
yield next;
} catch (err) {
Expand All @@ -24,7 +24,7 @@ app.use(auth({ name: 'tj', pass: 'tobi' }));

// secret response

app.use(function* (){
app.use(function* () {
this.body = 'secret';
});

Expand Down
28 changes: 14 additions & 14 deletions base-auth/test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
var app = require('./app');
var request = require('supertest').agent(app.listen());

describe('Koa Basic Auth', function(){
describe('with no credentials', function(){
it('should `throw` 401', function(done){
describe('Koa Basic Auth', function() {
describe('with no credentials', function() {
it('should `throw` 401', function(done) {
request
.get('/')
.expect(401, done);
})
})
});
});

describe('with invalid credentials', function(){
it('should `throw` 401', function(done){
describe('with invalid credentials', function() {
it('should `throw` 401', function(done) {
request
.get('/')
.auth('user', 'invalid password')
.expect(401, done);
})
})
});
});

describe('with valid credentials', function(){
it('should call the next middleware', function(done){
describe('with valid credentials', function() {
it('should call the next middleware', function(done) {
request
.get('/')
.auth('tj', 'tobi')
.expect(200)
.expect('secret', done);
})
})
})
});
});
});
2 changes: 1 addition & 1 deletion blog/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function *show(id) {
function *create() {
var post = yield parse(this);
var id = posts.push(post) - 1;
post.created_at = new Date;
post.created_at = new Date();
post.id = id;
this.redirect('/');
}
Expand Down
5 changes: 3 additions & 2 deletions blog/lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
*/

var views = require('co-views');
var path = require('path');

// setup views mapping .html
// to the swig template engine

module.exports = views(__dirname + '/../views', {
module.exports = views(path.join(__dirname, '/../views'), {
map: { html: 'swig' }
});
});
30 changes: 15 additions & 15 deletions blog/test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
var app = require('./app');
var request = require('supertest').agent(app.listen());

describe('Blog', function(){
describe('GET /',function(){
it('should see title "Posts"', function(done){
describe('Blog', function() {
describe('GET /', function() {
it('should see title "Posts"', function(done) {
request
.get('/')
.expect(200, function(err, res){
.expect(200, function(err, res) {
if (err) return done(err);

res.should.be.html;
res.text.should.include('<title>Posts</title>');
done();
});
});
it('should see 0 post', function(done){
it('should see 0 post', function(done) {
request
.get('/')
.expect(200, function(err, res){
.expect(200, function(err, res) {
if (err) return done(err);

res.should.be.html;
Expand All @@ -26,29 +26,29 @@ describe('Blog', function(){
});
});
});
describe('POST /post/new',function(){
it('should create post and redirect to /', function(done){
describe('POST /post/new', function() {
it('should create post and redirect to /', function(done) {
request
.post('/post')
.send({title: 'Title', body: 'Contents'})
.end(function(err, res){
.end(function(err, res) {
if (err) return done(err);

res.header.location.should.be.equal('/')
res.header.location.should.be.equal('/');
done();
});
});
});
describe('GET /post/0',function(){
it('should see post', function(done){
describe('GET /post/0', function() {
it('should see post', function(done) {
request
.get('/post/0')
.expect(200, function(err, res){
.expect(200, function(err, res) {
if (err) return done(err);

res.should.be.html;
res.text.should.include('<h1>Title</h1>')
res.text.should.include('<p>Contents</p>')
res.text.should.include('<h1>Title</h1>');
res.text.should.include('<p>Contents</p>');
done();
});
});
Expand Down
2 changes: 1 addition & 1 deletion body-parsing/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var app = module.exports = koa();
// co-body accepts application/json
// and application/x-www-form-urlencoded

app.use(function *(next){
app.use(function *(next) {
if ('POST' != this.method) return yield next;
var body = yield parse(this, { limit: '1kb' });
if (!body.name) this.throw(400, '.name required');
Expand Down
37 changes: 18 additions & 19 deletions body-parsing/test.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
var app = require('./app');
var request = require('supertest').agent(app.listen());

describe('Body Parsing', function(){
describe('POST /uppercase', function(){
describe('with JSON', function(){
it('should work', function(done){
describe('Body Parsing', function() {
describe('POST /uppercase', function() {
describe('with JSON', function() {
it('should work', function(done) {
request
.post('/uppercase')
.send({ name: 'tobi' })
.expect(200)
.expect({ name: 'TOBI' }, done);
})
})
});
});

describe('with urlencoded', function(){
it('should work', function(done){
describe('with urlencoded', function() {
it('should work', function(done) {
request
.post('/uppercase')
.send('name=tj')
.expect(200)
.expect({ name: 'TJ' }, done);
})
})

});
});

describe('when length > limit', function(){
it('should 413', function(done){
describe('when length > limit', function() {
it('should 413', function(done) {
request
.post('/json')
.send({ name: Array(5000).join('a') })
.expect(413, done);
})
})
});
});

describe('when no name is sent', function(){
it('should 400', function(done){
describe('when no name is sent', function() {
it('should 400', function(done) {
request
.post('/uppsercase')
.send('age=10')
.expect(400, done);
});
});
})
})
});
});
17 changes: 8 additions & 9 deletions compose/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,33 @@
* ]))
*/


var compose = require('koa-compose');
var koa = require('koa');
var app = module.exports = koa();

// x-response-time

function *responseTime(next){
var start = new Date;
function *responseTime(next) {
var start = new Date();
yield next;
var ms = new Date - start;
var ms = new Date() - start;
this.set('X-Response-Time', ms + 'ms');
}

// logger

function* logger(next){
var start = new Date;
function* logger(next) {
var start = new Date();
yield next;
var ms = new Date - start;
var ms = new Date() - start;
if ('test' != process.env.NODE_ENV) {
console.log('%s %s - %s', this.method, this.url, ms);
}
}

// response

function* respond(next){
function* respond(next) {
yield next;
if ('/' != this.url) return;
this.body = 'Hello World';
Expand All @@ -55,4 +54,4 @@ var all = compose([

app.use(all);

if (!module.parent) app.listen(3000);
if (!module.parent) app.listen(3000);
Loading

0 comments on commit 5d8404f

Please sign in to comment.