Permalink
Browse files

initial commit

  • Loading branch information...
brianc committed Oct 18, 2012
0 parents commit 1494150d9a06419de2df2210c8b0e919fd059c16
Showing with 189 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +111 −0 lib/index.js
  3. +30 −0 package.json
  4. +10 −0 test/app/app.js
  5. +5 −0 test/app/index.js
  6. +13 −0 test/index.js
  7. +1 −0 test/mocha.opts
  8. +18 −0 test/public.js
@@ -0,0 +1 @@
+node_modules/
@@ -0,0 +1,111 @@
+var assert = require('assert');
+
+var Client = function(app) {
+ this.app = app;
+ this.port = 3212;
+ if(this.isRemote()) return;
+ this.server = require('http').createServer(this.app);
+};
+
+Client.prototype.isRemote = function() {
+ return typeof this.app === 'string';
+}
+
+Client.prototype.start = function(cb) {
+ if(this.isRemote()) return cb();
+ this.server.listen(this.port, cb);
+};
+
+Client.prototype.stop = function(cb) {
+ if(this.isRemote()) return cb();
+ this.server.close(cb);
+};
+
+Client.prototype.request = function(path, options, cb) {
+ if(typeof options === 'function') {
+ cb = options;
+ options = {
+ };
+ }
+ options.path = path;
+ options.host = 'localhost';
+ options.port = this.port;
+ if(this.isRemote()) {
+ var parts = require('url').parse(this.app + path);
+ options.path = parts.pathname;
+ options.host = parts.hostname;
+ options.port = parts.port;
+ options.query = parts.query;
+ }
+ options.method = options.method || 'GET';
+ var req = require('http').request(options, function(res) {
+ res.body = '';
+
+ res.on('data', function(buffer) {
+ res.body += buffer.toString('utf8');
+ });
+
+ res.on('end', function() {
+ cb(null, res);
+ });
+
+ res.on('error', cb);
+ });
+ req.on('error', cb);
+ req.end();
+};
+
+Client.test = function(app, cb) {
+ var client = new Client(app);
+ describe('site', function() {
+ before(function(done) {
+ client.start(done);
+ });
+ after(function(done) {
+ client.stop(done);
+ });
+ cb(client);
+ });
+};
+
+Client.prototype.get = function(path, cb) {
+ var client = this;
+ describe('GET ' + path, function() {
+ before(function(done) {
+ var self = this;
+ client.request(path, function(err, res) {
+ self.response = res;
+ done(err);
+ });
+ });
+ var should = {
+ statusCode: function(code) {
+ it('has statusCode ' + code, function() {
+ assert.equal(this.response.statusCode, code);
+ });
+ },
+ header: function(name, val) {
+ it('has header "'+name+'" with value "'+val+'"', function() {
+ assert(this.response.headers[name], 'expected header "'+name+' to exist"');
+ assert(this.response.headers[name].indexOf(val) > -1, 'expected header "'+name+'" to contain "'+val+'" but it is "'+this.response.headers[name]+'"')
+ });
+ },
+ body: function(content) {
+ it('has body content "'+content+'"', function() {
+ assert(this.response.body.indexOf(content) > -1);
+ });
+ }
+ };
+ should.contentType = should.header.bind(should, 'content-type');
+ should.css = should.contentType.bind(should, 'text/css');
+ should.html = should.contentType.bind(should, 'text/html');
+ should.have = should;
+ should.be = should;
+ should.has = should;
+ should.is = should;
+ should.should = should;
+ cb(should);
+ });
+};
+
+module.exports = Client;
@@ -0,0 +1,30 @@
+{
+ "name": "omf",
+ "version": "0.0.1",
+ "description": "ORANGE MOCHA FRAPPUCCINO!!!11",
+ "main": "lib/",
+ "scripts": {
+ "test": "mocha -R list"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/brianc/node-omf"
+ },
+ "keywords": [
+ "testing",
+ "integration testing",
+ "functional testing",
+ "http testing",
+ "you do the jitterbug",
+ "A model idiot?"
+ ],
+ "author": "Brian M. Carlson",
+ "license": "MIT",
+ "dependencies": {
+ "mocha": "*",
+ "should": "*"
+ },
+ "devDependencies": {
+ "express": "*"
+ }
+}
@@ -0,0 +1,10 @@
+var express = require('express');
+var app = express();
+
+app.use(express.static(__dirname + '/public'));
+
+app.get('/', function(req, res, next) {
+ res.send('O hai!');
+});
+
+module.exports = app;
@@ -0,0 +1,5 @@
+var port = process.argv[2]||3000;
+
+require('./app').listen(port, function() {
+ console.log('listening on %d', port);
+});
@@ -0,0 +1,13 @@
+var omf = require('./../');
+
+var app = require('./app/app');
+
+omf.test(app, function(app) {
+
+ app.get('/', function(response) {
+ response.has.statusCode(200);
+ response.is.html();
+ response.has.body('O hai!');
+ });
+
+});
@@ -0,0 +1 @@
+-R list
@@ -0,0 +1,18 @@
+//require our client helper
+var client = require('./../');
+
+//initialize our test client
+client.test('http://bmc.io', function(app) {
+
+ app.get('/', function(response) {
+ response.has.statusCode(200);
+ response.is.html();
+ response.has.body('O hai!');
+ });
+
+ app.get('/index.css', function(response) {
+ response.has.statusCode(200);
+ response.is.css();
+ });
+
+});

0 comments on commit 1494150

Please sign in to comment.