Skip to content

Commit

Permalink
feat: [capricorn86#1315] Fixed handling of empty characters in headers
Browse files Browse the repository at this point in the history
  • Loading branch information
ushiboy committed Mar 17, 2024
1 parent 56ab217 commit f4f7818
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/happy-dom/src/fetch/Headers.ts
Expand Up @@ -74,7 +74,7 @@ export default class Headers implements IHeaders {
* @returns Value.
*/
public get(name: string): string | null {
return this[PropertySymbol.entries][name.toLowerCase()]?.value || null;
return this[PropertySymbol.entries][name.toLowerCase()]?.value ?? null;
}

/**
Expand All @@ -96,7 +96,13 @@ export default class Headers implements IHeaders {
* @returns An array of strings representing the values of all the different Set-Cookie headers.
*/
public getSetCookie(): string[] {
return CookieStringUtility.splitCookiesString(this.get('Set-Cookie') || '');
const cookiesString = this.get('Set-Cookie');
if (cookiesString === null) {
return [];
} else if (cookiesString === '') {
return [''];
}
return CookieStringUtility.splitCookiesString(cookiesString);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions packages/happy-dom/test/fetch/Headers.test.ts
Expand Up @@ -76,6 +76,14 @@ describe('Headers', () => {

expect(headers.get('Content-Type')).toBe('application/json, x-www-form-urlencoded');
});

it('Returns the value of Header as it is, set with an empty string.', () => {
const headers = new Headers();

headers.append('X-A', '');

expect(headers.get('X-A')).toBe('');
});
});

describe('set()', () => {
Expand Down Expand Up @@ -106,6 +114,13 @@ describe('Headers', () => {
expect(headers.getSetCookie()).toEqual([]);
});

it('Returns as a list of empty characters if the Set-Cookie header is set to an empty string.', () => {
const headers = new Headers();
headers.append('Set-Cookie', '');

expect(headers.getSetCookie()).toEqual(['']);
});

it('Returns an array of strings representing the values of all the different Set-Cookie headers.', () => {
const headers = new Headers();

Expand Down

0 comments on commit f4f7818

Please sign in to comment.