-
-
Notifications
You must be signed in to change notification settings - Fork 10
connect: サンプルの実装 #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
connect: サンプルの実装 #44
Changes from 6 commits
4fbcc67
8f6b720
f1b061c
16c2d23
0d74a5d
834cfa6
956c796
8556137
ff07629
e1e7e44
95025c6
506bad7
629ba42
e51c4a9
c1bac2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,8 @@ | |
"semi": [ | ||
2, | ||
"always" | ||
] | ||
], | ||
"no-console": 0 | ||
}, | ||
"env": { | ||
"es6": true, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
{ | ||
"rules": { | ||
"no-undef": 0, | ||
"no-unused-vars": 0, | ||
"no-console": 0 | ||
"no-unused-vars": 0 | ||
}, | ||
"plugins": [ | ||
"markdown" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"use strict"; | ||
import hello from "./hello"; | ||
import nosniff from "./nosniff"; | ||
import assert from "assert"; | ||
import connect from "connect"; | ||
import http from "http"; | ||
import fetch from "node-fetch"; | ||
const responseText = "response text"; | ||
var app = connect(); | ||
app.use(nosniff()); | ||
app.use(hello(responseText)); | ||
|
||
var server = http.createServer(app).listen(3000, () => { | ||
fetch("http://localhost:3000") | ||
.then(res => res.text()) | ||
.then(text => { | ||
assert.equal(text, responseText); | ||
server.close(); | ||
}).catch(console.error.bind(console)); | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// LICENSE : MIT | ||
"use strict"; | ||
export default function (text) { | ||
return function (req, res) { | ||
res.end(text); | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use strict"; | ||
function setHeaders(res, headers) { | ||
Object.keys(headers).forEach(key => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. これES6だともっとキレイに書けるような気がする… |
||
let value = headers[key]; | ||
if (value !== null) { | ||
res.setHeader(key, value); | ||
} | ||
}); | ||
} | ||
export default function () { | ||
return function (req, res, next) { | ||
setHeaders(res, { | ||
"X-Content-Type-Options": "nosniff" | ||
}); | ||
next(); | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// LICENSE : MIT | ||
"use strict"; | ||
import assert from "power-assert"; | ||
import connect from "connect"; | ||
import nosniff from "../../src/connect/nosniff"; | ||
import hello from "../../src/connect/hello"; | ||
import http from "http"; | ||
import fetch from "node-fetch"; | ||
describe("connect", function () { | ||
var responseText = "test"; | ||
var server; | ||
before(function (done) { | ||
var app = connect(); | ||
app.use(nosniff()); | ||
app.use(hello(responseText)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. これ順番変えたらどうなるんだろ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 普通にhelloでendが呼ばれてnosniffされなかった |
||
server = http.createServer(app).listen(3000, done); | ||
}); | ||
after(function () { | ||
server.close(); | ||
}); | ||
describe("hello", function () { | ||
it("should return response text", function () { | ||
return fetch("http://localhost:3000") | ||
.then(res => res.text()) | ||
.then(text => { | ||
assert.equal(text, responseText); | ||
}); | ||
}); | ||
}); | ||
describe("sniff", function () { | ||
it("should return response has `X-Content-Type-Options` header", function () { | ||
return fetch("http://localhost:3000") | ||
.then(res => { | ||
assert.equal(res.headers.get("x-content-type-options"), "nosniff"); | ||
}); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var
まざってるの何か統一したいなThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#45 issueを立てた