Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

dynamic urls

  • Loading branch information...
commit e50899762d05ea1e517a660d7bca21c8facb8e61 1 parent 8c5ca3c
@brianc authored
Showing with 121 additions and 10 deletions.
  1. +58 −1 README.md
  2. +19 −8 lib/index.js
  3. +1 −1  package.json
  4. +43 −0 test/index.js
View
59 README.md
@@ -98,7 +98,7 @@ omf('https://some-awesome-json-web-service.com', function(client) {
Shorthand to just do the bare minimum sanity checks. These are all equal.
```js
-omf(app, function(client) {
+omf(app, function(app) {
app.get('/', function(res) {
res.has.statusCode(200);
});
@@ -109,6 +109,63 @@ omf(app, function(client) {
});
```
+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:
+
+```js
+//test login & post flow
+omf(app, function(app) {
+ var credentials = {
+ email: 'test@example.com',
+ password: 'pass'
+ }
+
+ app.post('/', {json: credentials}, function(res) {
+ res.has.statusCode(200);
+ });
+
+ //at this point we have a login cookie, so lets create
+ //a post for our next test...
+
+ var post = {
+ text: 'I love to write posts about things'
+ };
+ app.post('/posts', {json: postJson}, function(res) {
+ res.has.statusCode(201); //created
+ it('has post body and id', function() {
+ //remember the scope of the 'it' tests have access to the
+ //raw response from `request`
+ var savedPost = this.response.body;
+ assert(savedPost.id, 'created savedPost should have a body');
+ assert.equal(savedPost.text, post.text);
+ //let's save the posts ID to a place our other test can access it
+ post.id = savedPost.id;
+ });
+ });
+
+ //now lets test the fetching of the newly created post
+ //remember, we didn't know the ID beforehand, so we don't know the direct url
+ //we're basically dropping out of what ORANGE MOCHA FRAPPUCCINO gives you
+ //as helpers and doing it ourselves manually but we still have
+ //our applications life-cycle managed and our urls rooted to our
+ //dynamically hosted app
+ 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
MIT
View
27 lib/index.js
@@ -1,7 +1,6 @@
-var request = require('request');
-
-var Client = function(app) {
+var Client = function(app, request) {
this.app = app;
+ this.request = request;
this.port = 3212;
if(this.isRemote()) return;
this.server = require('http').createServer(this.app);
@@ -32,8 +31,13 @@ Client.prototype._request = function(path, options, cb) {
cb = options;
options = {};
}
- options.uri = this.url(path);
- return request(options, function(err, res, body) {
+ if(typeof path === 'function') {
+ options = path();
+ options.uri = this.url(options.path);
+ } else {
+ options.uri = this.url(path);
+ }
+ return this.request(options, function(err, res, body) {
if(err) return cb(err);
res.body = body;
cb(null, res);
@@ -56,8 +60,12 @@ Client.prototype.req = function(method, path, options, cb) {
o = {};
}
o.method = method;
+ var name = path;
+ if(typeof name === 'function') {
+ name = '<dynamic>';
+ }
var client = this;
- describe(method + (method.length == 3 ? ' ' : ' ')+ path, function() {
+ describe(method + (method.length == 3 ? ' ' : ' ') + name, function() {
before(function(done) {
var self = this;
client._request(path, o, function(err, res) {
@@ -80,10 +88,13 @@ Client.prototype.get = function(path, options, cb) {
Client.prototype.post = function(path, options, cb) {
this.req('POST', path, options, cb);
-}
+};
+
+Client.prototype.response = require(__dirname + '/assertions');
var test = function(app, cb) {
- var client = new Client(app);
+ var request = require('request');
+ var client = new Client(app, request);
describe('site', function() {
before(function(done) {
client.start(done);
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "omf",
- "version": "0.3.0",
+ "version": "0.4.0",
"description": "ORANGE MOCHA FRAPPUCCINO!!!11",
"main": "lib/",
"scripts": {
View
43 test/index.js
@@ -15,6 +15,49 @@ omf(app, function(app) {
app.get('/', 200);
+ describe('using request directly', function() {
+ before(function(done) {
+ var self = this;
+ app.request.get(app.url('/'), function(err, result) {
+ if(err) return done(err);
+ self.response = result;
+ done();
+ });
+ })
+ app.response.has.statusCode(200);
+ });
+
+ describe('with dynamic builder', function() {
+ var build = function(status) {
+ return function() {
+ return {
+ name: 'dynamicUrl',
+ path: '/status/' + status
+ }
+ }
+ };
+ app.get(build(202), function(res) {
+ res.has.statusCode(202);
+ });
+ });
+
+ describe('with async dynamic builder', function() {
+ return;
+ var build = function(status) {
+ return function(cb) {
+ process.nextTick(function() {
+ cb(null, {
+ name: 'dynamicUrl',
+ path: '/status/' + status
+ });
+ });
+ }
+ };
+ app.get(build(202), function(res) {
+ res.has.statusCode(202);
+ });
+ });
+
app.get('/index.css', function(res) {
res.has.statusCode(200);
res.is.css();
Please sign in to comment.
Something went wrong with that request. Please try again.