Skip to content
Browse files

Added some system tests to support refactoring.

  • Loading branch information...
1 parent 9eb5a4b commit f951ca4023086936163e8135abd7e90cbb2d807c @tuxychandru committed
View
BIN test/system/dispatch/statics/test.pdf
Binary file not shown.
View
41 test/system/dispatch/test-routes.js
@@ -0,0 +1,41 @@
+var gh = require('../../../grasshopper');
+
+gh.configure({
+ viewsDir: __dirname + '/views',
+ staticsDir: __dirname + '/statics'
+});
+
+gh.addFilters(/\/filtered/, function(next) {
+ this.renderText('Filtered');
+});
+
+gh.get('/', function() {
+ this.renderText(this.params['name']);
+});
+
+gh.get('/args_supported/{name}', function(args) {
+ this.renderText(args['name']);
+});
+
+gh.get('/templated/{name}', function(args) {
+ this.model['name'] = args['name'];
+ this.render('simple_template');
+});
+
+gh.post('/', function() {
+ this.renderText(this.params['name']);
+});
+
+gh.get('/filtered', function(args) {
+ this.renderText('Filter missed.');
+});
+
+gh.post('/upload', function() {
+ this.renderText('Received: ' + this.params['name'] + ', '
+ + this.params['file'].filename + ' - '
+ + this.params['file'].length);
+});
+
+gh.get('/send_file', function(args) {
+ this.sendFile(__dirname + '/statics/test.pdf');
+});
View
127 test/system/dispatch/test.js
@@ -0,0 +1,127 @@
+var assert = require('assert'),
+ http = require('http'),
+ fs = require('fs'),
+ testUtil = require('../test-util'),
+ gh = require('../../../grasshopper');
+
+require('./test-routes');
+
+var suite = {name: 'Dispatch System Tests'};
+exports.suite = suite;
+
+suite.setupOnce = function(next) {
+ gh.serve(8080, function() {
+ next();
+ });
+};
+
+suite.tests = {
+ 'GET with param.': function(next) {
+ testUtil.invoke('GET', '/?name=Chandru', {}, function(res) {
+ res.on('data', function(chunk) {
+ assert.equal(chunk, 'Chandru');
+ next();
+ });
+ });
+ },
+
+ 'GET with URL argument.': function(next) {
+ testUtil.invoke('GET', '/args_supported/Chandru', {}, function(res) {
+ res.on('data', function(chunk) {
+ assert.equal(chunk, 'Chandru');
+ next();
+ });
+ });
+ },
+
+ 'GET with template.': function(next) {
+ testUtil.invoke('GET', '/templated/Chandru', {}, function(res) {
+ res.on('data', function(chunk) {
+ assert.equal(chunk, 'Hello, Chandru!\n');
+ next();
+ });
+ });
+ },
+
+ 'POST.': function(next) {
+ testUtil.invoke('POST', '/', {}, 'name=Chandru', function(res) {
+ res.on('data', function(chunk) {
+ assert.equal(chunk, 'Chandru');
+ next();
+ });
+ });
+ },
+
+ 'Filtered GET.': function(next) {
+ testUtil.invoke('GET', '/filtered', {}, function(res) {
+ res.on('data', function(chunk) {
+ assert.equal(chunk, 'Filtered');
+ next();
+ });
+ });
+ },
+
+ 'Multi-part POST.': function(next) {
+ var boundary = '------AABBCC';
+
+ var init = '--' + boundary + '\r\n';
+ init += 'Content-Disposition: form-data; name="name"\r\n';
+ init += 'Content-Type: text/plain; charset=ISO-8859-1\r\n';
+ init += 'Content-Transfer-Encoding: 8bit\r\n\r\n';
+ init += 'Chandru\r\n';
+ init += '--' + boundary + '\r\n';
+ init += 'Content-Disposition: form-data; name="file"; '
+ + 'filename="test.pdf"';
+ init += '\r\nContent-Type: \r\n';
+ init += 'Content-Transfer-Encoding: binary\r\n\r\n';
+
+ var body = fs.readFileSync(__dirname + '/statics/test.pdf');
+
+ var end = '\r\n--' + boundary + '--\r\n';
+
+ var bodyLength = Buffer.byteLength(init)
+ + body.length + Buffer.byteLength(end);
+
+ var headers = {
+ 'content-length': bodyLength,
+ 'content-type': 'multipart/form-data; boundary=' + boundary,
+ };
+
+ var bodyBuf = new Buffer(bodyLength);
+ bodyBuf.write(init);
+ body.copy(bodyBuf, Buffer.byteLength(init), 0);
+ bodyBuf.write(end, Buffer.byteLength(init) + body.length);
+
+ testUtil.invoke('POST', '/upload', headers, bodyBuf,function(res) {
+ res.on('data', function(chunk) {
+ assert.equal(chunk, 'Received: Chandru, test.pdf - '
+ + body.length);
+ next();
+ });
+ });
+ },
+
+ 'Static file.': function(next) {
+ testUtil.invoke('GET', '/test.pdf', {}, function(res) {
+ assert.equal(res.headers['content-length'], 51560);
+ next();
+ });
+ },
+
+ 'Send static file.': function(next) {
+ testUtil.invoke('GET', '/send_file', {}, function(res) {
+ assert.equal(res.headers['content-disposition'],
+ 'attachment; filename="test.pdf"');
+ assert.equal(res.headers['content-length'], 51560);
+ next();
+ });
+ }
+}
+
+suite.tearDownOnce = function(next) {
+ gh.stop();
+ next();
+};
+
+if(process.argv[1] == __filename)
+ require('../../common/ghunit').test(suite);
View
1 test/system/dispatch/views/simple_template.html
@@ -0,0 +1 @@
+Hello, <%= name %>!
View
26 test/system/test-util.js
@@ -0,0 +1,26 @@
+var http = require('http');
+
+exports.invoke = function(method, path, headers, body, cb) {
+ if(method == 'POST' && headers['content-type'] === undefined) {
+ headers['content-type'] = 'application/x-www-form-urlencoded';
+ }
+ if(method == 'POST' && headers['content-length'] === undefined) {
+ headers['content-length'] = Buffer.byteLength(body);
+ }
+ if(typeof body == 'function') {
+ cb = body;
+ body = undefined;
+ }
+
+ var req = http.createClient(8080, 'localhost')
+ .request(method, path, headers);
+ req.on('response', function(res) {
+ res.setEncoding('utf8');
+ cb(res);
+ });
+
+ if(body) {
+ req.write(body, 'utf8');
+ }
+ req.end();
+}

0 comments on commit f951ca4

Please sign in to comment.
Something went wrong with that request. Please try again.