From ab4ed502f20479c48eacbba91799b63efb14707b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B9=BD=E9=96=92?= <34772477+superfreeeee@users.noreply.github.com> Date: Sat, 16 Dec 2023 12:23:47 +0800 Subject: [PATCH] fix: parseResponseHeaders with empty string return ealier (#7) * fix: parseResponseHeaders with empty string return ealier * test: add helper test for parseResponseHeaders --- src/helper.ts | 5 ++++- test/helper.spec.ts | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 test/helper.spec.ts diff --git a/src/helper.ts b/src/helper.ts index d94cea1..5526b42 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -116,8 +116,11 @@ export const data2QueryString = (data: Record) => { * @returns 响应头对象 */ export const parseResponseHeaders = (headerString: string) => { - const headersAry = headerString.trim().split(/[\r\n]+/); const headersMap = {} as AlovaXHRResponseHeaders; + if (headerString === '') { + return headersMap; + } + const headersAry = headerString.trim().split(/[\r\n]+/); headersAry.forEach(line => { const [headerName, value] = line.split(/:\s*/); headersMap[headerName] = value; diff --git a/test/helper.spec.ts b/test/helper.spec.ts new file mode 100644 index 0000000..c406dc3 --- /dev/null +++ b/test/helper.spec.ts @@ -0,0 +1,14 @@ +import { parseResponseHeaders } from '../src/helper'; + +describe('parseResponseHeaders tests', () => { + test('mvp test', () => { + const header = parseResponseHeaders('x-powered-by: msw'); + expect(Object.keys(header).length).toBe(1); + expect(header['x-powered-by']).toBe('msw'); + }); + + test('empty header', () => { + const header = parseResponseHeaders(''); + expect(Object.keys(header).length).toBe(0); + }); +});