rc-web@69
|
1 var vows = require('vows')
|
rc-web@69
|
2 , request = require('request')
|
rc-web@69
|
3 , assert = require('assert')
|
rc-web@69
|
4 , static = require('../../lib/node-static');
|
rc-web@69
|
5
|
rc-web@69
|
6 var fileServer = new static.Server(__dirname + '/../fixtures');
|
rc-web@69
|
7 var suite = vows.describe('node-static');
|
rc-web@69
|
8 var TEST_PORT = 8080;
|
rc-web@69
|
9 var TEST_SERVER = 'http://localhost:' + TEST_PORT;
|
rc-web@69
|
10 var version = static.version.join('.');
|
rc-web@69
|
11 var server;
|
rc-web@69
|
12 var callback;
|
rc-web@69
|
13
|
rc-web@69
|
14 headers = {
|
rc-web@69
|
15 'requesting headers': {
|
rc-web@69
|
16 topic : function(){
|
rc-web@69
|
17 request.head(TEST_SERVER + '/index.html', this.callback);
|
rc-web@69
|
18 }
|
rc-web@69
|
19 }
|
rc-web@69
|
20 }
|
rc-web@69
|
21 headers['requesting headers']['should respond with node-static/' + version] = function(error, response, body){
|
rc-web@69
|
22 assert.equal(response.headers['server'], 'node-static/' + version);
|
rc-web@69
|
23 }
|
rc-web@69
|
24
|
rc-web@69
|
25 suite.addBatch({
|
rc-web@69
|
26 'once an http server is listening with a callback': {
|
rc-web@69
|
27 topic: function () {
|
rc-web@69
|
28 server = require('http').createServer(function (request, response) {
|
rc-web@69
|
29 fileServer.serve(request, response, function(err, result) {
|
rc-web@69
|
30 if (callback)
|
rc-web@69
|
31 callback(request, response, err, result);
|
rc-web@69
|
32 else
|
rc-web@69
|
33 request.end();
|
rc-web@69
|
34 });
|
rc-web@69
|
35 }).listen(TEST_PORT, this.callback)
|
rc-web@69
|
36 },
|
rc-web@69
|
37 'should be listening' : function(){
|
rc-web@69
|
38 /* This test is necessary to ensure the topic execution.
|
rc-web@69
|
39 * A topic without tests will be not executed */
|
rc-web@69
|
40 assert.isTrue(true);
|
rc-web@69
|
41 }
|
rc-web@69
|
42 },
|
rc-web@69
|
43 }).addBatch({
|
rc-web@69
|
44 'streaming a 404 page': {
|
rc-web@69
|
45 topic: function(){
|
rc-web@69
|
46 callback = function(request, response, err, result) {
|
rc-web@69
|
47 if (err) {
|
rc-web@69
|
48 response.writeHead(err.status, err.headers);
|
rc-web@69
|
49 setTimeout(function() {
|
rc-web@69
|
50 response.end('Custom 404 Stream.')
|
rc-web@69
|
51 }, 100);
|
rc-web@69
|
52 }
|
rc-web@69
|
53 }
|
rc-web@69
|
54 request.get(TEST_SERVER + '/not-found', this.callback);
|
rc-web@69
|
55 },
|
rc-web@69
|
56 'should respond with 404' : function(error, response, body){
|
rc-web@69
|
57 assert.equal(response.statusCode, 404);
|
rc-web@69
|
58 },
|
rc-web@69
|
59 'should respond with the streamed content': function(error, response, body){
|
rc-web@69
|
60 callback = null;
|
rc-web@69
|
61 assert.equal(body, 'Custom 404 Stream.');
|
rc-web@69
|
62 }
|
rc-web@69
|
63 }
|
rc-web@69
|
64 }).addBatch({
|
rc-web@69
|
65 'once an http server is listening without a callback': {
|
rc-web@69
|
66 topic: function () {
|
rc-web@69
|
67 server.close();
|
rc-web@69
|
68 server = require('http').createServer(function (request, response) {
|
rc-web@69
|
69 fileServer.serve(request, response);
|
rc-web@69
|
70 }).listen(TEST_PORT, this.callback)
|
rc-web@69
|
71 },
|
rc-web@69
|
72 'should be listening' : function(){
|
rc-web@69
|
73 /* This test is necessary to ensure the topic execution.
|
rc-web@69
|
74 * A topic without tests will be not executed */
|
rc-web@69
|
75 assert.isTrue(true);
|
rc-web@69
|
76 }
|
rc-web@69
|
77 }
|
rc-web@69
|
78 }).addBatch({
|
rc-web@69
|
79 'requesting a file not found': {
|
rc-web@69
|
80 topic : function(){
|
rc-web@69
|
81 request.get(TEST_SERVER + '/not-found', this.callback);
|
rc-web@69
|
82 },
|
rc-web@69
|
83 'should respond with 404' : function(error, response, body){
|
rc-web@69
|
84 assert.equal(response.statusCode, 404);
|
rc-web@69
|
85 }
|
rc-web@69
|
86 }
|
rc-web@69
|
87 })
|
rc-web@69
|
88 .addBatch({
|
rc-web@69
|
89 'requesting a malformed URI': {
|
rc-web@69
|
90 topic: function(){
|
rc-web@69
|
91 request.get(TEST_SERVER + '/a%AFc', this.callback);
|
rc-web@69
|
92 },
|
rc-web@69
|
93 'should respond with 400': function(error, response, body){
|
rc-web@69
|
94 assert.equal(response.statusCode, 400);
|
rc-web@69
|
95 }
|
rc-web@69
|
96 }
|
rc-web@69
|
97 })
|
rc-web@69
|
98 .addBatch({
|
rc-web@69
|
99 'serving hello.txt': {
|
rc-web@69
|
100 topic : function(){
|
rc-web@69
|
101 request.get(TEST_SERVER + '/hello.txt', this.callback);
|
rc-web@69
|
102 },
|
rc-web@69
|
103 'should respond with 200' : function(error, response, body){
|
rc-web@69
|
104 assert.equal(response.statusCode, 200);
|
rc-web@69
|
105 },
|
rc-web@69
|
106 'should respond with text/plain': function(error, response, body){
|
rc-web@69
|
107 assert.equal(response.headers['content-type'], 'text/plain');
|
rc-web@69
|
108 },
|
rc-web@69
|
109 'should respond with hello world': function(error, response, body){
|
rc-web@69
|
110 assert.equal(body, 'hello world');
|
rc-web@69
|
111 }
|
rc-web@69
|
112 }
|
rc-web@69
|
113 }).addBatch({
|
rc-web@69
|
114 'serving directory index': {
|
rc-web@69
|
115 topic : function(){
|
rc-web@69
|
116 request.get(TEST_SERVER, this.callback);
|
rc-web@69
|
117 },
|
rc-web@69
|
118 'should respond with 200' : function(error, response, body){
|
rc-web@69
|
119 assert.equal(response.statusCode, 200);
|
rc-web@69
|
120 },
|
rc-web@69
|
121 'should respond with text/html': function(error, response, body){
|
rc-web@69
|
122 assert.equal(response.headers['content-type'], 'text/html');
|
rc-web@69
|
123 }
|
rc-web@69
|
124 }
|
rc-web@69
|
125 }).addBatch({
|
rc-web@69
|
126 'serving index.html from the cache': {
|
rc-web@69
|
127 topic : function(){
|
rc-web@69
|
128 request.get(TEST_SERVER + '/index.html', this.callback);
|
rc-web@69
|
129 },
|
rc-web@69
|
130 'should respond with 200' : function(error, response, body){
|
rc-web@69
|
131 assert.equal(response.statusCode, 200);
|
rc-web@69
|
132 },
|
rc-web@69
|
133 'should respond with text/html': function(error, response, body){
|
rc-web@69
|
134 assert.equal(response.headers['content-type'], 'text/html');
|
rc-web@69
|
135 }
|
rc-web@69
|
136 }
|
rc-web@69
|
137 }).addBatch({
|
rc-web@69
|
138 'requesting with If-None-Match': {
|
rc-web@69
|
139 topic : function(){
|
rc-web@69
|
140 var _this = this;
|
rc-web@69
|
141 request.get(TEST_SERVER + '/index.html', function(error, response, body){
|
rc-web@69
|
142 request({
|
rc-web@69
|
143 method: 'GET',
|
rc-web@69
|
144 uri: TEST_SERVER + '/index.html',
|
rc-web@69
|
145 headers: {'if-none-match': response.headers['etag']}
|
rc-web@69
|
146 },
|
rc-web@69
|
147 _this.callback);
|
rc-web@69
|
148 });
|
rc-web@69
|
149 },
|
rc-web@69
|
150 'should respond with 304' : function(error, response, body){
|
rc-web@69
|
151 assert.equal(response.statusCode, 304);
|
rc-web@69
|
152 }
|
rc-web@69
|
153 },
|
rc-web@69
|
154 'requesting with If-None-Match and If-Modified-Since': {
|
rc-web@69
|
155 topic : function(){
|
rc-web@69
|
156 var _this = this;
|
rc-web@69
|
157 request.get(TEST_SERVER + '/index.html', function(error, response, body){
|
rc-web@69
|
158 var modified = Date.parse(response.headers['last-modified']);
|
rc-web@69
|
159 var oneDayLater = new Date(modified + (24 * 60 * 60 * 1000)).toUTCString();
|
rc-web@69
|
160 var nonMatchingEtag = '1111222233334444';
|
rc-web@69
|
161 request({
|
rc-web@69
|
162 method: 'GET',
|
rc-web@69
|
163 uri: TEST_SERVER + '/index.html',
|
rc-web@69
|
164 headers: {
|
rc-web@69
|
165 'if-none-match': nonMatchingEtag,
|
rc-web@69
|
166 'if-modified-since': oneDayLater
|
rc-web@69
|
167 }
|
rc-web@69
|
168 },
|
rc-web@69
|
169 _this.callback);
|
rc-web@69
|
170 });
|
rc-web@69
|
171 },
|
rc-web@69
|
172 'should respond with a 200': function(error, response, body){
|
rc-web@69
|
173 assert.equal(response.statusCode, 200);
|
rc-web@69
|
174 }
|
rc-web@69
|
175 }
|
rc-web@69
|
176 })
|
rc-web@69
|
177 .addBatch({
|
rc-web@69
|
178 'requesting POST': {
|
rc-web@69
|
179 topic : function(){
|
rc-web@69
|
180 request.post(TEST_SERVER + '/index.html', this.callback);
|
rc-web@69
|
181 },
|
rc-web@69
|
182 'should respond with 200' : function(error, response, body){
|
rc-web@69
|
183 assert.equal(response.statusCode, 200);
|
rc-web@69
|
184 },
|
rc-web@69
|
185 'should not be empty' : function(error, response, body){
|
rc-web@69
|
186 assert.isNotEmpty(body);
|
rc-web@69
|
187 }
|
rc-web@69
|
188 }
|
rc-web@69
|
189 })
|
rc-web@69
|
190 .addBatch({
|
rc-web@69
|
191 'requesting HEAD': {
|
rc-web@69
|
192 topic : function(){
|
rc-web@69
|
193 request.head(TEST_SERVER + '/index.html', this.callback);
|
rc-web@69
|
194 },
|
rc-web@69
|
195 'should respond with 200' : function(error, response, body){
|
rc-web@69
|
196 assert.equal(response.statusCode, 200);
|
rc-web@69
|
197 },
|
rc-web@69
|
198 'head must has no body' : function(error, response, body){
|
rc-web@69
|
199 assert.isEmpty(body);
|
rc-web@69
|
200 }
|
rc-web@69
|
201 }
|
rc-web@69
|
202 })
|
rc-web@69
|
203 .addBatch(headers)
|
rc-web@69
|
204 .addBatch({
|
rc-web@69
|
205 'addings custom mime types': {
|
rc-web@69
|
206 topic : function(){
|
rc-web@69
|
207 static.mime.define({'application/font-woff': ['woff']});
|
rc-web@69
|
208 this.callback();
|
rc-web@69
|
209 },
|
rc-web@69
|
210 'should add woff' : function(error, response, body){
|
rc-web@69
|
211 assert.equal(static.mime.lookup('woff'), 'application/font-woff');
|
rc-web@69
|
212 }
|
rc-web@69
|
213 }
|
rc-web@69
|
214 })
|
rc-web@69
|
215 .addBatch({
|
rc-web@69
|
216 'serving subdirectory index': {
|
rc-web@69
|
217 topic : function(){
|
rc-web@69
|
218 request.get(TEST_SERVER + '/there/', this.callback); // with trailing slash
|
rc-web@69
|
219 },
|
rc-web@69
|
220 'should respond with 200' : function(error, response, body){
|
rc-web@69
|
221 assert.equal(response.statusCode, 200);
|
rc-web@69
|
222 },
|
rc-web@69
|
223 'should respond with text/html': function(error, response, body){
|
rc-web@69
|
224 assert.equal(response.headers['content-type'], 'text/html');
|
rc-web@69
|
225 }
|
rc-web@69
|
226 }
|
rc-web@69
|
227 })
|
rc-web@69
|
228 .addBatch({
|
rc-web@69
|
229 'redirecting to subdirectory index': {
|
rc-web@69
|
230 topic : function(){
|
rc-web@69
|
231 request.get({ url: TEST_SERVER + '/there', followRedirect: false }, this.callback); // without trailing slash
|
rc-web@69
|
232 },
|
rc-web@69
|
233 'should respond with 301' : function(error, response, body){
|
rc-web@69
|
234 assert.equal(response.statusCode, 301);
|
rc-web@69
|
235 },
|
rc-web@69
|
236 'should respond with location header': function(error, response, body){
|
rc-web@69
|
237 assert.equal(response.headers['location'], '/there/'); // now with trailing slash
|
rc-web@69
|
238 },
|
rc-web@69
|
239 'should respond with empty string body' : function(error, response, body){
|
rc-web@69
|
240 assert.equal(body, '');
|
rc-web@69
|
241 }
|
rc-web@69
|
242 }
|
rc-web@69
|
243 })
|
rc-web@69
|
244 .addBatch({
|
rc-web@69
|
245 'requesting a subdirectory (with trailing slash) not found': {
|
rc-web@69
|
246 topic : function(){
|
rc-web@69
|
247 request.get(TEST_SERVER + '/notthere/', this.callback); // with trailing slash
|
rc-web@69
|
248 },
|
rc-web@69
|
249 'should respond with 404' : function(error, response, body){
|
rc-web@69
|
250 assert.equal(response.statusCode, 404);
|
rc-web@69
|
251 }
|
rc-web@69
|
252 }
|
rc-web@69
|
253 })
|
rc-web@69
|
254 .addBatch({
|
rc-web@69
|
255 'requesting a subdirectory (without trailing slash) not found': {
|
rc-web@69
|
256 topic : function(){
|
rc-web@69
|
257 request.get({ url: TEST_SERVER + '/notthere', followRedirect: false }, this.callback); // without trailing slash
|
rc-web@69
|
258 },
|
rc-web@69
|
259 'should respond with 404' : function(error, response, body){
|
rc-web@69
|
260 assert.equal(response.statusCode, 404);
|
rc-web@69
|
261 }
|
rc-web@69
|
262 }
|
rc-web@69
|
263 }).export(module);
|
rc-web@69
|
264
|