Skip to content

Commit

Permalink
fix: parseResponseHeaders with empty string return ealier (#7)
Browse files Browse the repository at this point in the history
* fix: parseResponseHeaders with empty string return ealier

* test: add helper test for parseResponseHeaders
  • Loading branch information
superfreeeee authored Dec 16, 2023
1 parent e6bc59e commit ab4ed50
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,11 @@ export const data2QueryString = (data: Record<string, any>) => {
* @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;
Expand Down
14 changes: 14 additions & 0 deletions test/helper.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit ab4ed50

Please sign in to comment.