forked from stephenmathieson/node-parse-bearer-token
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
67 lines (61 loc) · 1.67 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var express = require('express');
var http = require('http');
var request = require('supertest');
var parse = require('./');
describe('parse-bearer-token', function () {
[
['express', function createExpressApp() {
var app = express();
app.get('/', function (req, res) {
var token = parse(req);
res.status(200).send(String(token));
});
return app;
}],
['http', function createNativeApp() {
var app = http.createServer(function (req, res) {
var token = parse(req);
res.writeHead(200, { 'content-type': 'text/plain' });
res.end(String(token));
});
return app;
}],
].forEach(function (map) {
var name = map[0];
var factory = map[1];
describe('given an ' + name + ' app', function () {
var app;
before(function () {
app = factory();
});
it('should parse the bearer token', function (done) {
request(app)
.get('/')
.set('authorization', 'bearer thetoken')
.expect('thetoken')
.end(done);
});
it('should return null with no authorization header', function (done) {
request(app)
.get('/')
.expect('null')
.end(done);
});
it('should return null with a malformed bearer', function (done) {
request(app)
.get('/')
.set('authorization', 'bearer')
.expect('null')
.end(done);
});
it('should return null on non-bearer auth', function (done) {
request(app)
.get('/')
// foo:bar
.set('authorization', 'basic Zm9vOmJhcg==')
.expect('null')
.end(done);
});
});
});
});