Skip to content

Commit

Permalink
data-credentials support for amp-story bookend (#21679)
Browse files Browse the repository at this point in the history
* data-credentials support for amp-story bookend

* Made requested changes
  • Loading branch information
Pateriack authored and calebcordry committed Apr 4, 2019
1 parent 7793c82 commit b8e790b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
14 changes: 12 additions & 2 deletions extensions/amp-story/1.0/amp-story-request-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import {user, userAssert} from '../../../src/log';
/** @private @const {string} */
export const BOOKEND_CONFIG_ATTRIBUTE_NAME = 'src';

/** @private const {string} */
export const BOOKEND_CREDENTIALS_ATTRIBUTE_NAME = 'data-credentials';

/** @private @const {string} */
const TAG = 'amp-story-request-service';

Expand Down Expand Up @@ -63,7 +66,9 @@ export class AmpStoryRequestService {

if (bookendEl.hasAttribute(BOOKEND_CONFIG_ATTRIBUTE_NAME)) {
const rawUrl = bookendEl.getAttribute(BOOKEND_CONFIG_ATTRIBUTE_NAME);
return this.loadJsonFromAttribute_(rawUrl);
const credentials =
bookendEl.getAttribute(BOOKEND_CREDENTIALS_ATTRIBUTE_NAME);
return this.loadJsonFromAttribute_(rawUrl, credentials);
}

// Fallback. Check for an inline json config.
Expand All @@ -77,10 +82,11 @@ export class AmpStoryRequestService {

/**
* @param {string} rawUrl
* @param {string|null} credentials
* @return {(!Promise<!JsonObject>|!Promise<null>)}
* @private
*/
loadJsonFromAttribute_(rawUrl) {
loadJsonFromAttribute_(rawUrl, credentials) {
const opts = {};
opts.requireAmpResponseSourceOrigin = false;

Expand All @@ -89,6 +95,10 @@ export class AmpStoryRequestService {
return Promise.resolve(null);
}

if (credentials) {
opts.credentials = credentials;
}

return Services.urlReplacementsForDoc(this.storyElement_)
.expandUrlAsync(user().assertString(rawUrl))
.then(url => this.xhr_.fetchJson(url, opts))
Expand Down
27 changes: 27 additions & 0 deletions extensions/amp-story/1.0/test/test-amp-story-request-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import {
AmpStoryRequestService,
BOOKEND_CONFIG_ATTRIBUTE_NAME,
BOOKEND_CREDENTIALS_ATTRIBUTE_NAME,
} from '../amp-story-request-service';


Expand Down Expand Up @@ -105,4 +106,30 @@ describes.fakeWin('amp-story-store-service', {amp: true}, env => {
xhrMock.verify();
});
});

it('should fetch the bookend config with credentials', () => {
const bookendUrl = 'https://publisher.com/bookend';

bookendElement.setAttribute(BOOKEND_CONFIG_ATTRIBUTE_NAME, bookendUrl);
bookendElement.setAttribute(BOOKEND_CREDENTIALS_ATTRIBUTE_NAME, 'include');
xhrMock.expects('fetchJson')
.withExactArgs(
bookendUrl,
{
requireAmpResponseSourceOrigin: false,
credentials: 'include',
},
)
.resolves({
ok: true,
json() {
return Promise.resolve();
},
})
.once();

return requestService.loadBookendConfig().then(() => {
xhrMock.verify();
});
});
});

0 comments on commit b8e790b

Please sign in to comment.