api-bdd-test
Version:
use the bdd(cucumber) to test the RESTfull API
256 lines (247 loc) • 8 kB
JavaScript
(function() {
var cs, isArray, isObject, path, toQuery;
isObject = require('util-ex/lib/is/type/object');
isArray = require('util-ex/lib/is/type/array');
path = require('path');
cs = require('coffeescript');
toQuery = require('../to-query');
module.exports = function(aDictionary) {
this.define(new RegExp('(GET|HEAD|DEL(?:ETE)?)\\s+$string'), function(method, resource) {
var request, testScope;
testScope = this.ctx;
method = method.toLowerCase();
return request = this.api.request(method, resource).then((function(_this) {
return function(res) {
return testScope.result = res;
};
})(this))["catch"]((function(_this) {
return function(err) {
testScope.result = err;
return err;
};
})(this));
});
this.define(new RegExp('(GET|HEAD|DEL(?:ETE)?|POST|PATCH|PUT)\\s+$string[:]$object'), function(method, resource, data) {
var attach, i, len, ref, request, testScope;
if (data == null) {
data = {};
}
testScope = this.ctx;
if (resource == null) {
resource = this.resource;
}
method = method.toLowerCase();
request = this.api.request(method, resource);
data.head = data.heads || data.head;
data.accept = data.accepts || data.accept;
data.query = data.queries || data.query;
data.field = data.fields || data.field;
data.attach = data.attachs || data.attachments || data.attachment || data.attach;
if (data.query) {
toQuery(data.query);
}
if (data.data) {
request = request.send(data.data);
}
if (data.head) {
request = request.set(data.head);
}
if (data.type) {
request = request.type(data.type);
}
if (data.accept) {
request = request.accept(data.accept);
}
if (data.query) {
request = request.query(data.query);
}
if (data.field) {
request = request.field(data.field);
}
if (isArray(data.attach)) {
ref = data.attach;
for (i = 0, len = ref.length; i < len; i++) {
attach = ref[i];
if (isArray(attach) && attach.length >= 2) {
attach[1] = path.resolve(this.dir, attach[1]);
request = request.attach.apply(request, attach);
}
}
}
return request.then((function(_this) {
return function(res) {
return testScope.result = res;
};
})(this))["catch"]((function(_this) {
return function(err) {
testScope.result = err;
return err;
};
})(this));
});
this.define(new RegExp('(?:last|prev(?:ious)?)\\s+results?\\s+(?:should\\s+)?(be|is|are|includes?)\\s*[:]?\\s*$object'), function(isInclude, data) {
var testScope;
if (data == null) {
data = {};
}
testScope = this.ctx;
isInclude = isInclude[2] === 'n';
if (isInclude) {
return expect(testScope.result.body).to.be.containSubset(data);
} else {
return expect(testScope.result.body).to.be.deep.equal(data);
}
});
this.define(/(?:last|prev(?:ious)?)\s+status\s*(?:code)?\s*(?:should\s+)?((?:be|is)(?:n't|\s+not)?)\s*[:]?\s*$integer/, function(isNot, status) {
var testScope;
testScope = this.ctx;
isNot = isNot.length > 2;
if (isNot) {
expect(testScope.result.status).to.be.not.equal(status);
} else {
expect(testScope.result.status).to.be.equal(status);
}
});
this.define(/login\s+user:\s*$string\s*,\s*passw(?:or)?d:\s*$string/, function(username, password) {
var testScope;
testScope = this.ctx;
return this.api.login({
username: username,
password: password
}).then((function(_this) {
return function(res) {
testScope.result = res;
};
})(this))["catch"]((function(_this) {
return function(err) {
testScope.result = err;
return err;
};
})(this));
});
this.define(/logout\s+user|(?:exit|quit)\s+system/, function() {
var testScope;
testScope = this.ctx;
return this.api.logout().then((function(_this) {
return function(res) {
testScope.result = res;
};
})(this))["catch"]((function(_this) {
return function(err) {
testScope.result = err;
return err;
};
})(this));
});
this.define(/(?:keep|store|save)\s+(?:the\s+)?$string\s+(?:in)?to\s+$string/, function(aKey, aToVar) {
this.ctx[aToVar] = aKey;
});
this.define(/(?:keep|store|save)\s+(?:the\s+)?result\s+of\s+$string\s+(?:in)?to\s+$string/, function(aKey, aToVar) {
var i, k, len, vResult;
vResult = this.ctx.result;
if ((aKey != null) && aKey.length && (vResult != null)) {
aKey = aKey.split('.');
for (i = 0, len = aKey.length; i < len; i++) {
k = aKey[i];
if (vResult != null) {
vResult = vResult[k];
}
}
}
this.ctx[aToVar] = vResult;
});
this.define(/(?:keep|store|save)\s+(?:the\s+)?result\s+(?:in)?to\s+$string/, function(aToVar) {
this.ctx[aToVar] = this.ctx.result;
});
this.define(/expect\s+(?:the\s+)(?:stored|kept|saved)\s+$string(?:\s+is|be|are|to)?\s+(not\s+)?(above|below|most|least|equa?l?|(?:include|contain)(?:\s+key)?|[><!]=|[<=>])\s*(.+)$/, function(aKey, aNot, aOp, aValue) {
var myExpect;
aValue = cs["eval"](aValue);
myExpect = expect(aKey).to.be;
if (aNot != null) {
myExpect = myExpect.not;
}
switch (aOp) {
case 'least':
case '>=':
myExpect.least(aValue);
break;
case 'most':
case '<=':
myExpect.most(aValue);
break;
case 'above':
case '>':
myExpect.above(aValue);
break;
case 'below':
case '<':
myExpect.below(aValue);
break;
case 'equal':
case 'equ':
case '=':
myExpect.equal(aValue);
break;
case '!=':
myExpect.not.equal(aValue);
break;
default:
if (aOp.slice(aOp.length - 3) === 'key') {
myExpect.include.keys(aValue);
} else {
myExpect.containSubset(aValue);
}
}
});
this.define(/expect\s+(?:the\s+)(?:stored|kept|saved)\s+$string(?:\s+is|be|are|to)?\s+(not\s+)?(above|below|most|least|equa?l?|(?:include|contain)(?:\s+key)?|[><!]=|[<=>])[:]$object/, function(aKey, aNot, aOp, aValue) {
var myExpect;
myExpect = expect(aKey).to.be;
if (aNot != null) {
myExpect = myExpect.not;
}
switch (aOp) {
case 'least':
case '>=':
myExpect.least(aValue);
break;
case 'most':
case '<=':
myExpect.most(aValue);
break;
case 'above':
case '>':
myExpect.above(aValue);
break;
case 'below':
case '<':
myExpect.below(aValue);
break;
case 'equal':
case 'equ':
case '=':
myExpect.equal(aValue);
break;
case '!=':
myExpect.not.equal(aValue);
break;
default:
if (aOp[0] === 'n') {
myExpect = myExpect.not;
}
if (aOp.slice(aOp.length - 3) === 'key') {
myExpect.include.keys(aValue);
} else {
myExpect.containSubset(aValue);
}
}
});
return this.define(/expect\s+((?:not\s+)?exist)(?:the\s+)?(?:stored|kept|saved)\s+$string/, function(aExists, aKey) {
if (aExists[0] !== 'n') {
return expect(this.ctx[aKey]).to.be.exist;
} else {
return expect(this.ctx[aKey]).not.to.be.exist;
}
});
};
}).call(this);
//# sourceMappingURL=api-lib.js.map