Skip to content

Commit a2e49c1

Browse files
authored
refactor: update request to accept raw JSON or encoded (#1157)
1 parent 99b7ed2 commit a2e49c1

File tree

5 files changed

+26
-39
lines changed

5 files changed

+26
-39
lines changed

src/components/message/Message.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,9 @@ const Message = function ({ markup, meta, parentStyles, warnings }) {
186186

187187
request('GET', `${window.location.origin}/credit-presentment/smart/message?${query}`).then(
188188
({ data: resData }) => {
189-
const encodedData = resData.slice(resData.indexOf('<!--') + 4, resData.indexOf('-->'));
190-
const data = parseObjFromEncoding(encodedData);
189+
const jsonData = resData.slice(resData.indexOf('<!--') + 4, resData.indexOf('-->'));
190+
// TODO: Cleanup ternary and remove 'parseObjFromEncoding' from utils; only JSON.parse will be needed.
191+
const data = jsonData.startsWith('{') ? JSON.parse(jsonData) : parseObjFromEncoding(jsonData);
191192
button.innerHTML = data.markup ?? markup ?? '';
192193
const buttonWidth = button.offsetWidth;
193194
const buttonHeight = button.offsetHeight;

tests/unit/spec/src/components/message/Message.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getByText, fireEvent, queryByText } from '@testing-library/dom';
22

33
import Message from 'src/components/message/Message';
4-
import { request, createState } from 'src/utils';
4+
import { request, createState, parseObjFromEncoding } from 'src/utils';
55
import xPropsMock from 'utils/xPropsMock';
66

77
const ts = {
@@ -63,6 +63,7 @@ describe('Message', () => {
6363
createState.mockClear();
6464
request.mockClear();
6565
xPropsMock.clear();
66+
parseObjFromEncoding.mockClear();
6667
});
6768

6869
test('Renders the button with styles', () => {
@@ -179,4 +180,14 @@ describe('Message', () => {
179180
ts
180181
});
181182
});
183+
184+
test('raw json data from request', async () => {
185+
request.mockReturnValue(
186+
Promise.resolve({
187+
data: '<!--{"markup":"<div>json response</div>","meta":{"messageRequestId":"34567"}}-->'
188+
})
189+
);
190+
Message(serverData);
191+
expect(parseObjFromEncoding).not.toHaveBeenCalled();
192+
});
182193
});

utils/devServerProxy/lib/miscellaneous.js

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,39 +61,15 @@ export const localizeCurrency =
6161

6262
export const waitForTimeout = timeout => new Promise(resolve => setTimeout(resolve, timeout));
6363

64-
export function btoa(str) {
65-
const buffer = Buffer.from(str.toString(), 'binary');
66-
return buffer.toString('base64');
67-
}
68-
69-
export function toBinary(string) {
70-
const codeUnits = new Uint16Array(string.length);
71-
for (let i = 0; i < codeUnits.length; i++) {
72-
codeUnits[i] = string.charCodeAt(i);
73-
}
74-
return String.fromCharCode(...new Uint8Array(codeUnits.buffer));
75-
}
76-
77-
export const createMockZoidMarkup = ({ component, scriptUID, port, encodedData }) => {
64+
export const createMockZoidMarkup = ({ component, scriptUID, port, jsonData }) => {
7865
const setupFunctionName = component === 'message' ? 'crc.setupMessage' : 'crc.setupModal';
7966

8067
const interfaceScript = `<script>var interface = (window.opener ?? window.parent).document.querySelector('[data-uid-auto="${scriptUID}"]').outerHTML; document.write(interface);</script>`;
8168
const componentScript = `<script src="//localhost.paypal.com:${port}/smart-credit-${component}.js"></script>`;
82-
const initializerScript = `<script>${setupFunctionName}(JSON.parse(fromBinary(atob(document.firstChild.nodeValue))))</script>`;
83-
const utilScript = `
84-
<script>
85-
function fromBinary(binary) {
86-
const bytes = new Uint8Array(binary.length);
87-
for (let i = 0; i < bytes.length; i++) {
88-
bytes[i] = binary.charCodeAt(i);
89-
}
90-
return String.fromCharCode.apply(null, new Uint16Array(bytes.buffer));
91-
}
92-
</script>
93-
`;
69+
const initializerScript = `<script>${setupFunctionName}(JSON.parse(document.firstChild.nodeValue))</script>`;
9470

9571
return `
96-
<!--${encodedData}-->
72+
<!--${jsonData}-->
9773
<!DOCTYPE html>
9874
<html lang="en">
9975
<head>
@@ -104,7 +80,6 @@ export const createMockZoidMarkup = ({ component, scriptUID, port, encodedData }
10480
<body>
10581
${component !== 'modal-v2-lander' ? interfaceScript : ''}
10682
${componentScript}
107-
${utilScript}
10883
${initializerScript}
10984
</body>
11085
</html>

utils/devServerProxy/messages.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'path';
33
import got from 'got';
44

55
import { PORT, VARIANT } from '../../src/server/constants';
6-
import { populateTemplate, createMockZoidMarkup, waitForTimeout, btoa, toBinary } from './lib/miscellaneous';
6+
import { populateTemplate, createMockZoidMarkup, waitForTimeout } from './lib/miscellaneous';
77
import getDevAccountDetails from './lib/devAccountDetails';
88

99
// set this environment variable to simulate the time for the request to be answered
@@ -104,8 +104,8 @@ export default function createMessageRoutes(app, server, compiler) {
104104
app.get('/credit-presentment/smart/message', async (req, res) => {
105105
const { scriptUID } = req.query;
106106
const props = await getMessageData(req, compiler);
107-
const encodedData = btoa(toBinary(JSON.stringify(props)));
108-
const markup = createMockZoidMarkup({ component: 'message', encodedData, scriptUID, port });
107+
const jsonData = JSON.stringify(props);
108+
const markup = createMockZoidMarkup({ component: 'message', jsonData, scriptUID, port });
109109

110110
await waitForTimeout(REQUEST_DELAY);
111111

utils/devServerProxy/modals.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createMockZoidMarkup, populateTemplate, waitForTimeout, btoa, toBinary } from './lib/miscellaneous';
1+
import { createMockZoidMarkup, populateTemplate, waitForTimeout } from './lib/miscellaneous';
22
import getDevAccountDetails from './lib/devAccountDetails';
33

44
const REQUEST_DELAY = process.env.REQUEST_DELAY ?? 500;
@@ -53,7 +53,7 @@ export default function createModalRoutes(app, server) {
5353
app.get('/credit-presentment/smart/modal', async (req, res) => {
5454
const { scriptUID } = req.query;
5555
const props = getModalData(req);
56-
const encodedData = btoa(toBinary(JSON.stringify(props)));
56+
const jsonData = JSON.stringify(props);
5757

5858
const postfix = (() => {
5959
if (props.views) {
@@ -77,7 +77,7 @@ export default function createModalRoutes(app, server) {
7777

7878
const markup = createMockZoidMarkup({
7979
component: `modal-${postfix}`,
80-
encodedData,
80+
jsonData,
8181
scriptUID,
8282
port
8383
});
@@ -97,10 +97,10 @@ export default function createModalRoutes(app, server) {
9797

9898
app.get('/credit-presentment/lander/modal', async (req, res) => {
9999
const props = getModalData(req);
100-
const encodedData = btoa(toBinary(JSON.stringify(props)));
100+
const jsonData = JSON.stringify(props);
101101
const markup = createMockZoidMarkup({
102102
component: 'modal-v2-lander',
103-
encodedData,
103+
jsonData,
104104
port
105105
});
106106

0 commit comments

Comments
 (0)