forked from jamesshore/lets_code_javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_server_test.js
More file actions
160 lines (135 loc) · 4.72 KB
/
_server_test.js
File metadata and controls
160 lines (135 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) 2012 Titanium I.T. LLC. All rights reserved. See LICENSE.txt for details.
(function() {
"use strict";
var server = require("./server.js");
var http = require("http");
var fs = require("fs");
var async = require("async");
var assert = require("../shared/_assert.js");
var CONTENT_DIR = "generated/test";
var INDEX_PAGE = "index.html";
var OTHER_PAGE = "other.html";
var NOT_FOUND_PAGE = "test404.html";
var INDEX_PAGE_DATA = "This is index page file";
var OTHER_PAGE_DATA = "This is another page";
var NOT_FOUND_DATA = "This is 404 page file";
var PORT = 5020;
var BASE_URL = "http://localhost:" + PORT;
var TEST_FILES = [
[ CONTENT_DIR + "/" + INDEX_PAGE, INDEX_PAGE_DATA],
[ CONTENT_DIR + "/" + OTHER_PAGE, OTHER_PAGE_DATA],
[ CONTENT_DIR + "/" + NOT_FOUND_PAGE, NOT_FOUND_DATA]
];
describe("Server", function() {
beforeEach(function(done) {
async.each(TEST_FILES, createTestFile, done);
});
afterEach(function(done) {
async.each(TEST_FILES, deleteTestFile, done);
});
it("serves files from directory", function(done) {
httpGet(BASE_URL + "/" + INDEX_PAGE, function(response, responseData) {
assert.equal(200, response.statusCode, "status code");
assert.equal(INDEX_PAGE_DATA, responseData, "response text");
done();
});
});
it("supports multiple files", function(done) {
httpGet(BASE_URL + "/" + OTHER_PAGE, function(response, responseData) {
assert.equal(200, response.statusCode, "status code");
assert.equal(OTHER_PAGE_DATA, responseData, "response text");
done();
});
});
it("supports multiple files", function(done) {
httpGet(BASE_URL + "/" + OTHER_PAGE, function(response, responseData) {
assert.equal(200, response.statusCode, "status code");
assert.equal(OTHER_PAGE_DATA, responseData, "response text");
done();
});
});
it("serves index.html when asked for home page", function(done) {
httpGet(BASE_URL, function(response, responseData) {
assert.equal(200, response.statusCode, "status code");
assert.equal(INDEX_PAGE_DATA, responseData, "response text");
done();
});
});
it("returns 404 when file doesn't exist", function(done) {
httpGet(BASE_URL + "/bargle", function(response, responseData) {
assert.equal(404, response.statusCode, "status code");
assert.equal(NOT_FOUND_DATA, responseData, "404 text");
done();
});
});
it("requires home page parameter", function() {
assert.throws(function() {
server.start();
});
});
it("requires 404 page parameter", function() {
assert.throws(function() {
server.start(CONTENT_DIR);
});
});
it("requires port parameter", function() {
assert.throws(function() {
server.start(CONTENT_DIR, NOT_FOUND_PAGE);
});
});
it("runs callback when stop completes", function(done) {
server.start(CONTENT_DIR, NOT_FOUND_PAGE, PORT);
server.stop(function() {
done();
});
});
it("stop throws exception when not running", function() {
assert.throws(function() {
server.stop();
});
});
});
function httpGet(url, callback) {
server.start(CONTENT_DIR, NOT_FOUND_PAGE, PORT, function() {
http.get(url, function(response) {
var receivedData = "";
response.setEncoding("utf8");
response.on("data", function(chunk) {
receivedData += chunk;
});
response.on("error", function(err) {
console.log("ERROR", err);
});
response.on("end", function() {
server.stop(function() {
callback(response, receivedData);
});
});
});
});
}
function createTestFile(fileAndData, done) {
// Note: writeFile() MUST be called asynchronously in order for this code to work on Windows 7.
// If it's called synchronously, it fails with an EPERM error when the second test starts. This
// may be related to this Node.js issue: https://github.com/joyent/node/issues/6599
// This issue appeared after upgrading send 0.2.0 to 0.9.3. Prior to that, writeFileSync()
// worked fine.
fs.writeFile(fileAndData[0], fileAndData[1], function(err) {
if (err) console.log("could not create test file: [" + fileAndData[0] + "]. Error: " + err);
done();
});
}
function deleteTestFile(fileAndData, done) {
// Note: unlink() MUST be called asynchronously here in order for this code to work on Windows 7.
// If it's called synchronously, then it will run before the file is closed, and that is not allowed
// on Windows 7. It's possible that this is the result of a Node.js bug; see this issue for details:
// https://github.com/joyent/node/issues/6599
var file = fileAndData[0];
if (fs.existsSync(file)) {
fs.unlink(file, function(err) {
if (err || fs.existsSync(file)) console.log("could not delete test file: [" + file + "]. Error: " + err);
done();
});
}
}
}());