ProxyManagerTests.coffee 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. describe 'createProxy', ->
  36. beforeEach ->
  37. @req.url = @proxyPath
  38. @req.route.path = @proxyPath
  39. @req.query = {}
  40. @settings.proxyUrls = {}
  41. afterEach ->
  42. @next.reset()
  43. @request.reset()
  44. it 'does not calls next when match', ->
  45. target = '/'
  46. @settings.proxyUrls[@proxyPath] = target
  47. @proxyManager.createProxy(target)(@req, @res, @next)
  48. sinon.assert.notCalled(@next)
  49. sinon.assert.called(@request)
  50. it 'proxy full URL', ->
  51. targetUrl = 'https://user:pass@foo.bar:123/pa/th.ext?query#hash'
  52. @settings.proxyUrls[@proxyPath] = targetUrl
  53. @proxyManager.createProxy(targetUrl)(@req)
  54. assertCalledWith(@request, targetUrl)
  55. it 'overwrite query', ->
  56. targetUrl = 'foo.bar/baz?query'
  57. @req.query = { requestQuery: 'important' }
  58. @settings.proxyUrls[@proxyPath] = targetUrl
  59. @proxyManager.createProxy(targetUrl)(@req)
  60. newTargetUrl = 'foo.bar/baz?requestQuery=important'
  61. assertCalledWith(@request, newTargetUrl)
  62. it 'handles target objects', ->
  63. target = { baseUrl: 'api.v1', path: '/pa/th'}
  64. @settings.proxyUrls[@proxyPath] = target
  65. @proxyManager.createProxy(target)(@req, @res, @next)
  66. assertCalledWith(@request, 'api.v1/pa/th')
  67. it 'handles missing baseUrl', ->
  68. target = { path: '/pa/th'}
  69. @settings.proxyUrls[@proxyPath] = target
  70. @proxyManager.createProxy(target)(@req, @res, @next)
  71. assertCalledWith(@request, 'undefined/pa/th')
  72. it 'handles dynamic path', ->
  73. target = baseUrl: 'api.v1', path: (params) -> "/resource/#{params.id}"
  74. @settings.proxyUrls['/res/:id'] = target
  75. @req.url = '/res/123'
  76. @req.route.path = '/res/:id'
  77. @req.params = id: 123
  78. @proxyManager.createProxy(target)(@req, @res, @next)
  79. assertCalledWith(@request, 'api.v1/resource/123')