ProxyManagerTests.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon')
  14. const assertCalledWith = sinon.assert.calledWith
  15. const { expect } = require('chai')
  16. const modulePath = '../../../../app/src/infrastructure/ProxyManager'
  17. const SandboxedModule = require('sandboxed-module')
  18. const MockRequest = require('../helpers/MockRequest')
  19. const MockResponse = require('../helpers/MockResponse')
  20. describe('ProxyManager', function () {
  21. beforeEach(function () {
  22. this.settings = { proxyUrls: {} }
  23. this.request = sinon.stub().returns({
  24. on() {},
  25. pipe() {},
  26. })
  27. this.proxyManager = SandboxedModule.require(modulePath, {
  28. requires: {
  29. '@overleaf/settings': this.settings,
  30. request: this.request,
  31. },
  32. })
  33. this.proxyPath = '/foo/bar'
  34. this.req = new MockRequest()
  35. this.res = new MockResponse()
  36. return (this.next = sinon.stub())
  37. })
  38. describe('apply', function () {
  39. it('applies all paths', function () {
  40. this.router = { get: sinon.stub() }
  41. this.settings.proxyUrls = {
  42. '/foo/bar': '',
  43. '/foo/:id': '',
  44. }
  45. this.proxyManager.apply(this.router)
  46. sinon.assert.calledTwice(this.router.get)
  47. assertCalledWith(this.router.get, '/foo/bar')
  48. return assertCalledWith(this.router.get, '/foo/:id')
  49. })
  50. it('applies methods other than get', function () {
  51. this.router = {
  52. post: sinon.stub(),
  53. put: sinon.stub(),
  54. }
  55. this.settings.proxyUrls = {
  56. '/foo/bar': { options: { method: 'post' } },
  57. '/foo/:id': { options: { method: 'put' } },
  58. }
  59. this.proxyManager.apply(this.router)
  60. sinon.assert.calledOnce(this.router.post)
  61. sinon.assert.calledOnce(this.router.put)
  62. assertCalledWith(this.router.post, '/foo/bar')
  63. return assertCalledWith(this.router.put, '/foo/:id')
  64. })
  65. })
  66. describe('createProxy', function () {
  67. beforeEach(function () {
  68. this.req.url = this.proxyPath
  69. this.req.route.path = this.proxyPath
  70. this.req.query = {}
  71. this.req.params = {}
  72. this.req.headers = {}
  73. return (this.settings.proxyUrls = {})
  74. })
  75. afterEach(function () {
  76. this.next.reset()
  77. return this.request.reset()
  78. })
  79. it('proxy full URL', function () {
  80. const targetUrl = 'https://user:pass@foo.bar:123/pa/th.ext?query#hash'
  81. this.settings.proxyUrls[this.proxyPath] = targetUrl
  82. this.proxyManager.createProxy(targetUrl)(this.req)
  83. return assertCalledWith(this.request, { url: targetUrl })
  84. })
  85. it('overwrite query', function () {
  86. const targetUrl = 'https://foo.bar/baz?query'
  87. this.req.query = { requestQuery: 'important' }
  88. this.settings.proxyUrls[this.proxyPath] = targetUrl
  89. this.proxyManager.createProxy(targetUrl)(this.req)
  90. const newTargetUrl = 'https://foo.bar/baz?requestQuery=important'
  91. return assertCalledWith(this.request, { url: newTargetUrl })
  92. })
  93. it('handles target objects', function () {
  94. const target = { baseUrl: 'https://api.v1', path: '/pa/th' }
  95. this.settings.proxyUrls[this.proxyPath] = target
  96. this.proxyManager.createProxy(target)(this.req, this.res, this.next)
  97. return assertCalledWith(this.request, { url: 'https://api.v1/pa/th' })
  98. })
  99. it('handles missing baseUrl', function () {
  100. const target = { path: '/pa/th' }
  101. this.settings.proxyUrls[this.proxyPath] = target
  102. expect(() =>
  103. this.proxyManager.createProxy(target)(this.req, this.res, this.next)
  104. ).to.throw
  105. })
  106. it('handles dynamic path', function () {
  107. const target = {
  108. baseUrl: 'https://api.v1',
  109. path(params) {
  110. return `/resource/${params.id}`
  111. },
  112. }
  113. this.settings.proxyUrls['/res/:id'] = target
  114. this.req.url = '/res/123'
  115. this.req.route.path = '/res/:id'
  116. this.req.params = { id: 123 }
  117. this.proxyManager.createProxy(target)(this.req, this.res, this.next)
  118. return assertCalledWith(this.request, {
  119. url: 'https://api.v1/resource/123',
  120. })
  121. })
  122. it('set arbitrary options on request', function () {
  123. const target = {
  124. baseUrl: 'https://api.v1',
  125. path: '/foo',
  126. options: { foo: 'bar' },
  127. }
  128. this.req.url = '/foo'
  129. this.req.route.path = '/foo'
  130. this.proxyManager.createProxy(target)(this.req, this.res, this.next)
  131. return assertCalledWith(this.request, {
  132. foo: 'bar',
  133. url: 'https://api.v1/foo',
  134. })
  135. })
  136. it('passes cookies', function () {
  137. const target = { baseUrl: 'https://api.v1', path: '/foo' }
  138. this.req.url = '/foo'
  139. this.req.route.path = '/foo'
  140. this.req.headers = { cookie: 'cookie' }
  141. this.proxyManager.createProxy(target)(this.req, this.res, this.next)
  142. return assertCalledWith(this.request, {
  143. headers: {
  144. Cookie: 'cookie',
  145. },
  146. url: 'https://api.v1/foo',
  147. })
  148. })
  149. it('passes body for post', function () {
  150. const target = {
  151. baseUrl: 'https://api.v1',
  152. path: '/foo',
  153. options: { method: 'post' },
  154. }
  155. this.req.url = '/foo'
  156. this.req.route.path = '/foo'
  157. this.req.body = { foo: 'bar' }
  158. this.proxyManager.createProxy(target)(this.req, this.res, this.next)
  159. return assertCalledWith(this.request, {
  160. form: {
  161. foo: 'bar',
  162. },
  163. method: 'post',
  164. url: 'https://api.v1/foo',
  165. })
  166. })
  167. it('passes body for put', function () {
  168. const target = {
  169. baseUrl: 'https://api.v1',
  170. path: '/foo',
  171. options: { method: 'put' },
  172. }
  173. this.req.url = '/foo'
  174. this.req.route.path = '/foo'
  175. this.req.body = { foo: 'bar' }
  176. this.proxyManager.createProxy(target)(this.req, this.res, this.next)
  177. return assertCalledWith(this.request, {
  178. form: {
  179. foo: 'bar',
  180. },
  181. method: 'put',
  182. url: 'https://api.v1/foo',
  183. })
  184. })
  185. })
  186. })