diff --git a/USAGE.md b/USAGE.md index 7c47d37..3f91804 100644 --- a/USAGE.md +++ b/USAGE.md @@ -50,13 +50,13 @@ const {headerValue, error} = getGovClientBrowserDoNotTrackHeader(); * To get Gov-Client-Timezone HMRC Fraud prevention header: ```js -import getGovClientTimezoneHeader from 'user-data-for-fraud-prevention'; +import {getGovClientTimezoneHeader} from 'user-data-for-fraud-prevention'; const {headerValue, error} = getGovClientTimezoneHeader(); ``` * To get Gov-Client-Local-IPs HMRC Fraud prevention header: ```js -import getGovClientLocalIPsHeader from 'user-data-for-fraud-prevention'; +import {getGovClientLocalIPsHeader} from 'user-data-for-fraud-prevention'; const {headerValue, error} = await getGovClientLocalIPsHeader(); ``` @@ -65,3 +65,9 @@ const {headerValue, error} = await getGovClientLocalIPsHeader(); import {getGovClientWindowSizeHeader} from 'user-data-for-fraud-prevention'; const {headerValue, error} = getGovClientWindowSizeHeader(); ``` + +* To get Gov-Client-Screens HMRC Fraud prevention header: +```js +import {getGovClientScreensHeader} from 'user-data-for-fraud-prevention'; +const {headerValue, error} = getGovClientScreensHeader(); +``` diff --git a/src/js/hmrc/mtdFraudPrevention.js b/src/js/hmrc/mtdFraudPrevention.js index 9487723..42421fd 100644 --- a/src/js/hmrc/mtdFraudPrevention.js +++ b/src/js/hmrc/mtdFraudPrevention.js @@ -185,3 +185,15 @@ export const getGovClientWindowSizeHeader = () => { }; + +/** + * Returns the value for Gov-Client-Screens HMRC Fraud prevention header. + * @returns {object} with headerValue key having the value of the header or error key if there is an error + */ +export const getGovClientScreensHeader = () => { + try { + return {headerValue: getScreenData()}; + } catch (error) { + return {error}; + } +} \ No newline at end of file diff --git a/src/js/index.ts b/src/js/index.ts index d4b2637..77857e3 100644 --- a/src/js/index.ts +++ b/src/js/index.ts @@ -9,5 +9,6 @@ export { getGovClientDeviceID, getGovClientTimezoneHeader, getGovClientLocalIPsHeader, - getGovClientWindowSizeHeader + getGovClientWindowSizeHeader, + getGovClientScreensHeader } from './hmrc/mtdFraudPrevention'; diff --git a/tests/unit/hmrc/mtdFraudPrevention.test.js b/tests/unit/hmrc/mtdFraudPrevention.test.js index 2b5b210..07c8c7d 100644 --- a/tests/unit/hmrc/mtdFraudPrevention.test.js +++ b/tests/unit/hmrc/mtdFraudPrevention.test.js @@ -19,6 +19,7 @@ import {getGovClientDeviceID} from "../../../src/js/hmrc/mtdFraudPrevention"; import {getGovClientBrowserDoNotTrackHeader} from "../../../src/js/hmrc/mtdFraudPrevention"; import {getGovClientTimezoneHeader} from "../../../src/js/hmrc/mtdFraudPrevention"; import {getGovClientWindowSizeHeader} from "../../../src/js/hmrc/mtdFraudPrevention" +import {getGovClientScreensHeader} from "../../../src/js/hmrc/mtdFraudPrevention"; describe("FraudPreventionHeaders", () => { resetDeviceIpString(); @@ -372,3 +373,45 @@ describe("getGovClientWindowSizeHeader", () => { + +describe("GovClientScreensHeader", () => { + let screenSpy, windowSpy; + + beforeEach(() => { + screenSpy = jest.spyOn(global, 'screen', 'get'); + windowSpy = jest.spyOn(global, 'window', 'get'); + }); + + afterEach(() => { + screenSpy.mockRestore(); + windowSpy.mockRestore(); + }); + + it("returns correct headerValue when there is no error", () => { + screenSpy.mockImplementation(() => ({ + width: 1019, + height: 1021, + colorDepth: 17, + })); + + windowSpy.mockImplementation(() => ({ + devicePixelRatio: 2, + })); + + const {headerValue, error} = getGovClientScreensHeader(); + expect(error).toBe(undefined); + expect(headerValue).toBe(`width=1019&height=1021&scaling-factor=2&colour-depth=17`); + }); + + it("returns error when there is an error", () => { + screenSpy.mockImplementation(() => {throw Error("Something went wrong.")}); + + windowSpy.mockImplementation(() => ({ + devicePixelRatio: 2, + })); + + const {headerValue, error} = getGovClientScreensHeader(); + expect(error).toEqual(Error("Something went wrong.")); + expect(headerValue).toBe(undefined); + }); +});