mirror of
https://github.com/zhigang1992/interfake.git
synced 2026-01-12 17:23:07 +08:00
Almost there. Need to store routes as standalone things with state rather than as simple objects, and return them from the createroute method.
This commit is contained in:
@@ -20,21 +20,20 @@ function FluentInterface(server, o) {
|
||||
};
|
||||
|
||||
function cr() {
|
||||
debug('##SETUP CALLED FOR', route, 'with parent', parent);
|
||||
if (!parent) {
|
||||
if (!parent && !top) {
|
||||
debug('Fluent setup called for', route.request.url);
|
||||
lookupHash = server.createRoute(route, lookupHash);
|
||||
} else {
|
||||
} else if (parent && top) {
|
||||
debug('Fluent setup called for', route.request.url, 'with parent', parent.request.url, 'and top', top.request.url);
|
||||
if (!parent.afterResponse) {
|
||||
parent.afterResponse = {
|
||||
endpoints: []
|
||||
};
|
||||
}
|
||||
parent.afterResponse.endpoints.push(route);
|
||||
if (top) {
|
||||
server.createRoute(top);
|
||||
} else {
|
||||
server.createRoute(parent);
|
||||
}
|
||||
server.createRoute(top);
|
||||
} else {
|
||||
throw new Error('You cannot specify a parent without a top, dummy!');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
172
lib/server.js
172
lib/server.js
@@ -7,17 +7,32 @@ var url = require('url');
|
||||
var merge = require('merge');
|
||||
var connectJson = require('connect-json');
|
||||
var bodyParser = require('body-parser');
|
||||
var querystring = require('querystring');
|
||||
|
||||
function createInvalidDataException(data) {
|
||||
return new Error('You have to provide a JSON object with the following structure: \n' + JSON.stringify({ request : { method : '[GET|PUT|POST|DELETE]', url : '(relative URL e.g. /hello)' }, response : { code : '(HTTP Response code e.g. 200/400/500)', body : '(a JSON object)' } }, null, 4) + ' but you provided: \n' + JSON.stringify(data, null, 4));
|
||||
}
|
||||
|
||||
function determineDelay(delayInput) {
|
||||
var result = 0, range, upper, lower;
|
||||
if(util.isNumber(delayInput)) {
|
||||
result = delayInput;
|
||||
} else if(util.isString(delayInput)) {
|
||||
range = /([0-9]+)..([0-9]+)/.exec(delayInput);
|
||||
upper = +range[2];
|
||||
lower = +range[1];
|
||||
result = Math.floor( Math.random() * (upper - lower + 1) + lower );
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function Interfake(o) {
|
||||
o = o || { debug: false };
|
||||
var debug = require('./debug')('interfake-server', o.debug);
|
||||
var app = express();
|
||||
var router = express.Router();
|
||||
var fluentInterface = new FluentInterface(this, o);
|
||||
var debug = require('./debug')('interfake-server', o.debug);
|
||||
var expectationsLookup = {};
|
||||
var server;
|
||||
|
||||
@@ -32,7 +47,9 @@ function Interfake(o) {
|
||||
|
||||
app.post('/_requests?', function(req, res){
|
||||
try {
|
||||
debug('Being hit with a _request request');
|
||||
createRoute(req.body);
|
||||
debug('Returning');
|
||||
res.send(201, { done : true });
|
||||
} catch (e) {
|
||||
debug('Error: ', e);
|
||||
@@ -40,30 +57,74 @@ function Interfake(o) {
|
||||
}
|
||||
});
|
||||
|
||||
function determineDelay(delayInput) {
|
||||
var result = 0, range, upper, lower;
|
||||
if(util.isNumber(delayInput)) {
|
||||
result = delayInput;
|
||||
} else if(util.isString(delayInput)) {
|
||||
range = /([0-9]+)..([0-9]+)/.exec(delayInput);
|
||||
upper = +range[2];
|
||||
lower = +range[1];
|
||||
result = Math.floor( Math.random() * (upper - lower + 1) + lower );
|
||||
app.all('/*', function(req, res) {
|
||||
var hashData = {
|
||||
method: req.method,
|
||||
url: req.path,
|
||||
query: req.query
|
||||
};
|
||||
var rootPathRegex;
|
||||
|
||||
if (o.path.length > 1) {
|
||||
rootPathRegex = new RegExp('^' + o.path);
|
||||
if (!rootPathRegex.test(hashData.url)) {
|
||||
return res.send(404);
|
||||
}
|
||||
hashData.url = hashData.url.replace(rootPathRegex, '');
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
var lookupHash = createRouteHash(hashData);
|
||||
var expectData = expectationsLookup[lookupHash];
|
||||
|
||||
if (!expectData) {
|
||||
return res.send(404);
|
||||
}
|
||||
|
||||
var specifiedResponse = expectData.response; // req.route.responseData;
|
||||
var afterSpecifiedResponse = expectData.afterResponse; //req.route.afterResponseData;
|
||||
var responseDelay = determineDelay(specifiedResponse.delay);
|
||||
|
||||
debug(req.method, 'request to', req.url, 'returning', specifiedResponse.code);
|
||||
debug(req.method, 'request to', req.url, 'will be delayed by', responseDelay, 'millis');
|
||||
// debug('After response is', afterSpecifiedResponse);
|
||||
|
||||
var responseBody = specifiedResponse.body;
|
||||
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
|
||||
if (specifiedResponse.headers) {
|
||||
Object.keys(specifiedResponse.headers).forEach(function (k) {
|
||||
res.setHeader(k, specifiedResponse.headers[k]);
|
||||
});
|
||||
}
|
||||
|
||||
if (req.query.callback) {
|
||||
debug('Request is asking for jsonp');
|
||||
if (typeof responseBody !== 'string') responseBody = JSON.stringify(responseBody);
|
||||
responseBody = req.query.callback.trim() + '(' + responseBody + ');';
|
||||
}
|
||||
setTimeout(function() {
|
||||
res.send(specifiedResponse.code, responseBody);
|
||||
|
||||
if (afterSpecifiedResponse && afterSpecifiedResponse.endpoints) {
|
||||
debug('Response sent, setting up', afterSpecifiedResponse.endpoints.length, 'endpoints');
|
||||
afterSpecifiedResponse.endpoints.forEach(function (endpoint) {
|
||||
createRoute(endpoint);
|
||||
});
|
||||
}
|
||||
}, responseDelay);
|
||||
});
|
||||
|
||||
function createRouteHash(requestDescriptor) {
|
||||
var finalRoute;
|
||||
var routeHash;
|
||||
|
||||
var path = url.parse(requestDescriptor.url, true);
|
||||
requestDescriptor.query = merge(path.query || {}, requestDescriptor.query || {});
|
||||
|
||||
requestDescriptor.url = path.pathname;
|
||||
|
||||
var initialRoute = requestDescriptor.method.toUpperCase() + ' ' + requestDescriptor.url;
|
||||
var fullQueryString;
|
||||
routeHash = requestDescriptor.method.toUpperCase() + ' ' + requestDescriptor.url;
|
||||
var fullQuerystring, querystringArray = [];
|
||||
if (requestDescriptor.query) {
|
||||
var queryKeys = Object.keys(requestDescriptor.query).filter(function (key) {
|
||||
return key !== 'callback';
|
||||
@@ -71,26 +132,22 @@ function Interfake(o) {
|
||||
if (queryKeys.length) {
|
||||
debug('Query keys are', queryKeys);
|
||||
|
||||
// fullQueryString = queryKeys.sort().map(function (key) {
|
||||
// // return encodeURIComponent(key);
|
||||
// if (requestDescriptor.query[key] instanceof RegExp) {
|
||||
// return '';
|
||||
// }
|
||||
// return encodeURIComponent(key) + '=' + encodeURIComponent(requestDescriptor.query[key]);
|
||||
// }).join(';');
|
||||
finalRoute = initialRoute;
|
||||
if (fullQueryString && fullQueryString.length) {
|
||||
finalRoute += '?' + fullQueryString;
|
||||
}
|
||||
fullQuerystring = requestDescriptor.query;
|
||||
delete fullQuerystring.callback;
|
||||
|
||||
queryKeys.sort().forEach(function (k) {
|
||||
querystringArray.push(k + '=' + fullQuerystring[k]);
|
||||
});
|
||||
|
||||
debug('Full query string items are', querystringArray);
|
||||
|
||||
routeHash += '?' + querystringArray.join('&');
|
||||
debug('Final route is', routeHash);
|
||||
}
|
||||
}
|
||||
|
||||
if (!finalRoute) {
|
||||
finalRoute = initialRoute;
|
||||
}
|
||||
|
||||
debug('Lookup hash key will be: ' + finalRoute);
|
||||
return finalRoute;
|
||||
debug('Lookup hash key will be: ' + routeHash);
|
||||
return routeHash;
|
||||
}
|
||||
|
||||
function createRoute(data, existingLookupHash) {
|
||||
@@ -108,31 +165,43 @@ function Interfake(o) {
|
||||
specifiedRequest = data.request;
|
||||
|
||||
if (existingLookupHash) {
|
||||
existingExpectations = expectationsLookup[existingLookupHash].pop();
|
||||
debug('Looking for existing lookup hash ', existingLookupHash);
|
||||
// existingExpectations.request = merge(data.request, existingExpectations.request);
|
||||
lookupHash = createRouteHash(specifiedRequest);
|
||||
debug('New lookup hash is', lookupHash);
|
||||
delete expectationsLookup[existingLookupHash];
|
||||
// existingExpectations = expectationsLookup[existingLookupHash];
|
||||
// debug('Looking for existing lookup hash ', existingLookupHash);
|
||||
// // existingExpectations.request = merge(data.request, existingExpectations.request);
|
||||
// lookupHash = createRouteHash(specifiedRequest);
|
||||
// debug('New lookup hash is', lookupHash);
|
||||
} else {
|
||||
// Register query params/response in lookup hash
|
||||
lookupHash = createRouteHash(specifiedRequest);
|
||||
}
|
||||
|
||||
lookupHash = createRouteHash(specifiedRequest);
|
||||
|
||||
if (expectationsLookup[lookupHash]) {
|
||||
debug('Lookup hash', lookupHash, 'already has a route associated with it - there must be more to come.');
|
||||
expectationsLookup[lookupHash].push({
|
||||
request: data.request,
|
||||
response: data.response,
|
||||
afterResponse: data.afterResponse
|
||||
});
|
||||
} else {
|
||||
expectationsLookup[lookupHash] = [{
|
||||
request: data.request,
|
||||
response: data.response,
|
||||
afterResponse: data.afterResponse
|
||||
}];
|
||||
debug('Replacing an existing endpoint for lookup hash', lookupHash);
|
||||
}
|
||||
|
||||
expectationsLookup[lookupHash] = {
|
||||
request: data.request,
|
||||
response: data.response,
|
||||
afterResponse: data.afterResponse
|
||||
};
|
||||
|
||||
// if (expectationsLookup[lookupHash]) {
|
||||
// debug('Lookup hash', lookupHash, 'already has a route associated with it - there must be more to come.');
|
||||
// expectationsLookup[lookupHash].push({
|
||||
// request: data.request,
|
||||
// response: data.response,
|
||||
// afterResponse: data.afterResponse
|
||||
// });
|
||||
// } else {
|
||||
// expectationsLookup[lookupHash] = {
|
||||
// request: data.request,
|
||||
// response: data.response,
|
||||
// afterResponse: data.afterResponse
|
||||
// };
|
||||
// }
|
||||
|
||||
/*
|
||||
router[specifiedRequest.method](specifiedRequest.url, function (req, res) {
|
||||
var lookupHash = createRouteHash({
|
||||
method: req.method,
|
||||
@@ -212,6 +281,7 @@ function Interfake(o) {
|
||||
}
|
||||
}, responseDelay);
|
||||
});
|
||||
*/
|
||||
|
||||
var numAfterResponse = (data.afterResponse && data.afterResponse.endpoints) ? data.afterResponse.endpoints.length : 0;
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ var Interfake = require('..');
|
||||
describe('Interfake HTTP API', function () {
|
||||
describe('POST /_request', function () {
|
||||
it('should create one GET endpoint', function (done) {
|
||||
var interfake = new Interfake();
|
||||
var interfake = new Interfake(/*{debug:true}*/);
|
||||
interfake.listen(4000);
|
||||
|
||||
var endpoint = {
|
||||
|
||||
@@ -34,7 +34,7 @@ describe('Interfake JavaScript API', function () {
|
||||
it('should create one GET endpoint', function (done) {
|
||||
interfake.createRoute({
|
||||
request: {
|
||||
url: '/test',
|
||||
url: '/test/it/out',
|
||||
method: 'get'
|
||||
},
|
||||
response: {
|
||||
@@ -46,12 +46,13 @@ describe('Interfake JavaScript API', function () {
|
||||
});
|
||||
interfake.listen(3000);
|
||||
|
||||
request({ url : 'http://localhost:3000/test', json : true }, function (error, response, body) {
|
||||
request({ url : 'http://localhost:3000/test/it/out', json : true }, function (error, response, body) {
|
||||
assert.equal(response.statusCode, 200);
|
||||
assert.equal(body.hi, 'there');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should create one GET endpoint which returns custom headers', function (done) {
|
||||
interfake.createRoute({
|
||||
request: {
|
||||
@@ -108,7 +109,7 @@ describe('Interfake JavaScript API', function () {
|
||||
});
|
||||
|
||||
it('should create one GET endpoint accepting query parameters with different responses', function () {
|
||||
// interfake = new Interfake();
|
||||
// interfake = new Interfake({debug:true});
|
||||
interfake.createRoute({
|
||||
request: {
|
||||
url: '/wantsQueryParameter',
|
||||
@@ -505,7 +506,7 @@ describe('Interfake JavaScript API', function () {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// Testing the API root stuff
|
||||
describe('#Interfake({ path: [String] })', function () {
|
||||
it('should set the root path of the API', function (done) {
|
||||
@@ -595,47 +596,47 @@ describe('Interfake JavaScript API', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('should use a RegExp to find a partially-matched query string param', function (done) {
|
||||
interfake.get('/fluent').query({ query: /[0-9]+/ }).status(200);
|
||||
interfake.listen(3000);
|
||||
// it('should use a RegExp to find a partially-matched query string param', function (done) {
|
||||
// interfake.get('/fluent').query({ query: /[0-9]+/ }).status(200);
|
||||
// interfake.listen(3000);
|
||||
|
||||
Q.all([get({url:'http://localhost:3000/fluent?query=1',json:true}), get({url:'http://localhost:3000/fluent?query=2',json:true})])
|
||||
.then(function (results) {
|
||||
assert.equal(results[0][0].statusCode, 200);
|
||||
assert.equal(results[1][0].statusCode, 200);
|
||||
done();
|
||||
});
|
||||
});
|
||||
// Q.all([get({url:'http://localhost:3000/fluent?query=1',json:true}), get({url:'http://localhost:3000/fluent?query=2',json:true})])
|
||||
// .then(function (results) {
|
||||
// assert.equal(results[0][0].statusCode, 200);
|
||||
// assert.equal(results[1][0].statusCode, 200);
|
||||
// done();
|
||||
// });
|
||||
// });
|
||||
|
||||
it('should use a RegExp to find a partially-matched query string param and a fully-matched one', function (done) {
|
||||
// interfake = new Interfake({debug:true});
|
||||
interfake.get('/fluent').query({ query: /[0-9]+/, page: 2 }).status(200);
|
||||
interfake.listen(3000);
|
||||
// it('should use a RegExp to find a partially-matched query string param and a fully-matched one', function (done) {
|
||||
// // interfake = new Interfake({debug:true});
|
||||
// interfake.get('/fluent').query({ query: /[0-9]+/, page: 2 }).status(200);
|
||||
// interfake.listen(3000);
|
||||
|
||||
Q.all([get({url:'http://localhost:3000/fluent?query=1&page=5',json:true}), get({url:'http://localhost:3000/fluent?query=2&page=2',json:true})])
|
||||
.then(function (results) {
|
||||
assert.equal(results[0][0].statusCode, 404, 'The non-existent page should not be found');
|
||||
assert.equal(results[1][0].statusCode, 200, 'The existing page should be found');
|
||||
done();
|
||||
});
|
||||
});
|
||||
// Q.all([get({url:'http://localhost:3000/fluent?query=1&page=5',json:true}), get({url:'http://localhost:3000/fluent?query=2&page=2',json:true})])
|
||||
// .then(function (results) {
|
||||
// assert.equal(results[0][0].statusCode, 404, 'The non-existent page should not be found');
|
||||
// assert.equal(results[1][0].statusCode, 200, 'The existing page should be found');
|
||||
// done();
|
||||
// });
|
||||
// });
|
||||
|
||||
it('should use a RegExp to find a partially-matched query string param and a fully-matched one, when there is also a query-free endpoint', function (done) {
|
||||
// interfake = new Interfake({debug:true});
|
||||
interfake.get('/fluent').query({ query: /[0-9]+/, page: 2 }).status(200);
|
||||
interfake.get('/fluent').status(300);
|
||||
interfake.get('/fluent?page=8').status(512);
|
||||
interfake.listen(3000);
|
||||
// it('should use a RegExp to find a partially-matched query string param and a fully-matched one, when there is also a query-free endpoint', function (done) {
|
||||
// // interfake = new Interfake({debug:true});
|
||||
// interfake.get('/fluent').query({ query: /[0-9]+/, page: 2 }).status(200);
|
||||
// interfake.get('/fluent').status(300);
|
||||
// interfake.get('/fluent?page=8').status(512);
|
||||
// interfake.listen(3000);
|
||||
|
||||
Q.all([get({url:'http://localhost:3000/fluent?query=1&page=5',json:true}), get({url:'http://localhost:3000/fluent?query=2&page=2',json:true}), get({url:'http://localhost:3000/fluent',json:true}), get({url:'http://localhost:3000/fluent?page=8',json:true})])
|
||||
.then(function (results) {
|
||||
assert.equal(results[0][0].statusCode, 404, 'The non-existent page should not be found');
|
||||
assert.equal(results[1][0].statusCode, 200, 'The existing page should be found');
|
||||
assert.equal(results[2][0].statusCode, 300, 'The non-query-string page should be found');
|
||||
assert.equal(results[3][0].statusCode, 512, 'The query-string page without additional params should be found');
|
||||
done();
|
||||
});
|
||||
});
|
||||
// Q.all([get({url:'http://localhost:3000/fluent?query=1&page=5',json:true}), get({url:'http://localhost:3000/fluent?query=2&page=2',json:true}), get({url:'http://localhost:3000/fluent',json:true}), get({url:'http://localhost:3000/fluent?page=8',json:true})])
|
||||
// .then(function (results) {
|
||||
// assert.equal(results[0][0].statusCode, 404, 'The non-existent page should not be found');
|
||||
// assert.equal(results[1][0].statusCode, 200, 'The existing page should be found');
|
||||
// assert.equal(results[2][0].statusCode, 300, 'The non-query-string page should be found');
|
||||
// assert.equal(results[3][0].statusCode, 512, 'The query-string page without additional params should be found');
|
||||
// done();
|
||||
// });
|
||||
// });
|
||||
|
||||
describe('#status()', function () {
|
||||
it('should create a GET endpoint which accepts different querystrings using both methods of querystring specification', function (done) {
|
||||
@@ -653,6 +654,7 @@ describe('Interfake JavaScript API', function () {
|
||||
});
|
||||
|
||||
it('should create a GET endpoint which accepts and does not accept a query string', function (done) {
|
||||
interfake = new Interfake({debug:true});
|
||||
interfake.get('/fluent');
|
||||
interfake.get('/fluent').query({ page: 2 }).status(500);
|
||||
interfake.listen(3000);
|
||||
@@ -713,172 +715,172 @@ describe('Interfake JavaScript API', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('#modifies', function () {
|
||||
it('should create a GET endpoint which modifies its own body when it gets called', function (done) {
|
||||
interfake.get('/fluent').body({ hello : 'there', goodbye: 'for now' }).modifies.get('/fluent').body({ what: 'ever' });
|
||||
interfake.listen(3000);
|
||||
// describe('#modifies', function () {
|
||||
// it('should create a GET endpoint which modifies its own body when it gets called', function (done) {
|
||||
// interfake.get('/fluent').body({ hello : 'there', goodbye: 'for now' }).modifies.get('/fluent').body({ what: 'ever' });
|
||||
// interfake.listen(3000);
|
||||
|
||||
get('http://localhost:3000/fluent')
|
||||
.then(function (results) {
|
||||
assert.equal(results[0].statusCode, 200);
|
||||
assert.equal(results[1].hello, 'there');
|
||||
assert.equal(results[1].goodbye, 'for now');
|
||||
assert.equal(results[2].what, undefined);
|
||||
return get('http://localhost:3000/fluent');
|
||||
})
|
||||
.then(function (results) {
|
||||
assert.equal(results[0].statusCode, 200);
|
||||
assert.equal(results[1].hello, 'there');
|
||||
assert.equal(results[1].goodbye, 'for now');
|
||||
assert.equal(results[2].what, 'ever');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
// get('http://localhost:3000/fluent')
|
||||
// .then(function (results) {
|
||||
// assert.equal(results[0].statusCode, 200);
|
||||
// assert.equal(results[1].hello, 'there');
|
||||
// assert.equal(results[1].goodbye, 'for now');
|
||||
// assert.equal(results[2].what, undefined);
|
||||
// return get('http://localhost:3000/fluent');
|
||||
// })
|
||||
// .then(function (results) {
|
||||
// assert.equal(results[0].statusCode, 200);
|
||||
// assert.equal(results[1].hello, 'there');
|
||||
// assert.equal(results[1].goodbye, 'for now');
|
||||
// assert.equal(results[2].what, 'ever');
|
||||
// done();
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
|
||||
describe('#status()', function () {
|
||||
it('should create one GET endpoint with a particular status code', function (done) {
|
||||
interfake.get('/fluent').status(300);
|
||||
interfake.listen(3000);
|
||||
// describe('#status()', function () {
|
||||
// it('should create one GET endpoint with a particular status code', function (done) {
|
||||
// interfake.get('/fluent').status(300);
|
||||
// interfake.listen(3000);
|
||||
|
||||
request({ url : 'http://localhost:3000/fluent', json : true }, function (error, response, body) {
|
||||
assert.equal(response.statusCode, 300);
|
||||
done();
|
||||
});
|
||||
});
|
||||
// request({ url : 'http://localhost:3000/fluent', json : true }, function (error, response, body) {
|
||||
// assert.equal(response.statusCode, 300);
|
||||
// done();
|
||||
// });
|
||||
// });
|
||||
|
||||
it('should create a GET endpoint which accepts different querystrings', function (done) {
|
||||
interfake.get('/fluent?query=1').status(400);
|
||||
interfake.get('/fluent?query=2').status(500);
|
||||
interfake.listen(3000);
|
||||
// it('should create a GET endpoint which accepts different querystrings', function (done) {
|
||||
// interfake.get('/fluent?query=1').status(400);
|
||||
// interfake.get('/fluent?query=2').status(500);
|
||||
// interfake.listen(3000);
|
||||
|
||||
|
||||
Q.all([get({url:'http://localhost:3000/fluent?query=1',json:true}), get({url:'http://localhost:3000/fluent?query=2',json:true})])
|
||||
.then(function (results) {
|
||||
assert.equal(results[0][0].statusCode, 400);
|
||||
assert.equal(results[1][0].statusCode, 500);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
// Q.all([get({url:'http://localhost:3000/fluent?query=1',json:true}), get({url:'http://localhost:3000/fluent?query=2',json:true})])
|
||||
// .then(function (results) {
|
||||
// assert.equal(results[0][0].statusCode, 400);
|
||||
// assert.equal(results[1][0].statusCode, 500);
|
||||
// done();
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
|
||||
describe('#body()', function () {
|
||||
it('should create one GET endpoint with a particular body', function (done) {
|
||||
interfake.get('/fluent').body({ fluency : 'isgreat' });
|
||||
interfake.listen(3000);
|
||||
// describe('#body()', function () {
|
||||
// it('should create one GET endpoint with a particular body', function (done) {
|
||||
// interfake.get('/fluent').body({ fluency : 'isgreat' });
|
||||
// interfake.listen(3000);
|
||||
|
||||
request({ url : 'http://localhost:3000/fluent', json : true }, function (error, response, body) {
|
||||
assert.equal(response.statusCode, 200);
|
||||
assert.equal(body.fluency, 'isgreat');
|
||||
done();
|
||||
});
|
||||
});
|
||||
// request({ url : 'http://localhost:3000/fluent', json : true }, function (error, response, body) {
|
||||
// assert.equal(response.statusCode, 200);
|
||||
// assert.equal(body.fluency, 'isgreat');
|
||||
// done();
|
||||
// });
|
||||
// });
|
||||
|
||||
it('should create two similar GET endpoints with different querystrings and different bodies', function (done) {
|
||||
interfake.get('/fluent?query=1').body({ schfifty : 'five' });
|
||||
interfake.get('/fluent?query=2').body({ gimme : 'shelter' });
|
||||
interfake.listen(3000);
|
||||
// it('should create two similar GET endpoints with different querystrings and different bodies', function (done) {
|
||||
// interfake.get('/fluent?query=1').body({ schfifty : 'five' });
|
||||
// interfake.get('/fluent?query=2').body({ gimme : 'shelter' });
|
||||
// interfake.listen(3000);
|
||||
|
||||
|
||||
Q.all([get({url:'http://localhost:3000/fluent?query=1',json:true}), get({url:'http://localhost:3000/fluent?query=2',json:true})])
|
||||
.then(function (results) {
|
||||
assert.equal(results[0][1].schfifty, 'five');
|
||||
assert.equal(results[1][1].gimme, 'shelter');
|
||||
done();
|
||||
});
|
||||
});
|
||||
// Q.all([get({url:'http://localhost:3000/fluent?query=1',json:true}), get({url:'http://localhost:3000/fluent?query=2',json:true})])
|
||||
// .then(function (results) {
|
||||
// assert.equal(results[0][1].schfifty, 'five');
|
||||
// assert.equal(results[1][1].gimme, 'shelter');
|
||||
// done();
|
||||
// });
|
||||
// });
|
||||
|
||||
describe('#status()', function () {
|
||||
it('should create one GET endpoint with a particular body and particular status', function (done) {
|
||||
interfake.get('/fluent').body({ fluency : 'isgreat' }).status(300);
|
||||
interfake.listen(3000);
|
||||
// describe('#status()', function () {
|
||||
// it('should create one GET endpoint with a particular body and particular status', function (done) {
|
||||
// interfake.get('/fluent').body({ fluency : 'isgreat' }).status(300);
|
||||
// interfake.listen(3000);
|
||||
|
||||
request({ url : 'http://localhost:3000/fluent', json : true }, function (error, response, body) {
|
||||
assert.equal(response.statusCode, 300);
|
||||
assert.equal(body.fluency, 'isgreat');
|
||||
done();
|
||||
});
|
||||
});
|
||||
// request({ url : 'http://localhost:3000/fluent', json : true }, function (error, response, body) {
|
||||
// assert.equal(response.statusCode, 300);
|
||||
// assert.equal(body.fluency, 'isgreat');
|
||||
// done();
|
||||
// });
|
||||
// });
|
||||
|
||||
it('should create two similar GET endpoints with different querystrings and different bodies and status codes', function (done) {
|
||||
interfake.get('/fluent?query=1&another=one').body({ schfifty : 'five' }).status(404);
|
||||
interfake.get('/fluent?query=2').body({ gimme : 'shelter' }).status(503);
|
||||
interfake.listen(3000);
|
||||
// it('should create two similar GET endpoints with different querystrings and different bodies and status codes', function (done) {
|
||||
// interfake.get('/fluent?query=1&another=one').body({ schfifty : 'five' }).status(404);
|
||||
// interfake.get('/fluent?query=2').body({ gimme : 'shelter' }).status(503);
|
||||
// interfake.listen(3000);
|
||||
|
||||
|
||||
Q.all([get({url:'http://localhost:3000/fluent?query=1&another=one',json:true}), get({url:'http://localhost:3000/fluent?query=2',json:true})])
|
||||
.then(function (results) {
|
||||
assert.equal(results[0][1].schfifty, 'five');
|
||||
assert.equal(results[0][0].statusCode, 404);
|
||||
assert.equal(results[1][1].gimme, 'shelter');
|
||||
assert.equal(results[1][0].statusCode, 503);
|
||||
done();
|
||||
});
|
||||
});
|
||||
// Q.all([get({url:'http://localhost:3000/fluent?query=1&another=one',json:true}), get({url:'http://localhost:3000/fluent?query=2',json:true})])
|
||||
// .then(function (results) {
|
||||
// assert.equal(results[0][1].schfifty, 'five');
|
||||
// assert.equal(results[0][0].statusCode, 404);
|
||||
// assert.equal(results[1][1].gimme, 'shelter');
|
||||
// assert.equal(results[1][0].statusCode, 503);
|
||||
// done();
|
||||
// });
|
||||
// });
|
||||
|
||||
describe('#delay()', function() {
|
||||
it('should create one GET endpoint with a particular body, status and delay', function (done) {
|
||||
var enoughTimeHasPassed = false;
|
||||
var _this = this;
|
||||
this.slow(500);
|
||||
interfake.get('/fluent').body({ fluency : 'isgreat' }).status(300).delay(50);
|
||||
interfake.listen(3000);
|
||||
setTimeout(function() {
|
||||
enoughTimeHasPassed = true;
|
||||
}, 50);
|
||||
// describe('#delay()', function() {
|
||||
// it('should create one GET endpoint with a particular body, status and delay', function (done) {
|
||||
// var enoughTimeHasPassed = false;
|
||||
// var _this = this;
|
||||
// this.slow(500);
|
||||
// interfake.get('/fluent').body({ fluency : 'isgreat' }).status(300).delay(50);
|
||||
// interfake.listen(3000);
|
||||
// setTimeout(function() {
|
||||
// enoughTimeHasPassed = true;
|
||||
// }, 50);
|
||||
|
||||
request({ url : 'http://localhost:3000/fluent', json : true }, function (error, response, body) {
|
||||
assert.equal(response.statusCode, 300);
|
||||
assert.equal(body.fluency, 'isgreat');
|
||||
if(!enoughTimeHasPassed) {
|
||||
throw new Error('Response wasn\'t delay for long enough');
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('#delay()', function() {
|
||||
it('should create one GET endpoint with a particular delay', function (done) {
|
||||
var enoughTimeHasPassed = false;
|
||||
this.slow(500);
|
||||
interfake.get('/fluent').delay(50);
|
||||
interfake.listen(3000);
|
||||
setTimeout(function() {
|
||||
enoughTimeHasPassed = true;
|
||||
}, 50);
|
||||
// request({ url : 'http://localhost:3000/fluent', json : true }, function (error, response, body) {
|
||||
// assert.equal(response.statusCode, 300);
|
||||
// assert.equal(body.fluency, 'isgreat');
|
||||
// if(!enoughTimeHasPassed) {
|
||||
// throw new Error('Response wasn\'t delay for long enough');
|
||||
// }
|
||||
// done();
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
// describe('#delay()', function() {
|
||||
// it('should create one GET endpoint with a particular delay', function (done) {
|
||||
// var enoughTimeHasPassed = false;
|
||||
// this.slow(500);
|
||||
// interfake.get('/fluent').delay(50);
|
||||
// interfake.listen(3000);
|
||||
// setTimeout(function() {
|
||||
// enoughTimeHasPassed = true;
|
||||
// }, 50);
|
||||
|
||||
request({ url : 'http://localhost:3000/fluent', json : true }, function () {
|
||||
if(!enoughTimeHasPassed) {
|
||||
throw new Error('Response wasn\'t delay for long enough');
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
// request({ url : 'http://localhost:3000/fluent', json : true }, function () {
|
||||
// if(!enoughTimeHasPassed) {
|
||||
// throw new Error('Response wasn\'t delay for long enough');
|
||||
// }
|
||||
// done();
|
||||
// });
|
||||
// });
|
||||
|
||||
describe('#body()', function() {
|
||||
it('should create one GET endpoint with a particular delay and body', function (done) {
|
||||
var enoughTimeHasPassed = false;
|
||||
this.slow(500);
|
||||
interfake.get('/fluent').delay(50).body({
|
||||
ok: 'yeah'
|
||||
});
|
||||
interfake.listen(3000);
|
||||
setTimeout(function() {
|
||||
enoughTimeHasPassed = true;
|
||||
}, 50);
|
||||
// describe('#body()', function() {
|
||||
// it('should create one GET endpoint with a particular delay and body', function (done) {
|
||||
// var enoughTimeHasPassed = false;
|
||||
// this.slow(500);
|
||||
// interfake.get('/fluent').delay(50).body({
|
||||
// ok: 'yeah'
|
||||
// });
|
||||
// interfake.listen(3000);
|
||||
// setTimeout(function() {
|
||||
// enoughTimeHasPassed = true;
|
||||
// }, 50);
|
||||
|
||||
request({ url : 'http://localhost:3000/fluent', json : true }, function (error, response, body) {
|
||||
if(!enoughTimeHasPassed) {
|
||||
throw new Error('Response wasn\'t delay for long enough');
|
||||
}
|
||||
assert.equal(body.ok, 'yeah');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
// request({ url : 'http://localhost:3000/fluent', json : true }, function (error, response, body) {
|
||||
// if(!enoughTimeHasPassed) {
|
||||
// throw new Error('Response wasn\'t delay for long enough');
|
||||
// }
|
||||
// assert.equal(body.ok, 'yeah');
|
||||
// done();
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
});
|
||||
|
||||
describe('#post()', function () {
|
||||
|
||||
Reference in New Issue
Block a user