diff --git a/packages/happy-dom/src/cookie/urilities/CookieStringUtility.ts b/packages/happy-dom/src/cookie/urilities/CookieStringUtility.ts index afb94052..11409bfc 100644 --- a/packages/happy-dom/src/cookie/urilities/CookieStringUtility.ts +++ b/packages/happy-dom/src/cookie/urilities/CookieStringUtility.ts @@ -15,12 +15,15 @@ export default class CookieStringUtility { */ public static stringToCookie(originURL: URL, cookieString: string): ICookie | null { const parts = cookieString.split(';'); - const [key, ...value] = parts.shift().split('='); + const part = parts.shift(); + const index = part.indexOf('='); + const key = index !== -1 ? part.slice(0, index).trim() : part.trim(); + const value = index !== -1 ? part.slice(index + 1).trim() : null; const cookie: ICookie = { // Required - key: key.trim(), - value: value.length ? value.join('=') : null, + key, + value, originURL, // Optional @@ -38,9 +41,10 @@ export default class CookieStringUtility { } for (const part of parts) { - const [_key, ..._value] = part.split('='); - const key = _key.trim().toLowerCase(); - const value = _value.join('=').trim(); + const index = part.indexOf('='); + const key = + index !== -1 ? part.slice(0, index).trim().toLowerCase() : part.trim().toLowerCase(); + const value = index !== -1 ? part.slice(index + 1).trim() : ''; switch (key) { case 'expires': diff --git a/packages/happy-dom/test/cookie/CookieContainer.test.ts b/packages/happy-dom/test/cookie/CookieContainer.test.ts index 192a2981..bc65437c 100644 --- a/packages/happy-dom/test/cookie/CookieContainer.test.ts +++ b/packages/happy-dom/test/cookie/CookieContainer.test.ts @@ -29,7 +29,9 @@ describe('CookieContainer', () => { `key1=value1; Expires=${new Date(expires).toString()};` ) ), - CookieStringUtility.stringToCookie(originURL, `key2=value2; Max-Age=${maxAge};`), + ( + CookieStringUtility.stringToCookie(originURL, `key2 = value2 ; Max-Age=${maxAge};`) + ), CookieStringUtility.stringToCookie(originURL, `key3=value3; Domain=example.com;`), CookieStringUtility.stringToCookie(originURL, `key4=value4; Domain=other.com;`), (