url.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* global jest:false, test:false, expect:false, describe:false */
  2. const nock = require('nock')
  3. const request = require('supertest')
  4. jest.mock('tus-js-client')
  5. jest.mock('../../src/server/helpers/request', () => {
  6. return {
  7. ...jest.requireActual('../../src/server/helpers/request'),
  8. getURLMeta: () => {
  9. return Promise.resolve({ size: 7580, type: 'image/jpg' })
  10. },
  11. }
  12. })
  13. const { getServer } = require('../mockserver')
  14. const mockServer = getServer({ COMPANION_CLIENT_SOCKET_CONNECT_TIMEOUT: '0' })
  15. beforeAll(() => {
  16. nock('http://url.myendpoint.com').get('/files').reply(200, () => '')
  17. })
  18. afterAll(() => {
  19. nock.cleanAll()
  20. nock.restore()
  21. })
  22. const invalids = [
  23. // no url at all or unsupported protocol
  24. null, '', 'ftp://url.myendpoint.com/files',
  25. ]
  26. describe('url meta', () => {
  27. test('return a url\'s meta data', () => {
  28. return request(mockServer)
  29. .post('/url/meta')
  30. .set('Content-Type', 'application/json')
  31. .send({
  32. url: 'http://url.myendpoint.com/files',
  33. })
  34. .expect(200)
  35. .then((res) => {
  36. expect(res.body.size).toBe(7580)
  37. expect(res.body.type).toBe('image/jpg')
  38. })
  39. })
  40. test.each(invalids)('return 400 for invalid url', (urlCase) => {
  41. return request(mockServer)
  42. .post('/url/meta')
  43. .set('Content-Type', 'application/json')
  44. .send({
  45. url: urlCase,
  46. })
  47. .expect(400)
  48. .then((res) => expect(res.body.error).toBe('Invalid request body'))
  49. })
  50. })
  51. describe('url get', () => {
  52. test('url download gets instanitated', () => {
  53. return request(mockServer)
  54. .post('/url/get')
  55. .set('Content-Type', 'application/json')
  56. .send({
  57. url: 'http://url.myendpoint.com/files',
  58. endpoint: 'http://tusd.tusdemo.net/files',
  59. protocol: 'tus',
  60. })
  61. .expect(200)
  62. .then((res) => expect(res.body.token).toBeTruthy())
  63. })
  64. test.each(invalids)('downloads are not instantiated for invalid urls', (urlCase) => {
  65. return request(mockServer)
  66. .post('/url/get')
  67. .set('Content-Type', 'application/json')
  68. .send({
  69. url: urlCase,
  70. endpoint: 'http://tusd.tusdemo.net/files',
  71. protocol: 'tus',
  72. })
  73. .expect(400)
  74. .then((res) => expect(res.body.error).toBe('Invalid request body'))
  75. })
  76. })