Skip to content
This repository has been archived by the owner on Jan 12, 2019. It is now read-only.

Commit

Permalink
Allow changing global xhr beforeRequest at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
gesinger committed Feb 16, 2017
1 parent 91128c5 commit 102cfd0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
4 changes: 0 additions & 4 deletions src/videojs-contrib-hls.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,10 +688,6 @@ const HlsSourceHandler = function(mode) {
// The player had a beforeRequest set prior to the source change.
// Use it for the new source.
tech.hls.xhr.beforeRequest = previousBeforeRequest;
} else if (videojs.Hls.xhr.beforeRequest) {
// Use a global `before` function if specified on videojs.Hls.xhr
// but still allow for a per-player override
tech.hls.xhr.beforeRequest = videojs.Hls.xhr.beforeRequest;
}

tech.hls.src(source.src);
Expand Down
13 changes: 9 additions & 4 deletions src/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
* @param {Function} callback the callback to call when done
* @return {Request} the xhr request that is going to be made
*/
import {xhr as videojsXHR, mergeOptions} from 'video.js';
import {
xhr as videojsXHR,
mergeOptions,
default as videojs
} from 'video.js';

const xhrFactory = function() {
const xhr = function XhrFunction(options, callback) {
Expand All @@ -20,9 +24,10 @@ const xhrFactory = function() {

// Allow an optional user-specified function to modify the option
// object before we construct the xhr request
if (XhrFunction.beforeRequest &&
typeof XhrFunction.beforeRequest === 'function') {
let newOptions = XhrFunction.beforeRequest(options);
let beforeRequest = XhrFunction.beforeRequest || videojs.Hls.xhr.beforeRequest;

if (beforeRequest && typeof beforeRequest === 'function') {
let newOptions = beforeRequest(options);

if (newOptions) {
options = newOptions;
Expand Down
46 changes: 46 additions & 0 deletions test/xhr.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import QUnit from 'qunit';
import xhrFactory from '../src/xhr';
import { useFakeEnvironment } from './test-helpers.js';
import videojs from 'video.js';

QUnit.module('xhr', {
beforeEach(assert) {
this.env = useFakeEnvironment(assert);
this.clock = this.env.clock;
this.requests = this.env.requests;
this.xhr = xhrFactory();
},
afterEach() {
this.env.restore();
}
});

QUnit.test('xhr respects beforeRequest', function(assert) {
let defaultOptions = {
url: 'default'
};

this.xhr(defaultOptions);
assert.equal(this.requests.shift().url, 'default', 'url the same without override');

this.xhr.beforeRequest = (options) => {
options.url = 'player';
return options;
};

this.xhr(defaultOptions);
assert.equal(this.requests.shift().url, 'player', 'url changed with player override');

videojs.Hls.xhr.beforeRequest = (options) => {
options.url = 'global';
return options;
};

this.xhr(defaultOptions);
assert.equal(this.requests.shift().url, 'player', 'prioritizes player override');

delete this.xhr.beforeRequest;

this.xhr(defaultOptions);
assert.equal(this.requests.shift().url, 'global', 'url changed with global override');
});

0 comments on commit 102cfd0

Please sign in to comment.