-
Notifications
You must be signed in to change notification settings - Fork 744
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4d8c84
commit d26e3ef
Showing
4 changed files
with
86 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var auth = require('koa-basic-auth'); | ||
var koa = require('koa'); | ||
var app = koa(); | ||
|
||
// custom 401 handling | ||
|
||
app.use(function *(next){ | ||
try { | ||
yield next; | ||
} catch (err) { | ||
if (401 == err.status) { | ||
this.status = 401; | ||
this.body = 'cant haz that'; | ||
} else { | ||
throw err; | ||
} | ||
} | ||
}); | ||
|
||
// require auth | ||
|
||
app.use(auth({ name: 'tj', pass: 'tobi' })); | ||
|
||
// secret response | ||
|
||
app.use(function *(){ | ||
this.body = 'secret'; | ||
}); | ||
|
||
app.listen(3000); | ||
console.log('listening on port 3000'); |
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,52 @@ | ||
var basicAuth = require('koa-basic-auth'); | ||
var request = require('supertest'); | ||
var koa = require('koa'); | ||
var assert = require('assert'); | ||
|
||
describe('Koa Basic Auth', function(){ | ||
|
||
describe('with no credentials', function(){ | ||
it('should `throw` 401', function(done){ | ||
var app = koa(); | ||
|
||
app.use(basicAuth({ name: 'user', pass: 'pass' })); | ||
|
||
request(app.listen()) | ||
.get('/') | ||
.expect(401) | ||
.end(done); | ||
}) | ||
}) | ||
|
||
describe('with invalid credentials', function(){ | ||
it('should `throw` 401', function(done){ | ||
var app = koa(); | ||
|
||
app.use(basicAuth({ name: 'user', pass: 'pass' })); | ||
|
||
request(app.listen()) | ||
.get('/') | ||
.auth('foo', 'bar') | ||
.expect(401) | ||
.end(done); | ||
}) | ||
}) | ||
|
||
describe('with valid credentials', function(){ | ||
it('should call the next middleware', function(done){ | ||
var app = koa(); | ||
|
||
app.use(basicAuth({ name: 'user', pass: 'pass' })); | ||
app.use(function *(){ | ||
this.body = 'Protected'; | ||
}) | ||
|
||
request(app.listen()) | ||
.get('/') | ||
.auth('user', 'pass') | ||
.expect(200) | ||
.expect('Protected') | ||
.end(done); | ||
}) | ||
}) | ||
}) |
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