Path: blob/master/node_modules/node-static/test/integration/node-static-test.js
335 views
var vows = require('vows')1, request = require('request')2, assert = require('assert')3, static = require('../../lib/node-static');45var fileServer = new static.Server(__dirname + '/../fixtures');6var suite = vows.describe('node-static');7var TEST_PORT = 8080;8var TEST_SERVER = 'http://localhost:' + TEST_PORT;9var version = static.version.join('.');10var server;11var callback;1213headers = {14'requesting headers': {15topic : function(){16request.head(TEST_SERVER + '/index.html', this.callback);17}18}19}20headers['requesting headers']['should respond with node-static/' + version] = function(error, response, body){21assert.equal(response.headers['server'], 'node-static/' + version);22}2324suite.addBatch({25'once an http server is listening with a callback': {26topic: function () {27server = require('http').createServer(function (request, response) {28fileServer.serve(request, response, function(err, result) {29if (callback)30callback(request, response, err, result);31else32request.end();33});34}).listen(TEST_PORT, this.callback)35},36'should be listening' : function(){37/* This test is necessary to ensure the topic execution.38* A topic without tests will be not executed */39assert.isTrue(true);40}41},42}).addBatch({43'streaming a 404 page': {44topic: function(){45callback = function(request, response, err, result) {46if (err) {47response.writeHead(err.status, err.headers);48setTimeout(function() {49response.end('Custom 404 Stream.')50}, 100);51}52}53request.get(TEST_SERVER + '/not-found', this.callback);54},55'should respond with 404' : function(error, response, body){56assert.equal(response.statusCode, 404);57},58'should respond with the streamed content': function(error, response, body){59callback = null;60assert.equal(body, 'Custom 404 Stream.');61}62}63}).addBatch({64'once an http server is listening without a callback': {65topic: function () {66server.close();67server = require('http').createServer(function (request, response) {68fileServer.serve(request, response);69}).listen(TEST_PORT, this.callback)70},71'should be listening' : function(){72/* This test is necessary to ensure the topic execution.73* A topic without tests will be not executed */74assert.isTrue(true);75}76}77}).addBatch({78'requesting a file not found': {79topic : function(){80request.get(TEST_SERVER + '/not-found', this.callback);81},82'should respond with 404' : function(error, response, body){83assert.equal(response.statusCode, 404);84}85}86})87.addBatch({88'requesting a malformed URI': {89topic: function(){90request.get(TEST_SERVER + '/a%AFc', this.callback);91},92'should respond with 400': function(error, response, body){93assert.equal(response.statusCode, 400);94}95}96})97.addBatch({98'serving empty.css': {99topic : function(){100request.get(TEST_SERVER + '/empty.css', this.callback);101},102'should respond with 200' : function(error, response, body){103assert.equal(response.statusCode, 200);104},105'should respond with text/css': function(error, response, body){106assert.equal(response.headers['content-type'], 'text/css');107},108'should respond with empty string': function(error, response, body){109assert.equal(body, '');110}111}112})113.addBatch({114'serving hello.txt': {115topic : function(){116request.get(TEST_SERVER + '/hello.txt', this.callback);117},118'should respond with 200' : function(error, response, body){119assert.equal(response.statusCode, 200);120},121'should respond with text/plain': function(error, response, body){122assert.equal(response.headers['content-type'], 'text/plain');123},124'should respond with hello world': function(error, response, body){125assert.equal(body, 'hello world');126}127}128}).addBatch({129'serving first 5 bytes of hello.txt': {130topic : function(){131var options = {132url: TEST_SERVER + '/hello.txt',133headers: {134'Range': 'bytes=0-4'135}136};137request.get(options, this.callback);138},139'should respond with 206' : function(error, response, body){140assert.equal(response.statusCode, 206);141},142'should respond with text/plain': function(error, response, body){143assert.equal(response.headers['content-type'], 'text/plain');144},145'should have content-length of 5 bytes': function(error, response, body){146assert.equal(response.headers['content-length'], 5);147},148'should have a valid Content-Range header in response': function(error, response, body){149assert.equal(response.headers['content-range'], 'bytes 0-4/11');150},151'should respond with hello': function(error, response, body){152assert.equal(body, 'hello');153}154}155}).addBatch({156'serving last 5 bytes of hello.txt': {157topic : function(){158var options = {159url: TEST_SERVER + '/hello.txt',160headers: {161'Range': 'bytes=6-10'162}163};164request.get(options, this.callback);165},166'should respond with 206' : function(error, response, body){167assert.equal(response.statusCode, 206);168},169'should respond with text/plain': function(error, response, body){170assert.equal(response.headers['content-type'], 'text/plain');171},172'should have content-length of 5 bytes': function(error, response, body){173assert.equal(response.headers['content-length'], 5);174},175'should have a valid Content-Range header in response': function(error, response, body){176assert.equal(response.headers['content-range'], 'bytes 6-10/11');177},178'should respond with world': function(error, response, body){179assert.equal(body, 'world');180}181}182}).addBatch({183'serving all from the start of hello.txt': {184topic : function(){185var options = {186url: TEST_SERVER + '/hello.txt',187headers: {188'Range': 'bytes=0-'189}190};191request.get(options, this.callback);192},193'should respond with 206' : function(error, response, body){194assert.equal(response.statusCode, 206);195},196'should respond with text/plain': function(error, response, body){197assert.equal(response.headers['content-type'], 'text/plain');198},199'should have content-length of 11 bytes': function(error, response, body){200assert.equal(response.headers['content-length'], 11);201},202'should have a valid Content-Range header in response': function(error, response, body){203assert.equal(response.headers['content-range'], 'bytes 0-10/11');204},205'should respond with "hello world"': function(error, response, body){206assert.equal(body, 'hello world');207}208}209}).addBatch({210'serving directory index': {211topic : function(){212request.get(TEST_SERVER, this.callback);213},214'should respond with 200' : function(error, response, body){215assert.equal(response.statusCode, 200);216},217'should respond with text/html': function(error, response, body){218assert.equal(response.headers['content-type'], 'text/html');219}220}221}).addBatch({222'serving index.html from the cache': {223topic : function(){224request.get(TEST_SERVER + '/index.html', this.callback);225},226'should respond with 200' : function(error, response, body){227assert.equal(response.statusCode, 200);228},229'should respond with text/html': function(error, response, body){230assert.equal(response.headers['content-type'], 'text/html');231}232}233}).addBatch({234'requesting with If-None-Match': {235topic : function(){236var _this = this;237request.get(TEST_SERVER + '/index.html', function(error, response, body){238request({239method: 'GET',240uri: TEST_SERVER + '/index.html',241headers: {'if-none-match': response.headers['etag']}242},243_this.callback);244});245},246'should respond with 304' : function(error, response, body){247assert.equal(response.statusCode, 304);248}249},250'requesting with If-None-Match and If-Modified-Since': {251topic : function(){252var _this = this;253request.get(TEST_SERVER + '/index.html', function(error, response, body){254var modified = Date.parse(response.headers['last-modified']);255var oneDayLater = new Date(modified + (24 * 60 * 60 * 1000)).toUTCString();256var nonMatchingEtag = '1111222233334444';257request({258method: 'GET',259uri: TEST_SERVER + '/index.html',260headers: {261'if-none-match': nonMatchingEtag,262'if-modified-since': oneDayLater263}264},265_this.callback);266});267},268'should respond with a 200': function(error, response, body){269assert.equal(response.statusCode, 200);270}271}272})273.addBatch({274'requesting POST': {275topic : function(){276request.post(TEST_SERVER + '/index.html', this.callback);277},278'should respond with 200' : function(error, response, body){279assert.equal(response.statusCode, 200);280},281'should not be empty' : function(error, response, body){282assert.isNotEmpty(body);283}284}285})286.addBatch({287'requesting HEAD': {288topic : function(){289request.head(TEST_SERVER + '/index.html', this.callback);290},291'should respond with 200' : function(error, response, body){292assert.equal(response.statusCode, 200);293},294'head must has no body' : function(error, response, body){295assert.isEmpty(body);296}297}298})299.addBatch(headers)300.addBatch({301'addings custom mime types': {302topic : function(){303static.mime.define({'application/font-woff': ['woff']});304this.callback();305},306'should add woff' : function(error, response, body){307assert.equal(static.mime.lookup('woff'), 'application/font-woff');308}309}310})311.addBatch({312'serving subdirectory index': {313topic : function(){314request.get(TEST_SERVER + '/there/', this.callback); // with trailing slash315},316'should respond with 200' : function(error, response, body){317assert.equal(response.statusCode, 200);318},319'should respond with text/html': function(error, response, body){320assert.equal(response.headers['content-type'], 'text/html');321}322}323})324.addBatch({325'redirecting to subdirectory index': {326topic : function(){327request.get({ url: TEST_SERVER + '/there', followRedirect: false }, this.callback); // without trailing slash328},329'should respond with 301' : function(error, response, body){330assert.equal(response.statusCode, 301);331},332'should respond with location header': function(error, response, body){333assert.equal(response.headers['location'], '/there/'); // now with trailing slash334},335'should respond with empty string body' : function(error, response, body){336assert.equal(body, '');337}338}339})340.addBatch({341'requesting a subdirectory (with trailing slash) not found': {342topic : function(){343request.get(TEST_SERVER + '/notthere/', this.callback); // with trailing slash344},345'should respond with 404' : function(error, response, body){346assert.equal(response.statusCode, 404);347}348}349})350.addBatch({351'requesting a subdirectory (without trailing slash) not found': {352topic : function(){353request.get({ url: TEST_SERVER + '/notthere', followRedirect: false }, this.callback); // without trailing slash354},355'should respond with 404' : function(error, response, body){356assert.equal(response.statusCode, 404);357}358}359}).addBatch({360'once an http server is listening with custom index configuration': {361topic: function () {362server.close();363364fileServer = new static.Server(__dirname + '/../fixtures', { indexFile: "hello.txt" });365366server = require('http').createServer(function (request, response) {367fileServer.serve(request, response);368}).listen(TEST_PORT, this.callback)369},370'should be listening' : function(){371/* This test is necessary to ensure the topic execution.372* A topic without tests will be not executed */373assert.isTrue(true);374}375}376}).addBatch({377'serving custom index file': {378topic : function(){379request.get(TEST_SERVER + '/', this.callback);380},381'should respond with 200' : function(error, response, body){382assert.equal(response.statusCode, 200);383},384'should respond with empty string': function(error, response, body){385assert.equal(body, 'hello world');386}387}388}).export(module);389390391392