Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
timokoessler committed Feb 7, 2025
1 parent 55dea8a commit 4fb740c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 21 deletions.
25 changes: 25 additions & 0 deletions library/sources/Sax.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ const context: Context = {
route: "/posts/:id",
};

const contextWithQuery: Context = {
remoteAddress: "::1",
method: "POST",
url: "http://localhost:4000",
query: { xml: "<root><abc>123</abc></root>" },
headers: {},
body: undefined,
cookies: {},
routeParams: {},
source: "express",
route: "/posts/:id",
};

t.test("it works", async (t) => {
const agent = createTestAgent({
block: true,
Expand Down Expand Up @@ -142,6 +155,18 @@ t.test("it works", async (t) => {
parser.write(true).close();
t.same(getContext()?.xml, undefined);
});

runWithContext(contextWithQuery, () => {
let text = "";
const parser = sax.parser(true);
parser.ontext = function onText(txt) {
text += txt;
};

t.same(getContext()?.xml, undefined);
parser.write(contextWithQuery.query.xml as string).close();
t.same(getContext()?.xml, ["123"]);
});
});

t.test("it works with streams", (t) => {
Expand Down
49 changes: 28 additions & 21 deletions library/sources/sax/wrapEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,38 @@ export function wrapEvents(
) {
for (const event of eventsToWrap) {
const eventFunctionName = `on${event}`;

if (!(eventFunctionName in saxParser)) {
continue;
}

const eventFunction = saxParser[eventFunctionName];

// Check if the event function is set by the user and not already wrapped
if (typeof eventFunction === "function" && !isWrapped(eventFunction)) {
// Wrap the event function to get the results
wrap(saxParser, eventFunctionName, (original) => {
return function wrappedEventFunction() {
const result = original.apply(
// @ts-expect-error We don't know the type of this
this,
// eslint-disable-next-line prefer-rest-params
arguments
);
// If false, write was called a second time with xml that is not in the body
if (!saxParser["_aikido_add_to_context"]) {
return result;
}
if (typeof eventFunction !== "function" || isWrapped(eventFunction)) {
continue;
}

// Wrap the event function to get the results
wrap(saxParser, eventFunctionName, (original) => {
return function wrappedEventFunction() {
const result = original.apply(
// @ts-expect-error We don't know the type of this
this,
// eslint-disable-next-line prefer-rest-params
const args = Array.from(arguments);
for (const arg of args) {
addXmlToContext(arg, context);
}
arguments
);
// If false, write was called a second time with xml that is not in the body
if (!saxParser["_aikido_add_to_context"]) {
return result;
};
});
}
}
// eslint-disable-next-line prefer-rest-params
const args = Array.from(arguments);
for (const arg of args) {
addXmlToContext(arg, context);
}
return result;
};
});
}
}

0 comments on commit 4fb740c

Please sign in to comment.