Skip to content

Commit 2fd0912

Browse files
committed
More tests
Ref #121
1 parent 4ab2c5e commit 2fd0912

File tree

6 files changed

+150
-4
lines changed

6 files changed

+150
-4
lines changed

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
},
1414
"devDependencies": {
1515
"eslint": "^3.4.0",
16+
"express": "^4.14.0",
17+
"file-loader": "^0.9.0",
1618
"mocha": "^3.0.2",
17-
"should": "^11.1.0"
19+
"should": "^11.1.0",
20+
"supertest": "^2.0.0",
21+
"webpack": "^2.1.0-beta.22"
1822
},
1923
"license": "MIT",
2024
"engines": {
@@ -33,7 +37,7 @@
3337
"scripts": {
3438
"lint": "eslint *.js test",
3539
"pretest": "npm run -s lint",
36-
"test": "mocha --harmony --full-trace --check-leaks",
40+
"test": "mocha --full-trace --check-leaks",
3741
"beautify": "npm run lint -- --fix",
3842
"travis": "npm test && node middleware.js"
3943
}

test/GetFilenameFromUrl.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ describe("GetFilenameFromUrl", function() {
4040
publicPath: undefined,
4141
expected: "/b.js",
4242
}, {
43-
url: "/b.js",
43+
url: "/c.js",
4444
outputPath: undefined,
4545
publicPath: undefined,
46-
expected: "/b.js",
46+
expected: "/c.js",
4747
}, {
4848
url: "/more/complex/path.js",
4949
outputPath: "/a",

test/Server.test.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
var middleware = require("../middleware");
2+
var express = require("express");
3+
var webpack = require("webpack");
4+
var request = require("supertest");
5+
var webpackConfig = require("./fixtures/server-test/webpack.config");
6+
7+
8+
describe("Server", function() {
9+
var listen;
10+
var app;
11+
12+
function listenShorthand(done) {
13+
return app.listen(8000, '127.0.0.1', function(err) {
14+
if(err) done(err);
15+
done();
16+
});
17+
}
18+
19+
function close(done) {
20+
if(listen) {
21+
listen.close(done);
22+
} else {
23+
done();
24+
}
25+
}
26+
27+
describe("requests", function() {
28+
before(function(done) {
29+
app = express();
30+
var compiler = webpack(webpackConfig);
31+
app.use(middleware(compiler, {
32+
stats: "errors-only",
33+
quiet: true,
34+
publicPath: "/",
35+
}));
36+
listen = listenShorthand(done);
37+
});
38+
after(close);
39+
40+
it("GET request to bundle file", function(done) {
41+
request(app).get("/bundle.js")
42+
.expect("Content-Type", "application/javascript")
43+
// .expect("Content-Length", "2657")
44+
.expect("Access-Control-Allow-Origin", "*")
45+
.expect(200, /console\.log\("Hey\."\)/, done);
46+
});
47+
48+
it("POST request to bundle file", function(done) {
49+
request(app).post("/bundle.js")
50+
.expect(404, done);
51+
});
52+
53+
it("request to image", function(done) {
54+
request(app).get("/svg.svg")
55+
.expect("Content-Type", "image/svg+xml")
56+
.expect("Content-Length", "4778")
57+
.expect("Access-Control-Allow-Origin", "*")
58+
.expect(200, done);
59+
});
60+
61+
it("request to non existing file", function(done) {
62+
request(app).get("/nope")
63+
.expect("Content-Type", "text/html; charset=utf-8")
64+
.expect(404, done);
65+
});
66+
});
67+
68+
describe("custom headers", function() {
69+
before(function(done) {
70+
app = express();
71+
var compiler = webpack(webpackConfig);
72+
app.use(middleware(compiler, {
73+
stats: "errors-only",
74+
quiet: true,
75+
headers: { "X-nonsense-1": "yes", "X-nonsense-2": "no" }
76+
}));
77+
listen = listenShorthand(done);
78+
});
79+
after(close);
80+
81+
it("request to bundle file", function(done) {
82+
request(app).get("/bundle.js")
83+
.expect("X-nonsense-1", "yes")
84+
.expect("X-nonsense-2", "no")
85+
.expect(200, done);
86+
});
87+
});
88+
});

test/fixtures/server-test/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require("./svg.svg");
2+
3+
console.log("Hey.");

test/fixtures/server-test/svg.svg

Lines changed: 33 additions & 0 deletions
Loading
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
quiet: true,
3+
context: __dirname,
4+
entry: "./index.js",
5+
output: {
6+
filename: "bundle.js",
7+
path: "/"
8+
},
9+
module: {
10+
loaders: [
11+
{
12+
test: /\.svg$/,
13+
loader: "file",
14+
query: { name: "[name].[ext]" }
15+
}
16+
]
17+
}
18+
};

0 commit comments

Comments
 (0)