@@ -11,6 +11,7 @@ ChromeUtils.defineESModuleGetters(this, {
1111 FilterAdult : "resource:///modules/FilterAdult.sys.mjs" ,
1212 NewTabUtils : "resource://gre/modules/NewTabUtils.sys.mjs" ,
1313 NimbusFeatures : "resource://nimbus/ExperimentAPI.sys.mjs" ,
14+ ObliviousHTTP : "resource://gre/modules/ObliviousHTTP.sys.mjs" ,
1415 PageThumbs : "resource://gre/modules/PageThumbs.sys.mjs" ,
1516 PlacesUtils : "resource://gre/modules/PlacesUtils.sys.mjs" ,
1617 sinon : "resource://testing-common/Sinon.sys.mjs" ,
@@ -3395,5 +3396,99 @@ add_task(async function test_ContileIntegration() {
33953396 sandbox . restore ( ) ;
33963397 }
33973398
3399+ {
3400+ info (
3401+ "TopSitesFeed._fetchSites should cast headers from a Headers object to JS object when using OHTTP"
3402+ ) ;
3403+ let { feed } = prepFeed ( getTopSitesFeedForTest ( sandbox ) ) ;
3404+
3405+ Services . prefs . setStringPref (
3406+ "browser.newtabpage.activity-stream.discoverystream.ohttp.relayURL" ,
3407+ "https://relay.url"
3408+ ) ;
3409+ Services . prefs . setStringPref (
3410+ "browser.newtabpage.activity-stream.discoverystream.ohttp.configURL" ,
3411+ "https://config.url"
3412+ ) ;
3413+ Services . prefs . setBoolPref (
3414+ "browser.newtabpage.activity-stream.unifiedAds.ohttp.enabled" ,
3415+ true
3416+ ) ;
3417+ feed . store . state . Prefs . values [ "unifiedAds.tiles.enabled" ] = true ;
3418+ feed . store . state . Prefs . values [ "unifiedAds.adsFeed.enabled" ] = false ;
3419+ feed . store . state . Prefs . values [ "unifiedAds.endpoint" ] =
3420+ "https://test.endpoint/" ;
3421+ feed . store . state . Prefs . values [ "discoverystream.placements.tiles" ] = "1" ;
3422+ feed . store . state . Prefs . values [ "discoverystream.placements.tiles.counts" ] =
3423+ "1" ;
3424+ feed . store . state . Prefs . values [ "unifiedAds.blockedAds" ] = "" ;
3425+
3426+ const fakeOhttpConfig = { config : "config" } ;
3427+ sandbox . stub ( ObliviousHTTP , "getOHTTPConfig" ) . resolves ( fakeOhttpConfig ) ;
3428+
3429+ const ohttpRequestStub = sandbox
3430+ . stub ( ObliviousHTTP , "ohttpRequest" )
3431+ . resolves ( {
3432+ ok : true ,
3433+ status : 200 ,
3434+ headers : new Map ( [
3435+ [ "cache-control" , "private, max-age=859, stale-if-error=10463" ] ,
3436+ ] ) ,
3437+ json : ( ) =>
3438+ Promise . resolve ( {
3439+ 1 : [
3440+ {
3441+ block_key : 12345 ,
3442+ name : "test" ,
3443+ url : "https://www.test.com" ,
3444+ image_url : "images/test-com.png" ,
3445+ callbacks : {
3446+ click : "https://www.test-click.com" ,
3447+ impression : "https://www.test-impression.com" ,
3448+ } ,
3449+ } ,
3450+ ] ,
3451+ } ) ,
3452+ } ) ;
3453+
3454+ let fetched = await feed . _contile . _fetchSites ( ) ;
3455+
3456+ Assert . ok ( fetched ) ;
3457+ Assert . ok (
3458+ ohttpRequestStub . calledOnce ,
3459+ "ohttpRequest should be called once"
3460+ ) ;
3461+ const callArgs = ohttpRequestStub . getCall ( 0 ) . args ;
3462+ Assert . equal ( callArgs [ 0 ] , "https://relay.url" , "relay URL should match" ) ;
3463+ Assert . deepEqual (
3464+ callArgs [ 1 ] ,
3465+ fakeOhttpConfig ,
3466+ "config should be passed through"
3467+ ) ;
3468+ Assert . equal (
3469+ typeof callArgs [ 3 ] . headers ,
3470+ "object" ,
3471+ "headers should be a plain object"
3472+ ) ;
3473+ Assert . ok (
3474+ // We use instanceof here since isInstance isn't available for
3475+ // Headers, it seems.
3476+ // eslint-disable-next-line mozilla/use-isInstance
3477+ ! ( callArgs [ 3 ] . headers instanceof Headers ) ,
3478+ "headers should not be a Headers instance"
3479+ ) ;
3480+
3481+ Services . prefs . clearUserPref (
3482+ "browser.newtabpage.activity-stream.discoverystream.ohttp.relayURL"
3483+ ) ;
3484+ Services . prefs . clearUserPref (
3485+ "browser.newtabpage.activity-stream.discoverystream.ohttp.configURL"
3486+ ) ;
3487+ Services . prefs . clearUserPref (
3488+ "browser.newtabpage.activity-stream.unifiedAds.ohttp.enabled"
3489+ ) ;
3490+ sandbox . restore ( ) ;
3491+ }
3492+
33983493 Services . prefs . clearUserPref ( TOP_SITES_BLOCKED_SPONSORS_PREF ) ;
33993494} ) ;
0 commit comments