Skip to content

Commit

Permalink
feat: add removeResponseHeader and clearResponseHeaders utils (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hebilicious authored Jul 26, 2023
1 parent ef4882d commit 29445c4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/utils/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,30 @@ export function appendResponseHeader(

export const appendHeader = appendResponseHeader;

/**
* Remove all response headers, or only those specified in the headerNames array.
* @param event H3 event
* @param headerNames Array of header names to remove
*/
export function clearResponseHeaders(
event: H3Event,
headerNames?: string[]
): void {
if (headerNames && headerNames.length > 0) {
for (const name of headerNames) {
removeResponseHeader(event, name);
}
} else {
for (const [name] of Object.entries(getResponseHeaders(event))) {
removeResponseHeader(event, name);
}
}
}

export function removeResponseHeader(event: H3Event, name: string): void {
return event.node.res.removeHeader(name);
}

export function isStream(data: any): data is Readable | ReadableStream {
if (!data || typeof data !== "object") {
return false;
Expand Down
48 changes: 48 additions & 0 deletions test/header.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
appendHeader,
toNodeListener,
eventHandler,
removeResponseHeader,
clearResponseHeaders,
} from "../src";

describe("", () => {
Expand Down Expand Up @@ -267,4 +269,50 @@ describe("", () => {
expect(result.headers["nuxt-http-header"]).toEqual("value 1, value 2");
});
});

describe("clearResponseHeaders", () => {
it("can remove all response headers", async () => {
app.use(
"/",
eventHandler((event) => {
appendHeader(event, "header-1", "1");
appendHeader(event, "header-2", "2");
clearResponseHeaders(event);
})
);
const result = await request.get("/");
expect(result.headers["header-1"]).toBeUndefined();
expect(result.headers["header-2"]).toBeUndefined();
});

it("can remove multiple response headers", async () => {
app.use(
"/",
eventHandler((event) => {
appendHeader(event, "header-3", "3");
appendHeader(event, "header-4", "4");
appendHeader(event, "header-5", "5");
clearResponseHeaders(event, ["header-3", "header-5"]);
})
);
const result = await request.get("/");
expect(result.headers["header-3"]).toBeUndefined();
expect(result.headers["header-4"]).toBe("4");
expect(result.headers["header-5"]).toBeUndefined();
});

it("can remove a single response header", async () => {
app.use(
"/",
eventHandler((event) => {
appendHeader(event, "header-6", "6");
appendHeader(event, "header-7", "7");
removeResponseHeader(event, "header-6");
})
);
const result = await request.get("/");
expect(result.headers["header-6"]).toBeUndefined();
expect(result.headers["header-7"]).toBe("7");
});
});
});

0 comments on commit 29445c4

Please sign in to comment.