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
143 lines (121 loc) · 3.92 KB
/
_server_test.js
File metadata and controls
143 lines (121 loc) · 3.92 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
// 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 assert = require("assert");
var async = require("async");
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;
exports.setUp = function(done) {
fs.writeFileSync(CONTENT_DIR + "/" + INDEX_PAGE, INDEX_PAGE_DATA);
fs.writeFileSync(CONTENT_DIR + "/" + OTHER_PAGE, OTHER_PAGE_DATA);
fs.writeFileSync(CONTENT_DIR + "/" + NOT_FOUND_PAGE, NOT_FOUND_DATA);
done();
};
exports.tearDown = function(done) {
var filesToDelete = [
CONTENT_DIR + "/" + INDEX_PAGE,
CONTENT_DIR + "/" + OTHER_PAGE,
CONTENT_DIR + "/" + NOT_FOUND_PAGE
];
async.each(filesToDelete, cleanUpFile, done);
};
exports.test_servesFilesFromDirectory = function(test) {
httpGet(BASE_URL + "/" + INDEX_PAGE, function(response, responseData) {
test.equals(200, response.statusCode, "status code");
test.equals(INDEX_PAGE_DATA, responseData, "response text");
test.done();
});
};
exports.test_supportsMultipleFiles = function(test) {
httpGet(BASE_URL + "/" + OTHER_PAGE, function(response, responseData) {
test.equals(200, response.statusCode, "status code");
test.equals(OTHER_PAGE_DATA, responseData, "response text");
test.done();
});
};
exports.test_servesIndexDotHtmlWhenAskedForHomePage = function(test) {
httpGet(BASE_URL, function(response, responseData) {
test.equals(200, response.statusCode, "status code");
test.equals(INDEX_PAGE_DATA, responseData, "response text");
test.done();
});
};
exports.test_returns404WhenFileDoesNotExist = function(test) {
httpGet(BASE_URL + "/bargle", function(response, responseData) {
test.equals(404, response.statusCode, "status code");
test.equals(NOT_FOUND_DATA, responseData, "404 text");
test.done();
});
};
exports.test_requiresHomePageParameter = function(test) {
test.throws(function() {
server.start();
});
test.done();
};
exports.test_requires404PageParameter = function(test) {
test.throws(function() {
server.start(CONTENT_DIR);
});
test.done();
};
exports.test_requiresPortParameter = function(test) {
test.throws(function() {
server.start(CONTENT_DIR, NOT_FOUND_PAGE);
});
test.done();
};
exports.test_runsCallbackWhenStopCompletes = function(test) {
server.start(CONTENT_DIR, NOT_FOUND_PAGE, PORT);
server.stop(function() {
test.done();
});
};
exports.test_stopThrowsExceptionWhenNotRunning = function(test) {
test.throws(function() {
server.stop();
});
test.done();
};
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 cleanUpFile(file, 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
if (fs.existsSync(file)) {
fs.unlink(file, function() {
assert.ok(!fs.existsSync(file), "could not delete test file: [" + file + "]");
done();
});
}
}
}());