Skip to content

Commit

Permalink
Add first tests
Browse files Browse the repository at this point in the history
Ref #623
  • Loading branch information
SpaceK33z committed Sep 24, 2016
1 parent 1cf6549 commit 8224306
Show file tree
Hide file tree
Showing 16 changed files with 243 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"env": {
"node": true,
"mocha": true,
"browser": true
},
"rules": {
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
/client/live.bundle.js
/client/index.bundle.js
/client/sockjs.bundle.js
/coverage
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
"css-loader": "~0.25.0",
"eslint": "^3.4.0",
"file-loader": "~0.9.0",
"istanbul": "^0.4.5",
"jquery": "^2.2.0",
"less": "^2.5.1",
"less-loader": "~2.2.0",
"mocha": "^3.0.2",
"pug": "^2.0.0-beta5",
"pug-loader": "^2.3.0",
"style-loader": "~0.13.0",
"supertest": "^2.0.0",
"url-loader": "~0.5.6",
"webpack": "^2.1.0-beta.1"
},
Expand All @@ -55,6 +58,9 @@
"client-sockjs": "webpack ./client/sockjs.js client/sockjs.bundle.js --color --config client/webpack.sockjs.config.js -p",
"lint": "eslint bin lib test examples client/{index,live,socket,sockjs,webpack.config}.js",
"beautify": "npm run lint -- --fix",
"travis": "npm run lint && node lib/Server.js"
"test": "mocha --full-trace --check-leaks",
"posttest": "npm run -s lint",
"cover": "istanbul cover node_modules/mocha/bin/_mocha",
"travis": "npm run cover -- --report lcovonly && npm run lint"
}
}
24 changes: 24 additions & 0 deletions test/Compress.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var request = require("supertest");
var helper = require("./helper");
var config = require("./fixtures/simple-config/webpack.config");

describe("Compress", function() {
var server;
var req;

before(function(done) {
server = helper.start(config, {
quiet: true,
compress: true
}, done);
req = request(server.app);
});

after(helper.close);

it("request to bundle file", function(done) {
req.get("/bundle.js")
.expect("Content-Encoding", "gzip")
.expect(200, done);
});
});
93 changes: 93 additions & 0 deletions test/ContentBase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
var request = require("supertest");
var path = require("path");
var helper = require("./helper");
var config = require("./fixtures/contentbase-config/webpack.config");

var contentBasePublic = path.join(__dirname, "fixtures/contentbase-config/public");
var contentBaseOther = path.join(__dirname, "fixtures/contentbase-config/other");

describe("ContentBase", function() {
var server;
var req;
afterEach(helper.close);

describe("to directory", function() {
before(function(done) {
server = helper.start(config, {
quiet: true,
contentBase: contentBasePublic,
}, done);
req = request(server.app);
});

it("Request to index", function(done) {
req.get("/")
.expect(200, /Heyo/, done);
});

it("Request to other file", function(done) {
req.get("/other.html")
.expect(200, /Other html/, done);
});
});

describe("to directories", function() {
before(function(done) {
server = helper.start(config, {
quiet: true,
contentBase: [contentBasePublic, contentBaseOther],
}, done);
req = request(server.app);
});

it("Request to first directory", function(done) {
req.get("/")
.expect(200, /Heyo/, done);
});

it("Request to second directory", function(done) {
req.get("/foo.html")
.expect(200, /Foo!/, done);
});
});

describe("to port", function() {
before(function(done) {
server = helper.start(config, {
quiet: true,
contentBase: 9099999,
}, done);
req = request(server.app);
});

it("Request to page", function(done) {
req.get("/other.html")
.expect("Location", "//localhost:9099999/other.html")
.expect(302, done);
});
});

describe("to external url", function() {
before(function(done) {
server = helper.start(config, {
quiet: true,
contentBase: "http://example.com/",
}, done);
req = request(server.app);
});

it("Request to page", function(done) {
req.get("/foo.html")
// TODO: hmm, two slashes seems to be a bug?
.expect("Location", "http://example.com//foo.html")
.expect(302, done);
});

it("Request to page with search params", function(done) {
req.get("/foo.html?space=ship")
// TODO: hmm, two slashes seems to be a bug?
.expect("Location", "http://example.com//foo.html?space=ship")
.expect(302, done);
});
});
});
64 changes: 64 additions & 0 deletions test/Routes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
var request = require("supertest");
var fs = require("fs");
var path = require("path");
var helper = require("./helper");
var config = require("./fixtures/simple-config/webpack.config");

