UNPKG

api-bdd-test

Version:

use the bdd(cucumber) to test the RESTfull API

391 lines (382 loc) 12.7 kB
(function() { var cson, isObject, isString, path, toQuery; isObject = require('util-ex/lib/is/type/object'); isString = require('util-ex/lib/is/type/string'); path = require('path.js/lib/posix'); cson = require('../../cson-string'); toQuery = require('../../to-query'); module.exports = function(aDictionary) { var res4DataRegEx, resNameRegEx, resResultRegEx; resNameRegEx = '[::]?[((‘\'“" ]$identifier(?:[)),,.。’”"\' ]|$)'; resResultRegEx = '[的其]?(?:内容|结果)[为是]?[::]?$object'; res4DataRegEx = resNameRegEx + '[,,.。]?\\s*' + resResultRegEx; this.define(new RegExp('列[出举]资源\\s*' + resNameRegEx), function(resource) { var testScope; testScope = this.ctx; if (resource == null) { resource = this.resource; } return this.api.get(resource).then((function(_this) { return function(res) { testScope.result = res; }; })(this))["catch"]((function(_this) { return function(err) { testScope.result = err; return err; }; })(this)); }); this.define(new RegExp('[搜查][索询找]资源\\s*' + resNameRegEx + '[,,.。]?\\s*按?(?:指定|如下)?(?:条件|设置)[::]?$object'), function(resource, filter) { var result, testScope; testScope = this.ctx; if (resource == null) { resource = this.resource; } result = this.api.get(resource); if (filter) { result = result.query(toQuery(filter)); } return result.then((function(_this) { return function(res) { testScope.result = res; }; })(this))["catch"]((function(_this) { return function(err) { testScope.result = err; return err; }; })(this)); }); this.define(new RegExp('[新创]建资源\\s*' + res4DataRegEx), function(resource, data) { var testScope; testScope = this.ctx; if (resource == null) { resource = this.resource; } return this.api.post(resource, { data: data }).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('[新创]建资源\\s*' + resNameRegEx + '成功[,,.。]?\\s*' + resResultRegEx), function(resource, data) { var testScope; testScope = this.ctx; if (resource == null) { resource = this.resource; } return this.api.post(resource, { data: data }).then((function(_this) { return function(res) { testScope.result = res; if (res.status === 200 || res.status === 201) { return; } console.log(res); return expect(res.status).to.be.equal(200); }; })(this))["catch"]((function(_this) { return function(err) { testScope.result = err; console.log(err); expect(err).to.be["null"]; return err; }; })(this)); }); this.define(new RegExp('[编修][辑改](?:id|ID|编号)[为是::]?$string的?资源\\s*' + res4DataRegEx), function(id, resource, data) { var testScope; testScope = this.ctx; if (resource == null) { resource = this.resource; } if (resource) { id = path.join(resource, encodeURIComponent(id)); } return this.api.put(id, { data: data }).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('删[除掉](?:id|ID|编号)[为是::]?$string的?资源\\s*' + resNameRegEx), function(id, resource) { var testScope; testScope = this.ctx; if (resource == null) { resource = this.resource; } if (resource) { id = path.join(resource, encodeURIComponent(id)); } return this.api["delete"](id).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('检[查测]是否存在资源\\s*' + resNameRegEx + '\\s*[::]?$string'), function(resource, id) { var testScope; testScope = this.ctx; if (resource == null) { resource = this.resource; } if (resource) { id = path.join(resource, encodeURIComponent(id)); } return this.api.head(id).then((function(_this) { return function(res) { testScope.result = res; }; })(this))["catch"]((function(_this) { return function(err) { testScope.result = err; return err; }; })(this)); }); this.define(/上次[的]?(?:状态[码]?|status)[为是][::]?\s*$identifier/, function(data) { var testScope; testScope = this.ctx; if (data === 'ok') { data = '200'; } if (testScope.result.status + '' !== data) { console.log(testScope.result); } expect(testScope.result.status + '').to.be.equal(data); }); this.define(/上次[的]?(?:结果|body)([为是]|包[括含]?)[::]?\s*$object/, function(isInclude, data) { var testScope; testScope = this.ctx; if (isInclude[0] === '包') { return expect(testScope.result.body).to.be.containSubset(data); } else { return expect(testScope.result.body).to.be.deep.equal(data); } }); this.define(new RegExp('[获取拿得][取得到](?:id|ID|编号)[为是::]?$string的?资源\\s*' + resNameRegEx + '[,,.。]?\\s*[的其]?(?:内容|结果)(包[含括]|[为是等]同?于?)[::]?$object'), function(id, resource, aInclude, data) { if (resource == null) { resource = this.resource; } if (resource) { id = path.join(resource, encodeURIComponent(id)); } aInclude = aInclude[0] === '包'; return this.api.get(id).then((function(_this) { return function(res) { if (aInclude) { expect(res.body).to.be.containSubset(data); } else { expect(res.body).to.be.deep.equal(data); } }; })(this)); }); this.define(new RegExp('[获取拿得][取得到](?:id|ID|编号)[为是::]?$string[,,]?\\s*过?滤?条件[为是::]?$string的?资源\\s*' + resNameRegEx), function(id, filter, resource) { var result, testScope; testScope = this.ctx; if (resource == null) { resource = this.resource; } if (isString(filter) && filter.length) { filter = JSON.stringify(cson(filter)); } if (resource) { id = path.join(resource, encodeURIComponent(id)); } result = this.api.get(id); if (filter) { result.query({ filter: filter }); } return result.then((function(_this) { return function(res) { testScope.result = res; }; })(this))["catch"]((function(_this) { return function(err) { testScope.result = err; return err; }; })(this)); }); this.define(new RegExp('([不]?会?存在|没有?|有)(?:id|ID|编号)[为是::]?$string的?资源\\s*' + resNameRegEx), function(isExists, id, resource) { isExists = isExists[0] !== '不' && isExists[0] !== '没'; if (resource == null) { resource = this.resource; } if (resource) { id = path.join(resource, encodeURIComponent(id)); } return this.api.head(id).then((function(_this) { return function(res) { if (res.status === 204 || res.status === 200) { expect(isExists).to.be["true"]; } else if (res.status === 404) { expect(isExists).to.be["false"]; } else { throw new Error('Call isExists API Error:' + status); } }; })(this)); }); this.define(/登[录陆]\s*用户[::]\s*$string\s*[,,]\s*密码[::]\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(/注销用户|退出系统/, 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(/(?:记[住下忆]?|保[存留])$string到[::]?$string/, function(aKey, aToVar) { this.ctx[aToVar] = aKey; }); this.define(/(?:记[住下忆]?|保[存留])结果的$string(?:属性)?到[::]?$string/, function(aKey, aToVar) { var i, k, len, vResult; vResult = this.ctx.result; if (isString(aKey) && 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(/(?:记[住下忆]?|保[存留])结果到[::]?$string/, function(aToVar) { this.ctx[aToVar] = this.ctx.result; }); this.define(/(?:记[住下忆]?|保[存留])的\s*$string\s*(不)?((?:大于|小于)等于|至[少多]|等于|是|包[含括](?:key)?|[><!]=|[<=>])\s*(.+)$/, function(aKey, aNot, aOp, aValue) { var myExpect; aValue = cson(aValue); myExpect = expect(aKey).to.be; if (aNot != null) { myExpect = myExpect.not; } switch (aOp) { case '大于等于': case '>=': case '至少': myExpect.least(aValue); break; case '小于等于': case '<=': case '至多': myExpect.most(aValue); break; case '大于': case '>': myExpect.above(aValue); break; case '小于': case '<': myExpect.below(aValue); break; case '等于': case '=': case '是': myExpect.equal(aValue); break; default: if (aOp.slice(aOp.length - 3) === 'key') { myExpect.include.keys(aValue); } else { myExpect.containSubset(aValue); } } }); this.define(/(?:记[住下忆]?|保[存留])的\s*$string\s*(不)?((?:大于|小于)等于|至[少多]|等于|是|包[含括](?:key)?|[><!]=|[<=>])[::]$object/, function(aKey, aNot, aOp, aValue) { var myExpect; myExpect = expect(aKey).to.be; if (aNot != null) { myExpect = myExpect.not; } switch (aOp) { case '大于等于': case '>=': case '至少': myExpect.least(aValue); break; case '小于等于': case '<=': case '至多': myExpect.great(aValue); break; case '大于': case '>': myExpect.above(aValue); break; case '小于': case '<': myExpect.below(aValue); break; case '等于': case '=': case '是': myExpect.equal(aValue); break; default: if (aOp.slice(aOp.length - 3) === 'key') { myExpect.include.keys(aValue); } else { myExpect.containSubset(aValue); } } }); return this.define(/(不?存在)(?:记[住下忆]?|保[存留]的)?\s*$string$/, function(aExists, aKey) { if (aExists[0] !== '不') { 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