Skip to content

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

Merged
merged 15 commits into from
Sep 12, 2015
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"semi": [
2,
"always"
]
],
"no-console": 0
},
"env": {
"es6": true,
Expand Down
3 changes: 1 addition & 2 deletions .md.eslintrc
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"
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
],
"devDependencies": {
"babel": "^5.8.23",
"connect": "^3.4.0",
"coveralls": "^2.11.4",
"eslint": "^1.3.0",
"eslint-plugin-markdown": "git://github.com/eslint/eslint-plugin-markdown.git",
Expand All @@ -46,6 +47,7 @@
"gitbook-summary-to-path": "^1.0.1",
"jsdom": "^6.3.0",
"mocha": "^2.2.5",
"node-fetch": "^1.3.2",
"npm-run-all": "^1.2.8",
"power-assert": "^1.0.0",
"punctuate-coverage": "^1.0.3",
Expand Down
22 changes: 22 additions & 0 deletions src/connect/connect-example.js
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();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

varまざってるの何か統一したいな

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#45 issueを立てた

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));
});


7 changes: 7 additions & 0 deletions src/connect/hello.js
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);
};
}
17 changes: 17 additions & 0 deletions src/connect/nosniff.js
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 => {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これES6だともっとキレイに書けるような気がする…
for of以外に何かあったりするかなー(for ofはtranspileにやさしくない

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();
};
}
38 changes: 38 additions & 0 deletions test/connect/hello-test.js
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));
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これ順番変えたらどうなるんだろ?

Copy link
Owner Author

Choose a reason for hiding this comment

The 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");
});
});
});
});