url.js 2.2 KB

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