diff --git a/src/app/components/react-testing-library-with-providers.tsx b/src/app/components/react-testing-library-with-providers.tsx index f817794e6bd..b972eaacf42 100644 --- a/src/app/components/react-testing-library-with-providers.tsx +++ b/src/app/components/react-testing-library-with-providers.tsx @@ -31,6 +31,7 @@ interface Props extends PropsWithChildren { isNextJs?: boolean; pageLang?: string; isUK?: boolean | null; + requestServiceChain?: string | null; } const AllTheProviders: FC = ({ @@ -53,6 +54,7 @@ const AllTheProviders: FC = ({ statusCode = null, isNextJs = false, isUK = null, + requestServiceChain = null, }: Props) => { return ( @@ -75,6 +77,7 @@ const AllTheProviders: FC = ({ showCookieBannerBasedOnCountry={showCookieBannerBasedOnCountry} statusCode={statusCode} isUK={isUK} + requestServiceChain={requestServiceChain} > @@ -112,6 +115,7 @@ const customRender = ( isNextJs, pageLang, isUK, + requestServiceChain, } = options || {}; return render(ui, { @@ -135,6 +139,7 @@ const customRender = ( isNextJs={isNextJs} pageLang={pageLang} isUK={isUK} + requestServiceChain={requestServiceChain} > {children} diff --git a/src/app/contexts/RequestContext/index.test.tsx b/src/app/contexts/RequestContext/index.test.tsx index ef56e343057..5a66c5d7fdf 100644 --- a/src/app/contexts/RequestContext/index.test.tsx +++ b/src/app/contexts/RequestContext/index.test.tsx @@ -61,6 +61,7 @@ const input = { showAdsBasedOnLocation: true, mvtExperiments: [{ experimentName: 'foo', variation: 'bar' }], isUK: true, + requestServiceChain: 'SIMORGH', }; const expectedOutput = { @@ -92,6 +93,7 @@ const expectedOutput = { service: 'service', pathname: '/current-path', mvtExperiments: input.mvtExperiments, + requestServiceChain: 'SIMORGH', }; describe('RequestContext', () => { diff --git a/src/app/contexts/RequestContext/index.tsx b/src/app/contexts/RequestContext/index.tsx index dcacc3205eb..92f37a1575b 100644 --- a/src/app/contexts/RequestContext/index.tsx +++ b/src/app/contexts/RequestContext/index.tsx @@ -34,6 +34,7 @@ export type RequestContextProps = { pathname: string; platform: Platforms; previousPath: string | null; + requestServiceChain: string | null; service: Services; showAdsBasedOnLocation: boolean; showCookieBannerBasedOnCountry: boolean; @@ -67,6 +68,7 @@ type RequestProviderProps = { mvtExperiments?: MvtExperiment[] | null; variant?: Variants | null; isUK?: boolean | null; + requestServiceChain?: string | null; }; export const RequestContextProvider = ({ @@ -89,6 +91,7 @@ export const RequestContextProvider = ({ timeOnServer = null, variant = null, isUK = null, + requestServiceChain = null, }: PropsWithChildren) => { const { origin } = getOriginContext(bbcOrigin); const env: Environments = getEnv(origin); @@ -142,6 +145,7 @@ export const RequestContextProvider = ({ pathname, ...getMetaUrls(origin, pathname), mvtExperiments, + requestServiceChain, }), [ derivedPageType, @@ -158,6 +162,7 @@ export const RequestContextProvider = ({ pathname, platform, previousPath, + requestServiceChain, service, showAdsBasedOnLocation, showCookieBannerBasedOnCountry, diff --git a/src/app/legacy/containers/App/App.jsx b/src/app/legacy/containers/App/App.jsx index bb7d57690b4..0dc7542b1c7 100644 --- a/src/app/legacy/containers/App/App.jsx +++ b/src/app/legacy/containers/App/App.jsx @@ -49,6 +49,7 @@ export const App = ({ initialData, bbcOrigin }) => { toggles, mvtExperiments, isUK, + requestServiceChain, } = initialData; const routeProps = getRouteProps(pathname); @@ -67,6 +68,7 @@ export const App = ({ initialData, bbcOrigin }) => { showCookieBannerBasedOnCountry, mvtExperiments, isUK, + requestServiceChain, }); }; diff --git a/src/app/legacy/containers/PageHandlers/__snapshots__/withContexts.test.jsx.snap b/src/app/legacy/containers/PageHandlers/__snapshots__/withContexts.test.jsx.snap index ba65c51612c..e4673b1b308 100644 --- a/src/app/legacy/containers/PageHandlers/__snapshots__/withContexts.test.jsx.snap +++ b/src/app/legacy/containers/PageHandlers/__snapshots__/withContexts.test.jsx.snap @@ -6,7 +6,7 @@ exports[`withContexts HOC should return all context providers 1`] = ` {"lang":"en-GB","articleAuthor":"https://www.facebook.com/bbcnews","articleTimestampPrefix":"Updated","articleTimestampSuffix":"","atiAnalyticsAppName":"news","atiAnalyticsProducerId":"64","chartbeatDomain":"bbc.co.uk","brandName":"BBC News","product":"BBC News","defaultImage":"https://static.files.bbci.co.uk/ws/simorgh-assets/public/news/images/metadata/poster-1024x576.png","defaultImageAltText":"BBC News","dir":"ltr","externalLinkText":", external","imageCaptionOffscreenText":"Image caption, ","videoCaptionOffscreenText":"Video caption, ","audioCaptionOffscreenText":"Audio caption","defaultCaptionOffscreenText":"Caption, ","imageCopyrightOffscreenText":"Image source, ","locale":"en_GB","datetimeLocale":"en-gb","service":"news","serviceName":"News","languageName":"English","twitterCreator":"@BBCNews","twitterSite":"@BBCNews","noBylinesPolicy":"https://www.bbc.com/news/help-41670342#authorexpertise","publishingPrinciples":"https://www.bbc.com/news/help-41670342","isTrustProjectParticipant":true,"script":{"atlas":{"groupA":{"fontSize":78,"lineHeight":84},"groupB":{"fontSize":96,"lineHeight":104},"groupD":{"fontSize":140,"lineHeight":148}},"elephant":{"groupA":{"fontSize":60,"lineHeight":64},"groupB":{"fontSize":78,"lineHeight":84},"groupD":{"fontSize":116,"lineHeight":124}},"imperial":{"groupA":{"fontSize":50,"lineHeight":54},"groupB":{"fontSize":64,"lineHeight":72},"groupD":{"fontSize":96,"lineHeight":104}},"royal":{"groupA":{"fontSize":40,"lineHeight":44},"groupB":{"fontSize":52,"lineHeight":60},"groupD":{"fontSize":76,"lineHeight":84}},"foolscap":{"groupA":{"fontSize":32,"lineHeight":36},"groupB":{"fontSize":40,"lineHeight":44},"groupD":{"fontSize":56,"lineHeight":60}},"canon":{"groupA":{"fontSize":28,"lineHeight":32},"groupB":{"fontSize":32,"lineHeight":36},"groupD":{"fontSize":44,"lineHeight":48}},"trafalgar":{"groupA":{"fontSize":20,"lineHeight":24},"groupB":{"fontSize":24,"lineHeight":28},"groupD":{"fontSize":32,"lineHeight":36}},"paragon":{"groupA":{"fontSize":20,"lineHeight":24},"groupB":{"fontSize":22,"lineHeight":26},"groupD":{"fontSize":28,"lineHeight":32}},"doublePica":{"groupA":{"fontSize":18,"lineHeight":22},"groupB":{"fontSize":20,"lineHeight":24},"groupD":{"fontSize":24,"lineHeight":28}},"greatPrimer":{"groupA":{"fontSize":18,"lineHeight":22},"groupB":{"fontSize":18,"lineHeight":22},"groupD":{"fontSize":20,"lineHeight":24}},"bodyCopy":{"groupA":{"fontSize":15,"lineHeight":20},"groupB":{"fontSize":16,"lineHeight":22},"groupD":{"fontSize":16,"lineHeight":22}},"pica":{"groupA":{"fontSize":15,"lineHeight":20},"groupB":{"fontSize":16,"lineHeight":20},"groupD":{"fontSize":16,"lineHeight":20}},"longPrimer":{"groupA":{"fontSize":15,"lineHeight":18},"groupB":{"fontSize":15,"lineHeight":18},"groupD":{"fontSize":14,"lineHeight":18}},"brevier":{"groupA":{"fontSize":14,"lineHeight":18},"groupB":{"fontSize":14,"lineHeight":18},"groupD":{"fontSize":13,"lineHeight":16}},"minion":{"groupA":{"fontSize":12,"lineHeight":16},"groupB":{"fontSize":12,"lineHeight":16},"groupD":{"fontSize":12,"lineHeight":16}}},"manifestPath":"/articles/manifest.json","frontPageTitle":"Home","showAdPlaceholder":false,"showRelatedTopics":true,"translations":{"ads":{"advertisementLabel":"Advertisement"},"home":"Home","currentPage":"Current page","skipLinkText":"Skip to content","relatedContent":"Related content","relatedTopics":"Related topics","navMenuText":"Sections","mediaAssetPage":{"mediaPlayer":"Media player","audioPlayer":"Audio player","videoPlayer":"Video player"},"liveExperiencePage":{"liveLabel":"Live","liveCoverage":"Live Coverage","breaking":"Breaking","postedAt":"Posted at","summary":"Summary","shareButtonText":"Share"},"downloads":{"instructions":"You can download and view today’s news.","title":"File Download"},"gist":"At a glance","error":{"404":{"statusCode":"404","title":"Page cannot be found","message":"Sorry, we're unable to bring you the page you're looking for. Please try:","solutions":["Double checking the url","Hitting the refresh button in your browser","Searching for this page using the BBC search bar"],"callToActionFirst":"Alternatively, please visit the ","callToActionLinkText":"BBC News homepage.","callToActionLast":"","callToActionLinkUrl":"https://www.bbc.com/news"},"500":{"statusCode":"500","title":"Internal server error","message":"Sorry, we're currently unable to bring you the page you're looking for. Please try:","solutions":["Hitting the refresh button in your browser","Coming back again later"],"callToActionFirst":"Alternatively, please visit the ","callToActionLinkText":"BBC News homepage.","callToActionLast":"","callToActionLinkUrl":"https://www.bbc.com/news"}},"consentBanner":{"privacy":{"title":"We've updated our Privacy and Cookies Policy","description":{"uk":{"first":"We've made some important changes to our Privacy and Cookies Policy and we want you to know what this means for you and your data.","linkText":null,"last":null,"linkUrl":null},"international":{"first":"We've made some important changes to our Privacy and Cookies Policy and we want you to know what this means for you and your data.","linkText":null,"last":null,"linkUrl":null}},"accept":"OK","reject":"Find out what's changed","rejectUrl":"https://www.bbc.co.uk/usingthebbc/your-data-matters"},"cookie":{"amp":{"accept":"Accept data collection and continue","reject":"Reject data collection and continue","initial":{"title":"Let us know you agree to data collection on AMP","description":{"first":"We and our partners use technologies, such as ","linkText":"cookies","last":", and collect browsing data to give you the best online experience and to personalise the content and advertising shown to you. Please let us know if you agree.","linkUrl":"https://www.bbc.co.uk/usingthebbc/cookies/what-do-i-need-to-know-about-cookies/"},"manage":"Manage my settings"},"manage":{"title":"Manage consent settings on AMP pages","description":{"para1":"These settings apply to AMP pages only. You may be asked to set these preferences again when you visit non-AMP BBC pages.","para2":"The lightweight mobile page you have visited has been built using Google AMP technology.","heading2":"Strictly necessary data collection","para3":"To make our web pages work, we store some limited information on your device without your consent.","para4":{"text":"Read more about the essential information we store on your device to make our web pages work.","url":"https://www.bbc.co.uk/usingthebbc/strictly-necessary-cookies/"},"para5":"We use local storage to store your consent preferences on your device.","heading3":"Optional data collection","para6":"When you consent to data collection on AMP pages you are consenting to allow us to display personalised ads that are relevant to you when you are outside of the UK.","para7":{"text":"Read more about how we personalise ads in the BBC and our advertising partners.","url":"https://www.bbc.com/usingthebbc/cookies/how-does-the-bbc-use-cookies-for-advertising/"},"para8":"You can choose not to receive personalised ads by clicking “Reject data collection and continue” below. Please note that you will still see advertising, but it will not be personalised to you.","para9":"You can change these settings by clicking “Ad Choices / Do not sell my info” in the footer at any time."}}},"canonical":{"title":"Let us know you agree to cookies","description":{"uk":{"first":"We use ","linkText":"cookies","last":" to give you the best online experience. Please let us know if you agree to all of these cookies.","linkUrl":"https://www.bbc.co.uk/usingthebbc/cookies/what-do-i-need-to-know-about-cookies/"},"international":{"first":"We use ","linkText":"cookies","last":" to give you the best online experience. Please let us know if you agree to all of these cookies.","linkUrl":"https://www.bbc.co.uk/usingthebbc/cookies/what-do-i-need-to-know-about-cookies/"}},"accept":"Yes, I agree","reject":"No, take me to settings","rejectUrl":"https://www.bbc.co.uk/usingthebbc/cookies/how-can-i-change-my-bbc-cookie-settings/"}}},"media":{"noJs":"To play this content, please enable JavaScript, or try a different browser","contentExpired":"This content is no longer available","audio":"Audio","photogallery":"Image gallery","video":"Video","listen":"Listen","watch":"Watch","liveLabel":"LIVE","nextLabel":"NEXT","previousRadioShow":"Previous radio show","nextRadioShow":"Next radio show","duration":"Duration"},"socialEmbed":{},"featuresAnalysisTitle":"More to explore"},"mostRead":{"header":"Most read","lastUpdated":"Last updated:","numberOfItems":10,"hasMostRead":true},"radioSchedule":{"hasRadioSchedule":false},"recommendations":{"hasStoryRecommendations":false},"footer":{"trustProjectLink":{"href":"https://www.bbc.com/news/help-41670342","text":"Why you can trust the BBC"},"externalLink":{"href":"https://www.bbc.co.uk/editorialguidelines/guidance/feeds-and-links","text":"Read about our approach to external linking."},"links":[{"href":"https://www.bbc.com/terms","text":"Terms of Use"},{"href":"https://www.bbc.co.uk/aboutthebbc/","text":"About the BBC"},{"href":"https://www.bbc.com/privacy/","text":"Privacy Policy"},{"href":"https://www.bbc.com/usingthebbc/cookies/","text":"Cookies"},{"href":"https://www.bbc.com/accessibility/","text":"Accessibility Help"},{"href":"https://www.bbc.com/contact/","text":"Contact the BBC"},{"id":"COOKIE_SETTINGS","href":"#","text":"Do not share or sell my info","lang":"en-GB"}],"copyrightText":"BBC. The BBC is not responsible for the content of external sites."},"timezone":"Europe/London","navigation":[{"title":"Home","url":"/news"},{"title":"UK","url":"/news/uk"},{"title":"World","url":"/news/world"},{"title":"Business","url":"/news/business"},{"title":"Politics","url":"/news/politics"},{"title":"Tech","url":"/news/technology"},{"title":"Science","url":"/news/science_and_environment"},{"title":"Health","url":"/news/health"},{"title":"Family & Education","url":"/news/education"},{"title":"Entertainment & Arts","url":"/news/entertainment_and_arts"},{"title":"Stories","url":"/news/stories"}]} - {"env":"live","id":"c0000000000o","isUK":true,"origin":"https://www.bbc.com","pageType":"article","derivedPageType":null,"isAmp":true,"isApp":false,"isLite":false,"isNextJs":false,"platform":"amp","statsDestination":"NEWS_PS_TEST","statsPageIdentifier":"news.articles.c0000000000o.page","statusCode":200,"previousPath":null,"variant":null,"timeOnServer":null,"showAdsBasedOnLocation":true,"showCookieBannerBasedOnCountry":true,"service":"news","pathname":"/pathname","canonicalLink":"https://www.bbc.com/pathname","ampLink":"https://www.bbc.com/pathname.amp","canonicalUkLink":"https://www.bbc.co.uk/pathname","ampUkLink":"https://www.bbc.co.uk/pathname.amp","canonicalNonUkLink":"https://www.bbc.com/pathname","ampNonUkLink":"https://www.bbc.com/pathname.amp","mvtExperiments":[{"experimentName":"foo","variation":"bar"}]} + {"env":"live","id":"c0000000000o","isUK":true,"origin":"https://www.bbc.com","pageType":"article","derivedPageType":null,"isAmp":true,"isApp":false,"isLite":false,"isNextJs":false,"platform":"amp","statsDestination":"NEWS_PS_TEST","statsPageIdentifier":"news.articles.c0000000000o.page","statusCode":200,"previousPath":null,"variant":null,"timeOnServer":null,"showAdsBasedOnLocation":true,"showCookieBannerBasedOnCountry":true,"service":"news","pathname":"/pathname","canonicalLink":"https://www.bbc.com/pathname","ampLink":"https://www.bbc.com/pathname.amp","canonicalUkLink":"https://www.bbc.co.uk/pathname","ampUkLink":"https://www.bbc.co.uk/pathname.amp","canonicalNonUkLink":"https://www.bbc.com/pathname","ampNonUkLink":"https://www.bbc.com/pathname.amp","mvtExperiments":[{"experimentName":"foo","variation":"bar"}],"requestServiceChain":null} {"toggleState":{"testToggle":{"enabled":false}},"toggleDispatch":"*function - bound dispatchReducerAction*"} diff --git a/src/app/legacy/containers/PageHandlers/withContexts.jsx b/src/app/legacy/containers/PageHandlers/withContexts.jsx index 665060702ce..98d15b3af06 100644 --- a/src/app/legacy/containers/PageHandlers/withContexts.jsx +++ b/src/app/legacy/containers/PageHandlers/withContexts.jsx @@ -30,6 +30,7 @@ const WithContexts = Component => { mvtExperiments = null, isNextJs = false, isUK = false, + requestServiceChain = null, } = props; const { metadata: { atiAnalytics } = {} } = pageData ?? {}; @@ -60,6 +61,7 @@ const WithContexts = Component => { mvtExperiments={mvtExperiments} isNextJs={isNextJs} isUK={isUK} + requestServiceChain={requestServiceChain} > ( ); +const PreviewEnvironmentIndicator = () => { + const StyledDiv = styled.div` + color: ${props => props.theme.palette.WHITE}; + `; + return ⚠️ Mozart; +}; + const Brand = forwardRef((props, ref) => { + const [isPreview, setIsPreview] = useState(false); + + useEffect(() => { + const hostnameMatchesPreview = window.location.hostname.match(/preview/g); + const isPreviewEnvironment = + hostnameMatchesPreview && hostnameMatchesPreview.length > 0; + + if (isPreviewEnvironment) { + setIsPreview(isPreviewEnvironment); + } + }, []); + + const { requestServiceChain } = useContext(RequestContext); + + const shouldDisplayPreviewIndicator = + isPreview && requestServiceChain && requestServiceChain.includes('MOZART'); + const { svgHeight, maxWidth, @@ -205,6 +230,7 @@ const Brand = forwardRef((props, ref) => { )} {skipLink} {scriptLink &&
{scriptLink}
} + {shouldDisplayPreviewIndicator && } ); diff --git a/src/app/legacy/psammead/psammead-brand/src/index.test.jsx b/src/app/legacy/psammead/psammead-brand/src/index.test.jsx index 6ed773385c6..7c14ed8a4ff 100644 --- a/src/app/legacy/psammead/psammead-brand/src/index.test.jsx +++ b/src/app/legacy/psammead/psammead-brand/src/index.test.jsx @@ -1,5 +1,10 @@ import React, { useRef, useEffect } from 'react'; -import { suppressPropWarnings } from '#psammead/psammead-test-helpers/src'; +import { + suppressPropWarnings, + setWindowValue, + resetWindowValue, +} from '#psammead/psammead-test-helpers/src'; + import { POSTBOX, WHITE } from '../../../../components/ThemeProvider/palette'; import { render } from '../../../../components/react-testing-library-with-providers'; import Brand from '.'; @@ -196,4 +201,55 @@ describe('Brand', () => { expect(brandLink).toBe(container.querySelector('a')); }); }); + + describe('Preview Indicator', () => { + const windowLocation = window.location; + + afterEach(() => { + resetWindowValue('location', windowLocation); + }); + + it('should only be displayed when the hostname includes preview and requestServiceChain contains MOZART', () => { + setWindowValue('location', { + hostname: 'http://localhost-preview.test.bbc.com', + }); + + const { getByText } = render(, { + requestServiceChain: 'MOZART,SIMORGH', + }); + + expect(getByText('⚠️ Mozart')).toBeInTheDocument(); + }); + + it.each([ + 'http://localhost:7080/pidgin', + 'https://www.test.bbc.com/pidgin', + 'https://www.bbc.com/pidgin', + ])( + 'should not be displayed when hostname is %s because hostname does not include preview', + hostname => { + setWindowValue('location', { + hostname, + }); + + const { queryByText } = render(, { + requestServiceChain: 'MOZART,SIMORGH', + }); + + expect(queryByText('⚠️ Mozart')).not.toBeInTheDocument(); + }, + ); + + it('should not be displayed when hostname is preview but requestServiceChain does not contain MOZART', () => { + setWindowValue('location', { + hostname: 'http://localhost-preview.test.bbc.com', + }); + + const { queryByText } = render(, { + requestServiceChain: 'SIMORGH', + }); + + expect(queryByText('⚠️ Mozart')).not.toBeInTheDocument(); + }); + }); }); diff --git a/src/server/index.jsx b/src/server/index.jsx index 167af5e38eb..6838fdbd51a 100644 --- a/src/server/index.jsx +++ b/src/server/index.jsx @@ -233,7 +233,8 @@ server.get( isAmp, }); - const { isUK, showCookieBannerBasedOnCountry } = extractHeaders(headers); + const { isUK, showCookieBannerBasedOnCountry, requestServiceChain } = + extractHeaders(headers); data.toggles = toggles; data.path = urlPath; @@ -242,8 +243,10 @@ server.get( data.showCookieBannerBasedOnCountry = showCookieBannerBasedOnCountry; data.isUK = isUK; data.isLite = isLite; + data.requestServiceChain = requestServiceChain; let { status } = data; + // Set derivedPageType based on returned page data if (status === OK) { derivedPageType = ramdaPath(['pageData', 'metadata', 'type'], data); @@ -274,7 +277,8 @@ server.get( url, variant, }); - } catch ({ message }) { + } catch (error) { + const { message } = error; status = 500; sendCustomMetric({ metricName: NON_200_RESPONSE, diff --git a/src/server/utilities/extractHeaders/index.test.ts b/src/server/utilities/extractHeaders/index.test.ts index 20021cde0fa..b4514168950 100644 --- a/src/server/utilities/extractHeaders/index.test.ts +++ b/src/server/utilities/extractHeaders/index.test.ts @@ -5,24 +5,28 @@ describe('extractHeader', () => { const actual = extractHeaders({ 'x-bbc-edge-isuk': 'yes', }); - expect(actual).toStrictEqual({ - bbcOrigin: null, - isUK: true, - showAdsBasedOnLocation: false, - showCookieBannerBasedOnCountry: true, - }); + expect(actual).toStrictEqual( + expect.objectContaining({ + bbcOrigin: null, + isUK: true, + showAdsBasedOnLocation: false, + showCookieBannerBasedOnCountry: true, + }), + ); }); it(`sets isUK to true when 'x-country' is set to 'gb'`, () => { const actual = extractHeaders({ 'x-country': 'gb', }); - expect(actual).toStrictEqual({ - bbcOrigin: null, - isUK: true, - showAdsBasedOnLocation: false, - showCookieBannerBasedOnCountry: true, - }); + expect(actual).toStrictEqual( + expect.objectContaining({ + bbcOrigin: null, + isUK: true, + showAdsBasedOnLocation: false, + showCookieBannerBasedOnCountry: true, + }), + ); }); it(`sets showCookieBannerBasedOnCountry to false when 'x-country' is set to 'za' and 'x-bbc-edge-isuk' is set to 'no'`, () => { @@ -30,12 +34,14 @@ describe('extractHeader', () => { 'x-country': 'za', 'x-bbc-edge-isuk': 'no', }); - expect(actual).toStrictEqual({ - bbcOrigin: null, - isUK: false, - showAdsBasedOnLocation: false, - showCookieBannerBasedOnCountry: false, - }); + expect(actual).toStrictEqual( + expect.objectContaining({ + bbcOrigin: null, + isUK: false, + showAdsBasedOnLocation: false, + showCookieBannerBasedOnCountry: false, + }), + ); }); it(`sets showCookieBannerBasedOnCountry to true when 'x-country' is set to 'za' and 'x-bbc-edge-isuk' is set to 'yes'`, () => { @@ -43,47 +49,77 @@ describe('extractHeader', () => { 'x-country': 'za', 'x-bbc-edge-isuk': 'yes', }); - expect(actual).toStrictEqual({ - bbcOrigin: null, - isUK: true, - showAdsBasedOnLocation: false, - showCookieBannerBasedOnCountry: true, - }); + expect(actual).toStrictEqual( + expect.objectContaining({ + bbcOrigin: null, + isUK: true, + showAdsBasedOnLocation: false, + showCookieBannerBasedOnCountry: true, + }), + ); }); it(`sets showCookieBannerBasedOnCountry to false when 'x-bbc-edge-country' is set to 'za'`, () => { const actual = extractHeaders({ 'x-bbc-edge-country': 'za', }); - expect(actual).toStrictEqual({ - bbcOrigin: null, - isUK: null, - showAdsBasedOnLocation: false, - showCookieBannerBasedOnCountry: false, - }); + expect(actual).toStrictEqual( + expect.objectContaining({ + bbcOrigin: null, + isUK: null, + showAdsBasedOnLocation: false, + showCookieBannerBasedOnCountry: false, + }), + ); }); it(`sets showAdsBasedOnLocation to true when 'bbc-adverts' header is set to 'true'`, () => { const actual = extractHeaders({ 'bbc-adverts': 'true', }); - expect(actual).toStrictEqual({ - bbcOrigin: null, - isUK: null, - showAdsBasedOnLocation: true, - showCookieBannerBasedOnCountry: true, - }); + expect(actual).toStrictEqual( + expect.objectContaining({ + bbcOrigin: null, + isUK: null, + showAdsBasedOnLocation: true, + showCookieBannerBasedOnCountry: true, + }), + ); }); it(`sets bbcOrigin when 'bbc-origin' header is set`, () => { const actual = extractHeaders({ 'bbc-origin': 'https://www.bbc.co.uk/news', }); - expect(actual).toStrictEqual({ - bbcOrigin: 'https://www.bbc.co.uk/news', - isUK: null, - showAdsBasedOnLocation: false, - showCookieBannerBasedOnCountry: true, + expect(actual).toStrictEqual( + expect.objectContaining({ + bbcOrigin: 'https://www.bbc.co.uk/news', + isUK: null, + showAdsBasedOnLocation: false, + showCookieBannerBasedOnCountry: true, + }), + ); + }); + + describe('requestServiceChain', () => { + it('is set to empty string if req-svc-chain header does not exist', () => { + const actual = extractHeaders({}); + + expect(actual).toStrictEqual( + expect.objectContaining({ + requestServiceChain: '', + }), + ); + }); + + it('is set to the value of the req-svc-chain header', () => { + const actual = extractHeaders({ 'req-svc-chain': 'SIMORGH' }); + + expect(actual).toStrictEqual( + expect.objectContaining({ + requestServiceChain: 'SIMORGH', + }), + ); }); }); }); diff --git a/src/server/utilities/extractHeaders/index.ts b/src/server/utilities/extractHeaders/index.ts index 1ac648897e5..6610948ae94 100644 --- a/src/server/utilities/extractHeaders/index.ts +++ b/src/server/utilities/extractHeaders/index.ts @@ -4,6 +4,8 @@ import { COUNTRIES_WITH_COOKIE_BANNER } from '#app/lib/utilities/cookieCountries const extractHeaders = (headers: IncomingHttpHeaders) => { let isUK = null; let showCookieBannerBasedOnCountry = true; + let requestServiceChain = ''; + if (headers['x-bbc-edge-isuk']) { isUK = headers['x-bbc-edge-isuk'] === 'yes'; } @@ -23,11 +25,16 @@ const extractHeaders = (headers: IncomingHttpHeaders) => { ); } + if (headers['req-svc-chain']) { + requestServiceChain = headers['req-svc-chain'].toString(); + } + return { bbcOrigin: headers['bbc-origin'] || null, isUK, showAdsBasedOnLocation: headers['bbc-adverts'] === 'true' || false, showCookieBannerBasedOnCountry, + requestServiceChain, }; };