xhr.test.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import QUnit from 'qunit';
  2. import xhrFactory from '../src/xhr';
  3. import { useFakeEnvironment } from './test-helpers.js';
  4. import videojs from 'video.js';
  5. QUnit.module('xhr', {
  6. beforeEach(assert) {
  7. this.env = useFakeEnvironment(assert);
  8. this.clock = this.env.clock;
  9. this.requests = this.env.requests;
  10. this.xhr = xhrFactory();
  11. },
  12. afterEach() {
  13. this.env.restore();
  14. }
  15. });
  16. QUnit.test('xhr respects beforeRequest', function(assert) {
  17. let defaultOptions = {
  18. url: 'default'
  19. };
  20. this.xhr(defaultOptions);
  21. assert.equal(this.requests.shift().url, 'default', 'url the same without override');
  22. this.xhr.beforeRequest = (options) => {
  23. options.url = 'player';
  24. return options;
  25. };
  26. this.xhr(defaultOptions);
  27. assert.equal(this.requests.shift().url, 'player', 'url changed with player override');
  28. videojs.Hls.xhr.beforeRequest = (options) => {
  29. options.url = 'global';
  30. return options;
  31. };
  32. this.xhr(defaultOptions);
  33. assert.equal(this.requests.shift().url, 'player', 'prioritizes player override');
  34. delete this.xhr.beforeRequest;
  35. this.xhr(defaultOptions);
  36. assert.equal(this.requests.shift().url, 'global', 'url changed with global override');
  37. });