remote.test.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import request from 'supertest';
  2. import express from 'express';
  3. import remote from '../remote';
  4. import origin from '../origin';
  5. describe('Remote Test', () => {
  6. let app;
  7. let query;
  8. beforeEach(() => {
  9. app = express();
  10. app.use(express.json());
  11. app.use(
  12. (req, _, next) => {
  13. req.siteId = 'mysiteid';
  14. next();
  15. },
  16. origin(),
  17. remote({
  18. setupPath: 'scripts/mysetup.js',
  19. context: { content: {} },
  20. })
  21. );
  22. query = request(app);
  23. });
  24. it('should allow calls with Origin, X-Ricochet-Origin, Referer header', async () => {
  25. await query.get(`/ping`).expect(400);
  26. await query
  27. .get(`/ping`)
  28. .set('X-Ricochet-Origin', 'http://localhost:5000')
  29. .expect(200);
  30. await query.get(`/ping`).set('Origin', 'http://localhost:5000').expect(200);
  31. await query
  32. .get(`/ping`)
  33. .set('Referer', 'http://localhost:5000/test/toto')
  34. .expect(200);
  35. });
  36. it('should fails to parse setup', async () => {
  37. app = express();
  38. app.use(express.json());
  39. app.use(
  40. (req, _, next) => {
  41. req.ricochetOrigin = 'http://localhost:5000';
  42. next();
  43. },
  44. remote({
  45. setupPath: 'scripts/bad.js',
  46. })
  47. );
  48. query = request(app);
  49. const result = await query
  50. .get(`/`)
  51. .set('X-Ricochet-Origin', 'http://localhost:5000')
  52. .expect(500);
  53. expect(result.body.message).toEqual(
  54. expect.stringContaining('Unexpected identifier')
  55. );
  56. });
  57. });