purest.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const fs = require('fs')
  2. class MockPurest {
  3. constructor (opts) {
  4. const methodsToMock = ['query', 'select', 'where', 'qs', 'auth', 'json']
  5. const httpMethodsToMock = ['get', 'put', 'post', 'options', 'head']
  6. methodsToMock.forEach((item) => {
  7. this[item] = () => this
  8. })
  9. httpMethodsToMock.forEach((item) => {
  10. this[item] = (url) => {
  11. this._requestUrl = url
  12. return this
  13. }
  14. })
  15. this.opts = opts
  16. }
  17. request (done) {
  18. if (typeof done === 'function') {
  19. const responses = {
  20. zoom: {
  21. default: {}
  22. },
  23. dropbox: {
  24. default: {
  25. hash: '0a9f95a989dd4b1851f0103c31e304ce',
  26. user_email: 'foo@bar.com',
  27. email: 'foo@bar.com',
  28. entries: [{ rev: 'f24234cd4' }]
  29. }
  30. },
  31. drive: {
  32. 'files/README.md': {
  33. id: '0B2x-PmqQHSKdT013TE1VVjZ3TWs',
  34. mimeType: 'image/jpg',
  35. ownedByMe: true,
  36. permissions: [{ role: 'owner', emailAddress: 'ife@bala.com' }],
  37. size: 300,
  38. kind: 'drive#file',
  39. etag: '"bcIyJ9A3gXa8oTYmz6nzAjQd-lY/eQc3WbZHkXpcItNyGKDuKXM_bNY"'
  40. },
  41. default: {
  42. kind: 'drive#fileList',
  43. etag: '"bcIyJ9A3gXa8oTYmz6nzAjQd-lY/eQc3WbZHkXpcItNyGKDuKXM_bNY"',
  44. files: [{
  45. id: '0B2x-PmqQHSKdT013TE1VVjZ3TWs',
  46. mimeType: 'image/jpg',
  47. ownedByMe: true,
  48. permissions: [{ role: 'owner', emailAddress: 'ife@bala.com' }]
  49. }],
  50. size: 300
  51. }
  52. }
  53. }
  54. const providerResponses = responses[this.opts.providerName]
  55. const body = providerResponses[this._requestUrl] || providerResponses.default
  56. done(null, { body, statusCode: 200 }, body)
  57. }
  58. return this
  59. }
  60. on (evt, cb) {
  61. if (evt === 'response') {
  62. cb(fs.createReadStream('./README.md'))
  63. }
  64. return this
  65. }
  66. }
  67. module.exports = () => {
  68. return (options) => new MockPurest(options)
  69. }