rc-web@69: var vows = require('vows') rc-web@69: , request = require('request') rc-web@69: , assert = require('assert') rc-web@69: , static = require('../../lib/node-static'); rc-web@69: rc-web@69: var fileServer = new static.Server(__dirname + '/../fixtures'); rc-web@69: var suite = vows.describe('node-static'); rc-web@69: var TEST_PORT = 8080; rc-web@69: var TEST_SERVER = 'http://localhost:' + TEST_PORT; rc-web@69: var version = static.version.join('.'); rc-web@69: var server; rc-web@69: var callback; rc-web@69: rc-web@69: headers = { rc-web@69: 'requesting headers': { rc-web@69: topic : function(){ rc-web@69: request.head(TEST_SERVER + '/index.html', this.callback); rc-web@69: } rc-web@69: } rc-web@69: } rc-web@69: headers['requesting headers']['should respond with node-static/' + version] = function(error, response, body){ rc-web@69: assert.equal(response.headers['server'], 'node-static/' + version); rc-web@69: } rc-web@69: rc-web@69: suite.addBatch({ rc-web@69: 'once an http server is listening with a callback': { rc-web@69: topic: function () { rc-web@69: server = require('http').createServer(function (request, response) { rc-web@69: fileServer.serve(request, response, function(err, result) { rc-web@69: if (callback) rc-web@69: callback(request, response, err, result); rc-web@69: else rc-web@69: request.end(); rc-web@69: }); rc-web@69: }).listen(TEST_PORT, this.callback) rc-web@69: }, rc-web@69: 'should be listening' : function(){ rc-web@69: /* This test is necessary to ensure the topic execution. rc-web@69: * A topic without tests will be not executed */ rc-web@69: assert.isTrue(true); rc-web@69: } rc-web@69: }, rc-web@69: }).addBatch({ rc-web@69: 'streaming a 404 page': { rc-web@69: topic: function(){ rc-web@69: callback = function(request, response, err, result) { rc-web@69: if (err) { rc-web@69: response.writeHead(err.status, err.headers); rc-web@69: setTimeout(function() { rc-web@69: response.end('Custom 404 Stream.') rc-web@69: }, 100); rc-web@69: } rc-web@69: } rc-web@69: request.get(TEST_SERVER + '/not-found', this.callback); rc-web@69: }, rc-web@69: 'should respond with 404' : function(error, response, body){ rc-web@69: assert.equal(response.statusCode, 404); rc-web@69: }, rc-web@69: 'should respond with the streamed content': function(error, response, body){ rc-web@69: callback = null; rc-web@69: assert.equal(body, 'Custom 404 Stream.'); rc-web@69: } rc-web@69: } rc-web@69: }).addBatch({ rc-web@69: 'once an http server is listening without a callback': { rc-web@69: topic: function () { rc-web@69: server.close(); rc-web@69: server = require('http').createServer(function (request, response) { rc-web@69: fileServer.serve(request, response); rc-web@69: }).listen(TEST_PORT, this.callback) rc-web@69: }, rc-web@69: 'should be listening' : function(){ rc-web@69: /* This test is necessary to ensure the topic execution. rc-web@69: * A topic without tests will be not executed */ rc-web@69: assert.isTrue(true); rc-web@69: } rc-web@69: } rc-web@69: }).addBatch({ rc-web@69: 'requesting a file not found': { rc-web@69: topic : function(){ rc-web@69: request.get(TEST_SERVER + '/not-found', this.callback); rc-web@69: }, rc-web@69: 'should respond with 404' : function(error, response, body){ rc-web@69: assert.equal(response.statusCode, 404); rc-web@69: } rc-web@69: } rc-web@69: }) rc-web@69: .addBatch({ rc-web@69: 'requesting a malformed URI': { rc-web@69: topic: function(){ rc-web@69: request.get(TEST_SERVER + '/a%AFc', this.callback); rc-web@69: }, rc-web@69: 'should respond with 400': function(error, response, body){ rc-web@69: assert.equal(response.statusCode, 400); rc-web@69: } rc-web@69: } rc-web@69: }) rc-web@69: .addBatch({ rc-web@69: 'serving hello.txt': { rc-web@69: topic : function(){ rc-web@69: request.get(TEST_SERVER + '/hello.txt', this.callback); rc-web@69: }, rc-web@69: 'should respond with 200' : function(error, response, body){ rc-web@69: assert.equal(response.statusCode, 200); rc-web@69: }, rc-web@69: 'should respond with text/plain': function(error, response, body){ rc-web@69: assert.equal(response.headers['content-type'], 'text/plain'); rc-web@69: }, rc-web@69: 'should respond with hello world': function(error, response, body){ rc-web@69: assert.equal(body, 'hello world'); rc-web@69: } rc-web@69: } rc-web@69: }).addBatch({ rc-web@69: 'serving directory index': { rc-web@69: topic : function(){ rc-web@69: request.get(TEST_SERVER, this.callback); rc-web@69: }, rc-web@69: 'should respond with 200' : function(error, response, body){ rc-web@69: assert.equal(response.statusCode, 200); rc-web@69: }, rc-web@69: 'should respond with text/html': function(error, response, body){ rc-web@69: assert.equal(response.headers['content-type'], 'text/html'); rc-web@69: } rc-web@69: } rc-web@69: }).addBatch({ rc-web@69: 'serving index.html from the cache': { rc-web@69: topic : function(){ rc-web@69: request.get(TEST_SERVER + '/index.html', this.callback); rc-web@69: }, rc-web@69: 'should respond with 200' : function(error, response, body){ rc-web@69: assert.equal(response.statusCode, 200); rc-web@69: }, rc-web@69: 'should respond with text/html': function(error, response, body){ rc-web@69: assert.equal(response.headers['content-type'], 'text/html'); rc-web@69: } rc-web@69: } rc-web@69: }).addBatch({ rc-web@69: 'requesting with If-None-Match': { rc-web@69: topic : function(){ rc-web@69: var _this = this; rc-web@69: request.get(TEST_SERVER + '/index.html', function(error, response, body){ rc-web@69: request({ rc-web@69: method: 'GET', rc-web@69: uri: TEST_SERVER + '/index.html', rc-web@69: headers: {'if-none-match': response.headers['etag']} rc-web@69: }, rc-web@69: _this.callback); rc-web@69: }); rc-web@69: }, rc-web@69: 'should respond with 304' : function(error, response, body){ rc-web@69: assert.equal(response.statusCode, 304); rc-web@69: } rc-web@69: }, rc-web@69: 'requesting with If-None-Match and If-Modified-Since': { rc-web@69: topic : function(){ rc-web@69: var _this = this; rc-web@69: request.get(TEST_SERVER + '/index.html', function(error, response, body){ rc-web@69: var modified = Date.parse(response.headers['last-modified']); rc-web@69: var oneDayLater = new Date(modified + (24 * 60 * 60 * 1000)).toUTCString(); rc-web@69: var nonMatchingEtag = '1111222233334444'; rc-web@69: request({ rc-web@69: method: 'GET', rc-web@69: uri: TEST_SERVER + '/index.html', rc-web@69: headers: { rc-web@69: 'if-none-match': nonMatchingEtag, rc-web@69: 'if-modified-since': oneDayLater rc-web@69: } rc-web@69: }, rc-web@69: _this.callback); rc-web@69: }); rc-web@69: }, rc-web@69: 'should respond with a 200': function(error, response, body){ rc-web@69: assert.equal(response.statusCode, 200); rc-web@69: } rc-web@69: } rc-web@69: }) rc-web@69: .addBatch({ rc-web@69: 'requesting POST': { rc-web@69: topic : function(){ rc-web@69: request.post(TEST_SERVER + '/index.html', this.callback); rc-web@69: }, rc-web@69: 'should respond with 200' : function(error, response, body){ rc-web@69: assert.equal(response.statusCode, 200); rc-web@69: }, rc-web@69: 'should not be empty' : function(error, response, body){ rc-web@69: assert.isNotEmpty(body); rc-web@69: } rc-web@69: } rc-web@69: }) rc-web@69: .addBatch({ rc-web@69: 'requesting HEAD': { rc-web@69: topic : function(){ rc-web@69: request.head(TEST_SERVER + '/index.html', this.callback); rc-web@69: }, rc-web@69: 'should respond with 200' : function(error, response, body){ rc-web@69: assert.equal(response.statusCode, 200); rc-web@69: }, rc-web@69: 'head must has no body' : function(error, response, body){ rc-web@69: assert.isEmpty(body); rc-web@69: } rc-web@69: } rc-web@69: }) rc-web@69: .addBatch(headers) rc-web@69: .addBatch({ rc-web@69: 'addings custom mime types': { rc-web@69: topic : function(){ rc-web@69: static.mime.define({'application/font-woff': ['woff']}); rc-web@69: this.callback(); rc-web@69: }, rc-web@69: 'should add woff' : function(error, response, body){ rc-web@69: assert.equal(static.mime.lookup('woff'), 'application/font-woff'); rc-web@69: } rc-web@69: } rc-web@69: }) rc-web@69: .addBatch({ rc-web@69: 'serving subdirectory index': { rc-web@69: topic : function(){ rc-web@69: request.get(TEST_SERVER + '/there/', this.callback); // with trailing slash rc-web@69: }, rc-web@69: 'should respond with 200' : function(error, response, body){ rc-web@69: assert.equal(response.statusCode, 200); rc-web@69: }, rc-web@69: 'should respond with text/html': function(error, response, body){ rc-web@69: assert.equal(response.headers['content-type'], 'text/html'); rc-web@69: } rc-web@69: } rc-web@69: }) rc-web@69: .addBatch({ rc-web@69: 'redirecting to subdirectory index': { rc-web@69: topic : function(){ rc-web@69: request.get({ url: TEST_SERVER + '/there', followRedirect: false }, this.callback); // without trailing slash rc-web@69: }, rc-web@69: 'should respond with 301' : function(error, response, body){ rc-web@69: assert.equal(response.statusCode, 301); rc-web@69: }, rc-web@69: 'should respond with location header': function(error, response, body){ rc-web@69: assert.equal(response.headers['location'], '/there/'); // now with trailing slash rc-web@69: }, rc-web@69: 'should respond with empty string body' : function(error, response, body){ rc-web@69: assert.equal(body, ''); rc-web@69: } rc-web@69: } rc-web@69: }) rc-web@69: .addBatch({ rc-web@69: 'requesting a subdirectory (with trailing slash) not found': { rc-web@69: topic : function(){ rc-web@69: request.get(TEST_SERVER + '/notthere/', this.callback); // with trailing slash rc-web@69: }, rc-web@69: 'should respond with 404' : function(error, response, body){ rc-web@69: assert.equal(response.statusCode, 404); rc-web@69: } rc-web@69: } rc-web@69: }) rc-web@69: .addBatch({ rc-web@69: 'requesting a subdirectory (without trailing slash) not found': { rc-web@69: topic : function(){ rc-web@69: request.get({ url: TEST_SERVER + '/notthere', followRedirect: false }, this.callback); // without trailing slash rc-web@69: }, rc-web@69: 'should respond with 404' : function(error, response, body){ rc-web@69: assert.equal(response.statusCode, 404); rc-web@69: } rc-web@69: } rc-web@69: }).export(module); rc-web@69: