Skip to content

Commit

Permalink
Add support for 'debug' query param for the web-sdk bridge (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregjopa authored Oct 21, 2024
1 parent b947425 commit 1bd5666
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
14 changes: 13 additions & 1 deletion server/meta.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ function validateWebSDKUrl({ pathname, query }) {
);
}
// check for extraneous parameters
const validWebSDKBridgeParams = ["origin", "version", "payment-flow"];
const validWebSDKBridgeParams = [
"origin",
"version",
"payment-flow",
"debug",
];
for (const param of Object.keys(query)) {
if (!validWebSDKBridgeParams.includes(param)) {
throw new Error(`Invalid parameter on web-sdk bridge url: ${param}`);
Expand All @@ -116,6 +121,13 @@ function validateWebSDKUrl({ pathname, query }) {
);
}

// validate the optional debug parameter
if (query.debug && !["true", "false"].includes(query.debug)) {
throw new Error(
`Invalid debug parameter on web-sdk bridge url: ${query["debug"]}`
);
}

// validate the origin parameter
let url = null;
try {
Expand Down
56 changes: 55 additions & 1 deletion server/meta.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,7 @@ test("should error when invalid characters are found in the subdomain - we allow

test("should construct a valid web-sdk bridge url", () => {
const sdkUrl =
"https://www.paypal.com/web-sdk/v6/bridge?version=1.2.3&origin=https%3A%2F%2Fwww.example.com%3A8000&payment-flow=payment-handler";
"https://www.paypal.com/web-sdk/v6/bridge?version=1.2.3&origin=https%3A%2F%2Fwww.example.com%3A8000&payment-flow=payment-handler&debug=true";
const sdkUID = "abc123";

const { getSDKLoader } = unpackSDKMeta(
Expand Down Expand Up @@ -1276,6 +1276,35 @@ test("should error when extra parameters are present", () => {
}
});

test("should not error when the optional debug parameter is missing", () => {
const sdkUrl =
"https://www.paypal.com/web-sdk/v6/bridge?version=1.2.3&origin=https%3A%2F%2Fwww.example.com%3A8000&payment-flow=payment-handler";
const sdkUID = "abc123";

const { getSDKLoader } = unpackSDKMeta(
Buffer.from(
JSON.stringify({
url: sdkUrl,
attrs: {
"data-uid": sdkUID,
},
})
).toString("base64")
);

const $ = cheerio.load(getSDKLoader());
const script = $("script");
const src = script.attr("src");
const uid = script.attr("data-uid");

if (src !== sdkUrl) {
throw new Error(`Expected script url to be ${sdkUrl} - got ${src}`);
}
if (uid !== sdkUID) {
throw new Error(`Expected data UID be ${sdkUID} - got ${uid}`);
}
});

test("should error when the version parameter is missing", () => {
const sdkUrl =
"https://www.paypal.com/web-sdk/v6/bridge?origin=https%3A%2F%2Fwww.example.com%3A8000&payment-flow=payment-handler";
Expand Down Expand Up @@ -1425,3 +1454,28 @@ test("should error when the payment-flow parameter is invalid", () => {
throw new Error("Expected error to be thrown");
}
});

test("should error when the debug parameter is invalid", () => {
const sdkUrl =
"https://www.paypal.com/web-sdk/v6/bridge?version=1.2.3&origin=https%3A%2F%2Fwww.example.com%3A8000&payment-flow=popup&debug=invalid-value";

let error = null;
try {
unpackSDKMeta(
Buffer.from(
JSON.stringify({
url: sdkUrl,
attrs: {
"data-uid": "abc123",
},
})
).toString("base64")
);
} catch (err) {
error = err;
}

if (!error) {
throw new Error("Expected error to be thrown");
}
});

0 comments on commit 1bd5666

Please sign in to comment.