ORANGE MOCHA FRAPPUCCINO

A little library to build http verification tests quickly on top of mocha. Testing is a party, and everyone is invited!

install
npm install omf
use
var omf = require('omf');
var app = require();
omf(app, function(client) {
client.get('/', function(response) {
response.has.statusCode(200);
response.is.html();
response.has.body('O hai!');
});
client.get('/index.js', function(response) {
response.has.statusCode(200);
response.has.header('content-type', 'application/javascript');
});
});
Instead of passing in an express or http server listener as the first parameter, we can pass in the base url of any public website, and all the tests can still run. This way you can swap out your development app for one in production and use the same http tests as a verification tool for your live site.
var omf = require('omf');
var assert = require('assert');
omf('https://github.com', function(client) {
client.get('/brianc/node-omf', function(response){
response.has.statusCode(200);
response.has.body('ORANGE MOCHA FRAPPUCCINO');
it('has ETag header', function() {
assert(this.response.headers['etag']).ok();
});
});
});
ORANGE MOCHA FRAPPUCCINO just uses a tiny bit of meta-programmy-bla-bla to reduce the boiler plate in set up & tear down of http tests...but you still have the entire mocha framework at your disposal. For example, you can still nest sub-contexts, do more setup/teardown, or whatever!
var omf = require('omf');
var assert = require('assert');
omf('https://github.com', function(client) {
describe('when not signed in', function() {
before(function(done) {
process.nextTick(function() {
done();
});
});
client.get('/brianc/node-omf', function(response) {
response.has.statusCode(200);
});
});
});
Also, the requests are all sent using the request library. Anything you pass as the optional 2nd parameter will be passed straight into the request module.
omf('https://some-awesome-json-web-service.com', function(client) {
describe('with json request', function() {
var options = { json: true };
get('/user1.json', options, function(res) {
res.has.statusCode(200);
});
});
describe('with non-json request', function() {
get('/user1.json', function(res) {
res.has.statusCode(406);
});
});
});
Shorthand to just do the bare minimum sanity checks. These are all equal.
omf(app, function(app) {
app.get('/', function(res) {
res.has.statusCode(200);
});
app.get('/', 200);
app.get('/');
});
Sometimes you do not know the url ahead of time. ORANGE MOCHA FRAPPUCCINO supplies you with the same request object used in other tests so you can share a cookie jar and other things. Example:
omf(app, function(app) {
var credentials = {
email: 'test@example.com',
password: 'pass'
}
app.post('/', {json: credentials}, function(res) {
res.has.statusCode(200);
});
var post = {
text: 'I love to write posts about things'
};
app.post('/posts', {json: postJson}, function(res) {
res.has.statusCode(201);
it('has post body and id', function() {
var savedPost = this.response.body;
assert(savedPost.id, 'created savedPost should have a body');
assert.equal(savedPost.text, post.text);
post.id = savedPost.id;
});
});
describe('getting saved post', function() {
var optionsBuilder = function() {
return {
path: '/posts/' + post.id
}
};
app.get(optionsBuilder, function(res) {
res.has.statusCode(200);
it('returns saved post', function() {
var savedPost = res.body;
assert.equal(savedPost.text, post.text);
})
});
});
});
license
Copyright (c) 2012 Brian M. Carlson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.