var directoryIndex = fs.readFileSync(path.join(__dirname, "fixtures/directory-index.txt"), "utf-8");
var magicHtml = fs.readFileSync(path.join(__dirname, "fixtures/magic-html.txt"), "utf-8");

describe("Routes", function() {
var server;
var req;

before(function(done) {
server = helper.start(config, {
quiet: true,
headers: { "X-Foo": "1" }
}, done);
req = request(server.app);
});

after(helper.close);

it("GET request to inline bundle", function(done) {
req.get("/webpack-dev-server.js")
.expect("Content-Type", "application/javascript")
.expect(200, done);
});

it("GET request to live bundle", function(done) {
req.get("/__webpack_dev_server__/live.bundle.js")
.expect("Content-Type", "application/javascript")
.expect(200, done);
});

it("GET request to sockjs bundle", function(done) {
req.get("/__webpack_dev_server__/sockjs.bundle.js")
.expect("Content-Type", "application/javascript")
.expect(200, done);
});

it("GET request to live html", function(done) {
req.get("/webpack-dev-server/")
.expect("Content-Type", "text/html")
.expect(200, /__webpack_dev_server__/, done);
});

it("GET request to directory index", function(done) {
req.get("/webpack-dev-server")
.expect("Content-Type", "text/html")
.expect(200, directoryIndex.trim(), done);
});

it("GET request to magic html", function(done) {
req.get("/bundle")
.expect(200, magicHtml.trim(), done);
});

it("GET request with headers", function(done) {
req.get("/bundle")
.expect("X-Foo", "1")
.expect(200, done);
});
});
3 changes: 3 additions & 0 deletions test/fixtures/contentbase-config/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require("./index.html");

console.log("Hey.");
1 change: 1 addition & 0 deletions test/fixtures/contentbase-config/other/foo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Foo!
1 change: 1 addition & 0 deletions test/fixtures/contentbase-config/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Heyo.
1 change: 1 addition & 0 deletions test/fixtures/contentbase-config/public/other.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Other html
8 changes: 8 additions & 0 deletions test/fixtures/contentbase-config/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
context: __dirname,
entry: "./foo.js",
output: {
filename: "bundle.js",
publicPath: "/"
}
};
1 change: 1 addition & 0 deletions test/fixtures/directory-index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html><html><head><meta charset="utf-8"/></head><body><ul><li><a href="/bundle.js">bundle.js</a></li><li><a href="/bundle">bundle</a> (magic html for bundle.js) (<a href="/webpack-dev-server/bundle">webpack-dev-server</a>)</li></ul></body></html>
1 change: 1 addition & 0 deletions test/fixtures/magic-html.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html><html><head><meta charset="utf-8"/></head><body><script type="text/javascript" charset="utf-8" src="/bundle.js"></script></body></html>
1 change: 1 addition & 0 deletions test/fixtures/simple-config/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Hey.");
8 changes: 8 additions & 0 deletions test/fixtures/simple-config/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
context: __dirname,
entry: "./foo.js",
output: {
filename: "bundle.js",
path: "/"
}
};
28 changes: 28 additions & 0 deletions test/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var Server = require("../lib/Server");
var webpack = require("webpack");

var server;

module.exports = {
start: function(config, options, done) {
var compiler = webpack(config);
server = new Server(compiler, options);

server.listen(8080, "localhost", function(err) {
if(err) return done(err);
done();
});

return server;
},
close: function(done) {
if(server) {
server.close(function() {
server = null;
done();
});
} else {
done();
}
}
};

0 comments on commit 8224306

Please sign in to comment.