ProxyManagerTests.coffee 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. sinon = require('sinon')
  2. assertCalledWith = sinon.assert.calledWith
  3. chai = require('chai')
  4. should = chai.should()
  5. expect = chai.expect
  6. modulePath = '../../../../app/js/infrastructure/ProxyManager'
  7. SandboxedModule = require('sandboxed-module')
  8. MockRequest = require "../helpers/MockRequest"
  9. MockResponse = require "../helpers/MockResponse"
  10. describe "ProxyManager", ->
  11. before ->
  12. @settings = proxyUrls: {}
  13. @request = sinon.stub().returns(
  14. on: ()->
  15. pipe: ()->
  16. )
  17. @proxyManager = SandboxedModule.require modulePath, requires:
  18. 'settings-sharelatex': @settings
  19. 'logger-sharelatex': log: ->
  20. 'request': @request
  21. @proxyPath = '/foo/bar'
  22. @req = new MockRequest()
  23. @res = new MockResponse()
  24. @next = sinon.stub()
  25. describe 'apply', ->
  26. it 'applies all paths', ->
  27. @router = get: sinon.stub()
  28. @settings.proxyUrls =
  29. '/foo/bar': ''
  30. '/foo/:id': ''
  31. @proxyManager.apply(@router)
  32. sinon.assert.calledTwice(@router.get)
  33. assertCalledWith(@router.get, '/foo/bar')
  34. assertCalledWith(@router.get, '/foo/:id')
  35. it 'applies methods other than get', ->
  36. @router =
  37. post: sinon.stub()
  38. put: sinon.stub()
  39. @settings.proxyUrls =
  40. '/foo/bar': {options: {method: 'post'}}
  41. '/foo/:id': {options: {method: 'put'}}
  42. @proxyManager.apply(@router)
  43. sinon.assert.calledOnce(@router.post)
  44. sinon.assert.calledOnce(@router.put)
  45. assertCalledWith(@router.post, '/foo/bar')
  46. assertCalledWith(@router.put, '/foo/:id')
  47. describe 'createProxy', ->
  48. beforeEach ->
  49. @req.url = @proxyPath
  50. @req.route.path = @proxyPath
  51. @req.query = {}
  52. @req.params = {}
  53. @req.headers = {}
  54. @settings.proxyUrls = {}
  55. afterEach ->
  56. @next.reset()
  57. @request.reset()
  58. it 'does not calls next when match', ->
  59. target = '/'
  60. @settings.proxyUrls[@proxyPath] = target
  61. @proxyManager.createProxy(target)(@req, @res, @next)
  62. sinon.assert.notCalled(@next)
  63. sinon.assert.called(@request)
  64. it 'proxy full URL', ->
  65. targetUrl = 'https://user:pass@foo.bar:123/pa/th.ext?query#hash'
  66. @settings.proxyUrls[@proxyPath] = targetUrl
  67. @proxyManager.createProxy(targetUrl)(@req)
  68. assertCalledWith(@request, {url: targetUrl})
  69. it 'overwrite query', ->
  70. targetUrl = 'foo.bar/baz?query'
  71. @req.query = { requestQuery: 'important' }
  72. @settings.proxyUrls[@proxyPath] = targetUrl
  73. @proxyManager.createProxy(targetUrl)(@req)
  74. newTargetUrl = 'foo.bar/baz?requestQuery=important'
  75. assertCalledWith(@request, {url: newTargetUrl})
  76. it 'handles target objects', ->
  77. target = { baseUrl: 'api.v1', path: '/pa/th'}
  78. @settings.proxyUrls[@proxyPath] = target
  79. @proxyManager.createProxy(target)(@req, @res, @next)
  80. assertCalledWith(@request, {url: 'api.v1/pa/th'})
  81. it 'handles missing baseUrl', ->
  82. target = { path: '/pa/th'}
  83. @settings.proxyUrls[@proxyPath] = target
  84. @proxyManager.createProxy(target)(@req, @res, @next)
  85. assertCalledWith(@request, {url: 'undefined/pa/th'})
  86. it 'handles dynamic path', ->
  87. target = baseUrl: 'api.v1', path: (params) -> "/resource/#{params.id}"
  88. @settings.proxyUrls['/res/:id'] = target
  89. @req.url = '/res/123'
  90. @req.route.path = '/res/:id'
  91. @req.params = id: 123
  92. @proxyManager.createProxy(target)(@req, @res, @next)
  93. assertCalledWith(@request, {url: 'api.v1/resource/123'})
  94. it 'set arbitrary options on request', ->
  95. target = baseUrl: 'api.v1', path: '/foo', options: foo: 'bar'
  96. @req.url = '/foo'
  97. @req.route.path = '/foo'
  98. @proxyManager.createProxy(target)(@req, @res, @next)
  99. assertCalledWith(@request,
  100. foo: 'bar'
  101. url: 'api.v1/foo'
  102. )
  103. it 'passes cookies', ->
  104. target = baseUrl: 'api.v1', path: '/foo'
  105. @req.url = '/foo'
  106. @req.route.path = '/foo'
  107. @req.headers = cookie: 'cookie'
  108. @proxyManager.createProxy(target)(@req, @res, @next)
  109. assertCalledWith(@request,
  110. headers: Cookie: 'cookie'
  111. url: 'api.v1/foo'
  112. )
  113. it 'passes body for post', ->
  114. target = baseUrl: 'api.v1', path: '/foo', options: method: 'post'
  115. @req.url = '/foo'
  116. @req.route.path = '/foo'
  117. @req.body = foo: 'bar'
  118. @proxyManager.createProxy(target)(@req, @res, @next)
  119. assertCalledWith(@request,
  120. form: foo: 'bar'
  121. method: 'post'
  122. url: 'api.v1/foo'
  123. )
  124. it 'passes body for put', ->
  125. target = baseUrl: 'api.v1', path: '/foo', options: method: 'put'
  126. @req.url = '/foo'
  127. @req.route.path = '/foo'
  128. @req.body = foo: 'bar'
  129. @proxyManager.createProxy(target)(@req, @res, @next)
  130. assertCalledWith(@request,
  131. form: foo: 'bar'
  132. method: 'put'
  133. url: 'api.v1/foo'
  134. )