Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test infrasctructure copied from https://github.com/senchalabs/connect
- Loading branch information
Showing
8 changed files
with
206 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
body{color:"green";} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
compressed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
body{color:"red";} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,77 @@ | |||
var gzipStatic = require('..'); | |||
|
|||
var connect = require('connect'); | |||
var fixtures = __dirname + '/fixtures'; | |||
var app = connect(); | |||
|
|||
/* global describe, it */ | |||
|
|||
|
|||
app.use(gzipStatic(fixtures)); | |||
|
|||
app.use(function(req, res){ | |||
res.statusCode = 404; | |||
res.end('sorry!'); | |||
}); | |||
|
|||
describe('gzipStatic', function(){ | |||
it('should serve static files', function(done){ | |||
app.request() | |||
.get('/style.css') | |||
.expect('body{color:"red";}', done); | |||
}); | |||
|
|||
it('should set Content-Type', function(done){ | |||
app.request() | |||
.get('/style.css') | |||
.expect('Content-Type', 'text/css; charset=UTF-8', done); | |||
}); | |||
|
|||
it('should default max-age=0', function(done){ | |||
app.request() | |||
.get('/style.css') | |||
.expect('Cache-Control', 'public, max-age=0', done); | |||
}); | |||
|
|||
it('should serve uncompressed files unless requested', function(done){ | |||
app.request() | |||
.get('/print.css') | |||
.expect('body{color:"green";}', done); | |||
}); | |||
|
|||
it('should serve compressed files when requested', function(done){ | |||
app.request() | |||
.set('Accept-Encoding', 'gzip') | |||
.get('/print.css') | |||
.expect('compressed', done); | |||
}); | |||
|
|||
it('should set Content-Type for compressed files', function(done){ | |||
app.request() | |||
.set('Accept-Encoding', 'gzip') | |||
.get('/print.css') | |||
.expect('Content-Type', 'text/css; charset=UTF-8', done); | |||
}); | |||
|
|||
it('should set Vary for compressed files', function(done){ | |||
app.request() | |||
.set('Accept-Encoding', 'gzip') | |||
.get('/print.css') | |||
.expect('Vary', 'Accept-Encoding', done); | |||
}); | |||
|
|||
it('should set Content-Encoding for compressed files', function(done){ | |||
app.request() | |||
.set('Accept-Encoding', 'gzip') | |||
.get('/print.css') | |||
.expect('Content-Encoding', 'gzip', done); | |||
}); | |||
|
|||
it('should ignore POST requests', function(done){ | |||
app.request() | |||
.set('Accept-Encoding', 'gzip') | |||
.post('/print.css') | |||
.expect(404, done); | |||
}); | |||
|
|||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,2 @@ | |||
--require should | |||
--require test/support/http |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,116 @@ | |||
|
|||
/** | |||
* Module dependencies. | |||
*/ | |||
|
|||
var EventEmitter = require('events').EventEmitter | |||
, methods = ['get', 'post', 'put', 'delete', 'head'] | |||
, connect = require('connect') | |||
, http = require('http'); | |||
|
|||
module.exports = request; | |||
|
|||
connect.proto.request = function(){ | |||
return request(this); | |||
}; | |||
|
|||
function request(app) { | |||
return new Request(app); | |||
} | |||
|
|||
function Request(app) { | |||
var self = this; | |||
this.data = []; | |||
this.header = {}; | |||
this.app = app; | |||
if (!this.server) { | |||
this.server = http.Server(app); | |||
this.server.listen(0, function(){ | |||
self.addr = self.server.address(); | |||
self.listening = true; | |||
}); | |||
} | |||
} | |||
|
|||
/** | |||
* Inherit from `EventEmitter.prototype`. | |||
*/ | |||
|
|||
Request.prototype.__proto__ = EventEmitter.prototype; | |||
|
|||
methods.forEach(function(method){ | |||
Request.prototype[method] = function(path){ | |||
return this.request(method, path); | |||
}; | |||
}); | |||
|
|||
Request.prototype.set = function(field, val){ | |||
this.header[field] = val; | |||
return this; | |||
}; | |||
|
|||
Request.prototype.write = function(data){ | |||
this.data.push(data); | |||
return this; | |||
}; | |||
|
|||
Request.prototype.request = function(method, path){ | |||
this.method = method; | |||
this.path = path; | |||
return this; | |||
}; | |||
|
|||
Request.prototype.expect = function(body, fn){ | |||
var args = arguments; | |||
this.end(function(res){ | |||
switch (args.length) { | |||
case 3: | |||
res.headers.should.have.property(body.toLowerCase(), args[1]); | |||
args[2](); | |||
break; | |||
default: | |||
if ('number' == typeof body) { | |||
res.statusCode.should.equal(body); | |||
} else { | |||
res.body.should.equal(body); | |||
} | |||
fn(); | |||
} | |||
}); | |||
}; | |||
|
|||
Request.prototype.end = function(fn){ | |||
var self = this; | |||
|
|||
if (this.listening) { | |||
var req = http.request({ | |||
method: this.method | |||
, port: this.addr.port | |||
, host: this.addr.address | |||
, path: this.path | |||
, headers: this.header | |||
}); | |||
|
|||
this.data.forEach(function(chunk){ | |||
req.write(chunk); | |||
}); | |||
|
|||
req.on('response', function(res){ | |||
var buf = ''; | |||
res.setEncoding('utf8'); | |||
res.on('data', function(chunk){ buf += chunk; }); | |||
res.on('end', function(){ | |||
res.body = buf; | |||
fn(res); | |||
}); | |||
}); | |||
|
|||
req.end(); | |||
} else { | |||
this.server.on('listening', function(){ | |||
self.end(fn); | |||
}); | |||
} | |||
|
|||
return this; | |||
}; |