Skip to content

Commit f0f9406

Browse files
committed
refactor: refactor headers declaration
1 parent 4e31172 commit f0f9406

24 files changed

+359
-192
lines changed

README-CN.MD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,10 @@ it('should match object`', async () => {
639639
* @status 200
640640
*
641641
* http响应头信息,支持重复配置
642-
* @header content-type: application/json
642+
* @headers content-type: application/json
643+
*
644+
* 远程mock请求头信息,只有在设置了 @remote 时才有效,允许重复设置
645+
* @remoteRequestHeaders content-type: application/json
643646
*
644647
* http响应延迟,模拟网络延时,单位毫秒,默认0
645648
* @delay 100

README.MD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ it('should match object`', async () => {
651651
*
652652
* Request headers, request headers, only available for @remote tag
653653
* It can be set repeatedly.
654-
* @requestHeaders content-type: application/json
654+
* @remoteRequestHeaders content-type: application/json
655655
*
656656
* Simulate network latency in milliseconds.
657657
* Default: 0

package-lock.json

Lines changed: 31 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "http-request-mock",
3-
"version": "1.8.28",
3+
"version": "2.0.0",
44
"description": "Intercept & mock http requests issued by XMLHttpRequest, fetch, nodejs https/http module, axios, jquery, superagent, ky, node-fetch, request, got or any other request libraries by intercepting XMLHttpRequest, fetch and nodejs native requests in low level.",
55
"main": "src/index.js",
66
"module": "http-request-mock.esm.mjs",

src/browser.pure.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import InterceptorWxRequest from './interceptor/wx-request';
33
import InterceptorXhr from './interceptor/xml-http-request';
44
import Mocker from './mocker/mocker';
55

6-
export type { HttpVerb, Method, MockItemExt, MockItemInfo, RequestInfo } from './types';
6+
export type { HttpVerb, MockItemExt, MockItemInfo, RequestInfo } from './types';
77
export { Mocker };
88

99
export default class BrowserPureIndex {

src/browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import faker from '../tool/plugin/faker.js';
33
import BrowserPureIndex from './browser.pure';
44
import Mocker from './mocker/mocker';
55

6-
export type { HttpVerb, Method, MockItemExt, MockItemInfo, RequestInfo } from './types';
6+
export type { HttpVerb, MockItemExt, MockItemInfo, RequestInfo } from './types';
77
export { Mocker };
88

99
/**

src/common/request.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { IncomingMessage } from 'http';
33
import Stream from 'stream';
44
import { URL } from 'url';
55
import * as zlib from 'zlib';
6-
import { AnyObject, OriginalResponse } from './../types';
6+
import type { AnyObject, Headers, OriginalResponse } from '../types';
77
import { str2arrayBuffer, tryToParseJson } from './utils';
88

99
/**
@@ -22,7 +22,7 @@ import { str2arrayBuffer, tryToParseJson } from './utils';
2222
export default function request(requestConfig: {
2323
url: string | URL,
2424
method: string,
25-
headers?: Record<string, string>,
25+
headers?: Headers,
2626
body?: unknown,
2727
opts?: Record<string, string>
2828
}): Promise<{body: string, json: AnyObject, response: IncomingMessage}> {

src/common/utils.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import { Query } from '../types';
2+
import type { Query } from '../types';
33

44
/**
55
* Get query parameters from the specified request url.
@@ -87,6 +87,25 @@ export function tryToParseObject(body: unknown) {
8787
}
8888
}
8989

90+
export function tryToParsePostBody(body: unknown): unknown {
91+
if (!body) {
92+
return body;
93+
}
94+
95+
if (typeof body === 'string') {
96+
const info = tryToParseObject(body);
97+
if (info && typeof info === 'object') {
98+
return info;
99+
}
100+
}
101+
102+
if (typeof body === 'string' && body.includes('&') && body.includes('=')) {
103+
return getQuery(body);
104+
}
105+
106+
return body;
107+
}
108+
90109
/**
91110
* Try to parse a JSON string
92111
* @param {unknown} body
@@ -224,9 +243,11 @@ export function isImported(obj: unknown) {
224243
* Get caller file from error stack
225244
*/
226245
export function getCallerFile() {
246+
type SimplifiedStackInfo = { getFileName: () => string };
247+
227248
const oldPrepareStackTrace = Error.prepareStackTrace;
228249
Error.prepareStackTrace = (_, stack) => stack;
229-
const stack = new Error().stack as unknown as Record<string, { getFileName: () => string }>;
250+
const stack = new Error().stack as unknown as Record<string, SimplifiedStackInfo>;
230251
Error.prepareStackTrace = oldPrepareStackTrace;
231252

232253

@@ -240,3 +261,22 @@ export function getCallerFile() {
240261
}
241262
}
242263
}
264+
265+
export function get<T>(obj: object, path: string | Array<string | number>, defaultValue?: unknown): T {
266+
if (typeof path === 'string') {
267+
path = path.replace(/\[(\w+)\]/g, '.$1');
268+
path = path.split('.').filter(Boolean);
269+
}
270+
271+
let result: unknown = obj;
272+
for (const key of path as Array<string | number>) {
273+
if (result && result[key as keyof typeof result] !== undefined) {
274+
result = result[key as keyof typeof result];
275+
} else {
276+
result = undefined;
277+
break;
278+
}
279+
}
280+
281+
return (result === undefined ? defaultValue : result) as T;
282+
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Dummy from './dummy';
44
import NodeHttpAndHttps from './interceptor/node/http-and-https';
55
import Mocker from './mocker/mocker-for-node';
66

7-
export type { HttpVerb, Method, MockItemExt, MockItemInfo, RequestInfo } from './types';
7+
export type { HttpVerb, MockItemExt, MockItemInfo, RequestInfo } from './types';
88
export { Mocker };
99

1010
export default class Index extends Browser{

src/interceptor/base.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getQuery, tryToParseObject } from '../common/utils';
1+
import { get, getQuery, tryToParsePostBody } from '../common/utils';
22
import MockItem from '../mocker/mock-item';
33
import Mocker from '../mocker/mocker';
44
import { HttpVerb, RequestInfo } from '../types';
@@ -73,11 +73,12 @@ export default class BaseInterceptor {
7373
method: requestInfo.method || 'GET',
7474
query: getQuery(requestInfo.url),
7575
};
76-
if (requestInfo.headers || requestInfo.header) {
77-
info.headers = requestInfo.headers || requestInfo.header;
76+
if (get(requestInfo, 'headers') || get(requestInfo, 'header')) {
77+
info.headers = get(requestInfo, 'headers') || get(requestInfo, 'header');
7878
}
7979
if (requestInfo.body !== undefined) {
80-
info.body = tryToParseObject(requestInfo.body);
80+
info.rawBody = requestInfo.body;
81+
info.body = tryToParsePostBody(requestInfo.body);
8182
}
8283
return info;
8384
}

0 commit comments

Comments
 (0)