ProxyManagerTests.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. 'settings-sharelatex': 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('does not calls next when match', function () {
  80. const target = '/'
  81. this.settings.proxyUrls[this.proxyPath] = target
  82. this.proxyManager.createProxy(target)(this.req, this.res, this.next)
  83. sinon.assert.notCalled(this.next)
  84. return sinon.assert.called(this.request)
  85. })
  86. it('proxy full URL', function () {
  87. const targetUrl = 'https://user:pass@foo.bar:123/pa/th.ext?query#hash'
  88. this.settings.proxyUrls[this.proxyPath] = targetUrl
  89. this.proxyManager.createProxy(targetUrl)(this.req)
  90. return assertCalledWith(this.request, { url: targetUrl })
  91. })
  92. it('overwrite query', function () {
  93. const targetUrl = 'foo.bar/baz?query'
  94. this.req.query = { requestQuery: 'important' }
  95. this.settings.proxyUrls[this.proxyPath] = targetUrl
  96. this.proxyManager.createProxy(targetUrl)(this.req)
  97. const newTargetUrl = 'foo.bar/baz?requestQuery=important'
  98. return assertCalledWith(this.request, { url: newTargetUrl })
  99. })
  100. it('handles target objects', function () {
  101. const target = { baseUrl: 'api.v1', path: '/pa/th' }
  102. this.settings.proxyUrls[this.proxyPath] = target
  103. this.proxyManager.createProxy(target)(this.req, this.res, this.next)
  104. return assertCalledWith(this.request, { url: 'api.v1/pa/th' })
  105. })
  106. it('handles missing baseUrl', function () {
  107. const target = { path: '/pa/th' }
  108. this.settings.proxyUrls[this.proxyPath] = target
  109. this.proxyManager.createProxy(target)(this.req, this.res, this.next)
  110. return assertCalledWith(this.request, { url: 'undefined/pa/th' })
  111. })
  112. it('handles dynamic path', function () {
  113. const target = {
  114. baseUrl: 'api.v1',
  115. path(params) {
  116. return `/resource/${params.id}`
  117. },
  118. }
  119. this.settings.proxyUrls['/res/:id'] = target
  120. this.req.url = '/res/123'
  121. this.req.route.path = '/res/:id'
  122. this.req.params = { id: 123 }
  123. this.proxyManager.createProxy(target)(this.req, this.res, this.next)
  124. return assertCalledWith(this.request, { url: 'api.v1/resource/123' })
  125. })
  126. it('set arbitrary options on request', function () {
  127. const target = {
  128. baseUrl: 'api.v1',
  129. path: '/foo',
  130. options: { foo: 'bar' },
  131. }
  132. this.req.url = '/foo'
  133. this.req.route.path = '/foo'
  134. this.proxyManager.createProxy(target)(this.req, this.res, this.next)
  135. return assertCalledWith(this.request, {
  136. foo: 'bar',
  137. url: 'api.v1/foo',
  138. })
  139. })
  140. it('passes cookies', function () {
  141. const target = { baseUrl: 'api.v1', path: '/foo' }
  142. this.req.url = '/foo'
  143. this.req.route.path = '/foo'
  144. this.req.headers = { cookie: 'cookie' }
  145. this.proxyManager.createProxy(target)(this.req, this.res, this.next)
  146. return assertCalledWith(this.request, {
  147. headers: {
  148. Cookie: 'cookie',
  149. },
  150. url: 'api.v1/foo',
  151. })
  152. })
  153. it('passes body for post', function () {
  154. const target = {
  155. baseUrl: 'api.v1',
  156. path: '/foo',
  157. options: { method: 'post' },
  158. }
  159. this.req.url = '/foo'
  160. this.req.route.path = '/foo'
  161. this.req.body = { foo: 'bar' }
  162. this.proxyManager.createProxy(target)(this.req, this.res, this.next)
  163. return assertCalledWith(this.request, {
  164. form: {
  165. foo: 'bar',
  166. },
  167. method: 'post',
  168. url: 'api.v1/foo',
  169. })
  170. })
  171. it('passes body for put', function () {
  172. const target = {
  173. baseUrl: 'api.v1',
  174. path: '/foo',
  175. options: { method: 'put' },
  176. }
  177. this.req.url = '/foo'
  178. this.req.route.path = '/foo'
  179. this.req.body = { foo: 'bar' }
  180. this.proxyManager.createProxy(target)(this.req, this.res, this.next)
  181. return assertCalledWith(this.request, {
  182. form: {
  183. foo: 'bar',
  184. },
  185. method: 'put',
  186. url: 'api.v1/foo',
  187. })
  188. })
  189. })
  190. })