Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
samouri committed Apr 19, 2021
1 parent 03ac23a commit 8d84f48
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/service/real-time-config/real-time-config-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,10 @@ export class RealTimeConfigManager {
errorReportingUrl
);
}
if (!Services.urlForDoc(this.ampDoc_).isSecure(url)) {
if (
!Services.urlForDoc(this.ampDoc_).isSecure(url) &&
!isAmpScriptUri(url)
) {
return this.buildErrorResponse_(
RTC_ERROR_ENUM.INSECURE_URL,
callout,
Expand All @@ -446,6 +449,7 @@ export class RealTimeConfigManager {
if (url.length > MAX_URL_LENGTH) {
url = this.truncUrl_(url);
}

return this.sendRtcCallout_(
url,
timeoutMillis,
Expand Down Expand Up @@ -516,7 +520,7 @@ export class RealTimeConfigManager {
let rtcFetch;
if (isAmpScriptUri(url)) {
rtcFetch = Services.scriptForDocOrNull(element)
.then((service) => service.fetch(url, callout))
.then((service) => service.fetch(url))
.then((json) => {
checkStillCurrent();
const rtcTime = Date.now() - this.rtcStartTime_;
Expand Down
24 changes: 24 additions & 0 deletions test/unit/test-real-time-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,30 @@ describes.realWin('real-time-config service', {amp: true}, (env) => {
expectedRtcArray,
});
});

it('should fetch RTC from amp-script URIs', async () => {
const ampScriptFetch = env.sandbox.stub();
ampScriptFetch.returns(Promise.resolve({targeting: ['sports']}));
env.sandbox
.stub(Services, 'scriptForDocOrNull')
.returns(Promise.resolve({fetch: ampScriptFetch}));

const urls = ['amp-script:scriptId.functionName'];
setRtcConfig({urls, vendors: {}, timeoutMillis: 500});
const rtcResponse = await execute_(
element,
/* customMacros */ {},
/* consentState */ undefined,
/* consentString */ undefined,
/* consentMetadata */ undefined,
() => {}
);
expect(ampScriptFetch).calledWithExactly(
'amp-script:scriptId.functionName'
);
expect(rtcResponse[0].response).deep.equal({targeting: ['sports']});
});

it('should send RTC callouts to inflated vendor URLs', () => {
const vendors = {
'fAkeVeNdOR': {SLOT_ID: 1, PAGE_ID: 2},
Expand Down

0 comments on commit 8d84f48

Please sign in to comment.