index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var nock = require('nock');
  2. var utils = require('../../lib/utils');
  3. var extend = utils.extend;
  4. var replace = utils.replace;
  5. function Nockle(base, config) {
  6. if (!(this instanceof Nockle)) {
  7. return new Nockle(base, config);
  8. }
  9. this.base = base;
  10. this.config = extend({}, config);
  11. }
  12. Nockle.prototype.succeed = function (method, api, values, reply) {
  13. values = extend({}, this.config, values);
  14. return nock(replace(this.base, values))[method](replace(api, values))
  15. .reply(200, reply || {});
  16. };
  17. Nockle.prototype.fail = function (method, api, values, reply) {
  18. values = extend({}, this.config, values);
  19. return nock(replace(this.base, values))[method](replace(api, values))
  20. .reply(404, reply || { error: 'error' });
  21. };
  22. Nockle.prototype.get = function (api, values, reply) {
  23. return this.succeed('get', api, values, reply);
  24. };
  25. Nockle.prototype.post = function (api, values, reply) {
  26. return this.succeed('post', api, values, reply);
  27. };
  28. Nockle.prototype.put = function (api, values, reply) {
  29. return this.succeed('put', api, values, reply);
  30. };
  31. Nockle.prototype.delete = function (api, values, reply) {
  32. return this.succeed('delete', api, values, reply);
  33. };
  34. Nockle.prototype.failget = function (api, values, reply) {
  35. return this.fail('get', api, values, reply);
  36. };
  37. Nockle.prototype.failpost = function (api, values, reply) {
  38. return this.fail('post', api, values, reply);
  39. };
  40. Nockle.prototype.failput = function (api, values, reply) {
  41. return this.fail('put', api, values, reply);
  42. };
  43. Nockle.prototype.faildelete = function (api, values, reply) {
  44. return this.fail('delete', api, values, reply);
  45. };
  46. module.exports = function (chai) {
  47. chai.Nockle = Nockle;
  48. };