Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 'debug' query param for the web-sdk bridge #219

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice test coverage!

Loading