From 182c11481e36519d205cfc08b6ce33e84ef569ea Mon Sep 17 00:00:00 2001 From: Uwe Schmidt Date: Fri, 5 May 2023 00:03:22 +0200 Subject: [PATCH] Add tests for `setAjaxHeaders` --- src/viewer.js | 4 +- test/modules/ajax-tiles.js | 126 +++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 2 deletions(-) diff --git a/src/viewer.js b/src/viewer.js index 75113dea..096bc597 100644 --- a/src/viewer.js +++ b/src/viewer.js @@ -992,12 +992,12 @@ $.extend( $.Viewer.prototype, $.EventSource.prototype, $.ControlDock.prototype, if (propagate) { for (var i = 0; i < this.world.getItemCount(); i++) { - this.world.getItemAt(i).setAjaxHeaders(null, propagate); + this.world.getItemAt(i).setAjaxHeaders(null, true); } if (this.referenceStrip && this.referenceStrip.miniViewers) { for (var key in this.referenceStrip.miniViewers) { - this.referenceStrip.miniViewers[key].setAjaxHeaders(this.ajaxHeaders, propagate); + this.referenceStrip.miniViewers[key].setAjaxHeaders(this.ajaxHeaders, true); } } } diff --git a/test/modules/ajax-tiles.js b/test/modules/ajax-tiles.js index a77dfed8..c18ec282 100644 --- a/test/modules/ajax-tiles.js +++ b/test/modules/ajax-tiles.js @@ -245,4 +245,130 @@ tileSource: staticHeaderTileSource }); }); + + QUnit.test('Viewer headers can be updated', function(assert) { + var done = assert.async(); + + var newHeaders = { + 'X-Viewer-Header': 'ViewerHeaderValue-Updated', + 'X-Viewer-Header2': 'ViewerHeaderValue2' + } + var newHeaders2 = { + Range: 'test', + } + + var tileLoaded = function tileLoaded(evt) { + viewer.removeHandler('tile-loaded', tileLoaded); + // set new Viewer headers and propagate to TiledImage and Tile + viewer.setAjaxHeaders(newHeaders); + viewer.addHandler('tile-loaded', tileLoaded2); + }; + + var tileLoaded2 = function tileLoaded(evt) { + viewer.removeHandler('tile-loaded', tileLoaded2); + assert.deepEqual(viewer.ajaxHeaders, newHeaders); + assert.deepEqual(evt.tiledImage.ajaxHeaders, newHeaders); + assert.deepEqual( + evt.tile.ajaxHeaders, + OpenSeadragon.extend( + {}, viewer.ajaxHeaders, evt.tiledImage.ajaxHeaders, + { Range: getTileRangeHeader(evt.tile.level, evt.tile.x, evt.tile.y) } + ) + ); + // set new Viewer headers and propagate to TiledImage and Tile + viewer.setAjaxHeaders(newHeaders2, true); + viewer.addHandler('tile-loaded', tileLoaded3); + }; + + var tileLoaded3 = function tileLoaded(evt) { + viewer.removeHandler('tile-loaded', tileLoaded3); + assert.deepEqual(viewer.ajaxHeaders, newHeaders2); + assert.deepEqual(evt.tiledImage.ajaxHeaders, newHeaders2); + assert.equal(evt.tile.ajaxHeaders['X-Viewer-Header'], undefined); + assert.equal(evt.tile.ajaxHeaders['X-Viewer-Header2'], undefined); + // 'Range' header entry set per tile and must not be overwritten by Viewer header + assert.equal(evt.tile.ajaxHeaders.Range, getTileRangeHeader(evt.tile.level, evt.tile.x, evt.tile.y)); + // set new Viewer headers but do not propagate to TiledImage and Tile + viewer.setAjaxHeaders(newHeaders, false); + viewer.addHandler('tile-loaded', tileLoaded4); + }; + + var tileLoaded4 = function tileLoaded(evt) { + viewer.removeHandler('tile-loaded', tileLoaded4); + assert.deepEqual(viewer.ajaxHeaders, newHeaders); + assert.deepEqual(evt.tiledImage.ajaxHeaders, newHeaders2); + assert.equal(evt.tile.ajaxHeaders['X-Viewer-Header'], undefined); + assert.equal(evt.tile.ajaxHeaders['X-Viewer-Header2'], undefined); + done(); + }; + + viewer.addHandler('tile-loaded', tileLoaded); + viewer.open(customTileSource); + }); + + QUnit.test('TileImage headers can be updated', function(assert) { + var done = assert.async(); + + var tileSourceHeaders = { + 'X-Tile-Header': 'TileHeaderValue' + } + var newHeaders = { + 'X-TiledImage-Header': 'TiledImageHeaderValue-Updated', + 'X-TiledImage-Header2': 'TiledImageHeaderValue2' + } + var newHeaders2 = { + 'X-Viewer-Header': 'ViewerHeaderValue-Updated', + 'X-Tile-Header': 'TileHeaderValue-Updated' + } + + var tileLoaded = function tileLoaded(evt) { + viewer.removeHandler('tile-loaded', tileLoaded); + // set new TiledImage headers and propagate to Tile + evt.tiledImage.setAjaxHeaders(newHeaders); + viewer.addHandler('tile-loaded', tileLoaded2); + }; + + var tileLoaded2 = function tileLoaded(evt) { + viewer.removeHandler('tile-loaded', tileLoaded2); + assert.deepEqual(viewer.ajaxHeaders, { 'X-Viewer-Header': 'ViewerHeaderValue' }); + assert.deepEqual(evt.tiledImage._ownAjaxHeaders, newHeaders); + assert.deepEqual(evt.tiledImage.ajaxHeaders, OpenSeadragon.extend({}, viewer.ajaxHeaders, newHeaders)); + assert.deepEqual(evt.tile.ajaxHeaders, OpenSeadragon.extend({}, viewer.ajaxHeaders, newHeaders, tileSourceHeaders)); + // set new TiledImage headers (that overwrite header entries of Viewer and Tile) and propagate to Tile + evt.tiledImage.setAjaxHeaders(newHeaders2, true); + viewer.addHandler('tile-loaded', tileLoaded3); + }; + + var tileLoaded3 = function tileLoaded(evt) { + viewer.removeHandler('tile-loaded', tileLoaded3); + assert.deepEqual(viewer.ajaxHeaders, { 'X-Viewer-Header': 'ViewerHeaderValue' }); + assert.deepEqual(evt.tiledImage._ownAjaxHeaders, newHeaders2); + assert.deepEqual(evt.tiledImage.ajaxHeaders, OpenSeadragon.extend({}, viewer.ajaxHeaders, newHeaders2)); + assert.deepEqual(evt.tile.ajaxHeaders, OpenSeadragon.extend({}, viewer.ajaxHeaders, newHeaders2, tileSourceHeaders)); + // set new TiledImage headers but do not propagate to Tile + evt.tiledImage.setAjaxHeaders({}, false); + viewer.addHandler('tile-loaded', tileLoaded4); + }; + + var tileLoaded4 = function tileLoaded(evt) { + viewer.removeHandler('tile-loaded', tileLoaded4); + assert.deepEqual(viewer.ajaxHeaders, { 'X-Viewer-Header': 'ViewerHeaderValue' }); + assert.deepEqual(evt.tiledImage._ownAjaxHeaders, {}); + assert.deepEqual(evt.tiledImage.ajaxHeaders, viewer.ajaxHeaders); + assert.deepEqual(evt.tile.ajaxHeaders, OpenSeadragon.extend({}, viewer.ajaxHeaders, newHeaders2, tileSourceHeaders)); + done(); + }; + + viewer.addHandler('tile-loaded', tileLoaded); + viewer.addTiledImage({ + ajaxHeaders: { + 'X-TiledImage-Header': 'TiledImageHeaderValue' + }, + tileSource: OpenSeadragon.extend({}, customTileSource, { + getTileAjaxHeaders: function() { + return tileSourceHeaders; + } + }), + }); + }); })();