Permalink
Browse files

add tests and documentation for enhanced delay and custom response

  • Loading branch information...
ekashida committed Apr 1, 2013
1 parent 4d9cb23 commit f3ed04ba5370944b7b1aac44345aaeac2176a6fc
Showing with 114 additions and 0 deletions.
  1. +23 −0 README.md
  2. +4 −0 tests/fixtures/file.json
  3. +87 −0 tests/tests.js
View
@@ -32,6 +32,29 @@ Default Routes
* `json` - Send query parameters or POST parameters and get them back as JSON
* `jsonp` - Send query parameters or POST parameters and get them back as JSON (pass a GET param of `callback=[yourcallback]`
+Delayed Responses
+-----------------
+
+You can delay an `echoecho` request with the `delay` route followed by a value
+in seconds (e.g., `/delay/3`) or a range in seconds (e.g., `/delay/1-3`). Given
+a range, the response will be delayed by a random period of time within the
+range.
+
+You can also delay any route by prepending the delay route (e.g.,
+`/delay/2/get`, `/delay/1-2/json?response={"hello":"world"}`, etc).
+
+Customized Responses
+--------------------
+
+You can customize the response content for any route (except for `status`)
+by specifying either a `response` or `file` query parameter.
+
+The `response` query parameter lets you specify the custom response in the URL,
+whereas the `file` query parameter will attempt to read a file on the server.
+
+The above ways of specifying custom responses will also work with any delayed
+route.
+
Using in Your Server
--------------------
View
@@ -0,0 +1,4 @@
+{
+ "diaper": "pampers",
+ "wipes": "kirkland"
+}
View
@@ -445,6 +445,16 @@ var tests = {
assert.equal(topic.body, topic.expected);
}
},
+ 'and handle invalid custom json': {
+ topic: function() {
+ fetch({
+ path: '/foo/bar/baz/echo/json?response=invalidJSON'
+ }, this.callback);
+ },
+ 'with query body': function(res) {
+ assert.equal(res.body, 'Bad Request');
+ }
+ },
"and post custom json, express style": {
topic: function() {
var self = this,
@@ -605,6 +615,20 @@ var tests = {
assert.equal(topic.body, 'waited for 3 seconds');
}
},
+ 'and delay by a range of seconds': {
+ topic: function() {
+ fetch({
+ method: 'GET',
+ path: '/foo/bar/baz/echo/delay/1-3'
+ }, this.callback);
+ },
+ 'with query body': function(res) {
+ var delay = res.body.match(/[\d.]+/);
+ delay = delay && parseFloat(delay);
+ assert(delay <= 3, 'delay should be less than 3 seconds');
+ assert(delay >= 1, 'delay should be more than 1 second');
+ }
+ },
'should be instantitable': {
'should be different objects': {
topic: function() {
@@ -728,5 +752,68 @@ tests["should be loaded"]['and should load paths']["should load custom scheme"]
};
+// -- `response` query parameter tests ----------------------------------------
+
+function assertResponseParam(route, method) {
+ var json = '{"diaper":"huggies"}';
+ path = '/foo/bar/baz/echo/' + route + '?response=' + json
+ context = {
+ topic: function() {
+ fetch({
+ method: method,
+ path: path
+ }, this.callback);
+ }
+ };
+
+ context['delayed ' + route + ' route should respond'] = function (res) {
+ assert.equal(res.body, json, 'should have responded custom response');
+ }
+
+ return context;
+}
+
+tests['should be loaded']
+ ['and should load paths']
+ ['and should use custom response (query)'] = {
+ 'get': assertResponseParam('get', 'GET'),
+ 'post': assertResponseParam('post', 'POST'),
+ 'json': assertResponseParam('json', 'GET'),
+ 'put': assertResponseParam('post', 'PUT'),
+ 'delete': assertResponseParam('delete', 'DELETE')
+};
+
+// -- `file` query parameter tests --------------------------------------------
+
+var file = fs.readFileSync(__dirname + '/fixtures/file.json', 'utf8');
+
+function assertFileParam(route, method) {
+ var path = '/foo/bar/baz/echo/' + route + '?file=../tests/fixtures/file.json'
+ context = {
+ topic: function() {
+ fetch({
+ method: method,
+ path: path
+ }, this.callback);
+ }
+ };
+
+ context['delayed ' + route + ' route should respond'] = function (res) {
+ assert.equal(res.body, file, 'should have responded custom response');
+ }
+
+ return context;
+}
+
+tests['should be loaded']
+ ['and should load paths']
+ ['and should use custom response (file)'] = {
+ 'get': assertFileParam('get', 'GET'),
+ 'post': assertFileParam('post', 'POST'),
+ 'json': assertFileParam('json', 'GET'),
+ 'put': assertFileParam('post', 'PUT'),
+ 'delete': assertFileParam('delete', 'DELETE')
+};
+
vows.describe('echoecho').addBatch(tests).export(module);

0 comments on commit f3ed04b

Please sign in to comment.