-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from koajs/custom-accepts
Custom accepts
- Loading branch information
Showing
10 changed files
with
141 additions
and
29 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,3 +1,5 @@ | ||
language: node_js | ||
node_js: | ||
- '0.11' | ||
script: "make test-travis" | ||
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" |
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
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/**! | ||
* koa-onerror - test/accepts.test.js | ||
* | ||
* Copyright(c) fengmk2 and other contributors. | ||
* MIT Licensed | ||
* | ||
* Authors: | ||
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com) | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var fs = require('fs'); | ||
var koa = require('koa'); | ||
var request = require('supertest'); | ||
var pedding = require('pedding'); | ||
var onerror = require('..'); | ||
|
||
describe('accepts.test.js', function () { | ||
it('should return json response', function (done) { | ||
done = pedding(2, done); | ||
var app = koa(); | ||
app.on('error', function () {}); | ||
onerror(app, { | ||
accepts: function () { | ||
if (this.url.indexOf('.json') > 0) { | ||
return 'json'; | ||
} | ||
return 'text'; | ||
} | ||
}); | ||
app.use(commonError); | ||
|
||
request(app.callback()) | ||
.get('/user.json') | ||
.set('Accept', '*/*') | ||
.expect(500) | ||
.expect('Content-Type', 'application/json; charset=utf-8') | ||
.expect({ error: 'foo is not defined' }, done); | ||
|
||
request(app.callback()) | ||
.get('/user') | ||
.set('Accept', 'application/json') | ||
.expect(500) | ||
.expect('Content-Type', 'text/plain; charset=utf-8') | ||
.expect('foo is not defined', done); | ||
}); | ||
|
||
it('should redrect when accepts type not json', function (done) { | ||
var app = koa(); | ||
app.on('error', function () {}); | ||
onerror(app, { | ||
accepts: function () { | ||
if (this.url.indexOf('.json') > 0) { | ||
return 'json'; | ||
} | ||
return 'text'; | ||
}, | ||
redirect: 'http://foo.com/500.html' | ||
}); | ||
app.use(commonError); | ||
|
||
request(app.callback()) | ||
.get('/user') | ||
.set('Accept', '*/*') | ||
.expect('Content-Type', 'text/html; charset=utf-8') | ||
.expect('Location', 'http://foo.com/500.html') | ||
.expect('Redirecting to <a href="http://foo.com/500.html">http://foo.com/500.html</a>.') | ||
.expect(302, done); | ||
}); | ||
}); | ||
|
||
function* commonError() { | ||
foo(); | ||
} |
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
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