Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7ee849c

Browse files
🔫 add tests for handler
1 parent 75248d9 commit 7ee849c

File tree

6 files changed

+306
-8
lines changed

6 files changed

+306
-8
lines changed

‎build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ set -o xtrace
1414
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
1515

1616
docker-compose build dev
17+
docker-compose run --rm dev npm test
1718
docker-compose run --rm dev npm run build

‎module/functions/basic-auth.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"build": "npx gulp",
77
"clean": "npx gulp clean",
8-
"test": "echo \"Error: no test specified\" && exit 1"
8+
"test": "npx mocha"
99
},
1010
"repository": {
1111
"type": "git",
@@ -20,11 +20,14 @@
2020
"devDependencies": {
2121
"@babel/core": "^7.2.2",
2222
"@babel/preset-env": "^7.3.1",
23+
"chai": "^4.2.0",
2324
"gulp": "^4.0.0",
2425
"gulp-babel": "^8.0.0",
2526
"gulp-cli": "^2.0.1",
2627
"gulp-concat": "^2.6.1",
2728
"gulp-uglify": "^3.0.1",
28-
"rimraf": "^2.6.3"
29+
"mocha": "^5.2.0",
30+
"rimraf": "^2.6.3",
31+
"sinon": "^7.2.3"
2932
}
3033
}

‎src/basic-auth.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ exports.handler = (event, context, callback) => {
2222
'www-authenticate': [
2323
{
2424
key: 'WWW-Authenticate',
25-
value:'Basic',
25+
value:'Basic',
2626
}
2727
]
2828
},
2929
}
3030

3131
callback(null, response)
32+
return
3233
}
3334

3435
// Continue request processing if authentication passed

‎test/basic-auth.test.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
const expect = require('chai').expect
2+
const sinon = require('sinon')
3+
const { handler } = require('../src/basic-auth')
4+
5+
describe('Basic Auth', function() {
6+
describe('handler', function () {
7+
it('should return 401 and WWW-Authenticate: Basic header without Authorization header', function() {
8+
const event = {
9+
Records: [
10+
{
11+
cf: {
12+
request: {
13+
headers: {
14+
},
15+
},
16+
},
17+
},
18+
],
19+
}
20+
21+
const callback = sinon.fake()
22+
23+
handler(event, null, callback)
24+
25+
expect(callback.calledOnce).to.be.true
26+
27+
const [ err, response ] = callback.args[0]
28+
29+
expect(err).to.be.null
30+
expect(response).to.have.property('status', '401')
31+
expect(response).to.have.property('headers')
32+
33+
const { headers } = response
34+
35+
expect(headers).to.have.property('www-authenticate')
36+
37+
expect(headers['www-authenticate']).to.deep.equal([
38+
{
39+
key: 'WWW-Authenticate',
40+
value: 'Basic',
41+
},
42+
])
43+
})
44+
45+
it('should return 401 and WWW-Authenticate: Basic header if authentication failed', function() {
46+
const event = {
47+
Records: [
48+
{
49+
cf: {
50+
request: {
51+
headers: {
52+
authorization: [
53+
{
54+
// new Buffer('impossible:impossible').toString('base64')
55+
value: 'Basic aW1wb3NzaWJsZTppbXBvc3NpYmxl',
56+
},
57+
],
58+
},
59+
},
60+
},
61+
},
62+
],
63+
}
64+
65+
const callback = sinon.fake()
66+
67+
handler(event, null, callback)
68+
69+
expect(callback.calledOnce).to.be.true
70+
71+
const [ err, response ] = callback.args[0]
72+
73+
expect(err).to.be.null
74+
expect(response).to.have.property('status', '401')
75+
expect(response).to.have.property('headers')
76+
77+
const { headers } = response
78+
79+
expect(headers).to.have.property('www-authenticate')
80+
81+
expect(headers['www-authenticate']).to.deep.equal([
82+
{
83+
key: 'WWW-Authenticate',
84+
value: 'Basic',
85+
},
86+
])
87+
})
88+
89+
it('should return request if authentication succeeded', function() {
90+
const event = {
91+
Records: [
92+
{
93+
cf: {
94+
request: {
95+
headers: {
96+
authorization: [
97+
{
98+
// new Buffer('${user}:${password}').toString('base64')
99+
value: 'Basic JHt1c2VyfToke3Bhc3N3b3JkfQ==',
100+
},
101+
],
102+
},
103+
},
104+
},
105+
},
106+
],
107+
}
108+
109+
const callback = sinon.fake()
110+
111+
handler(event, null, callback)
112+
113+
expect(callback.calledOnce).to.be.true
114+
115+
const [ err, response ] = callback.args[0]
116+
117+
expect(err).to.be.null
118+
expect(response).to.deep.equal(event.Records[0].cf.request)
119+
})
120+
})
121+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /