Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 指定 response 的提取方式到变量 #1028

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/models/httpVariableResolveResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ export const enum ResolveWarningMessage {
UnsupportedBodyContentType = 'Only JSON and XML response/request body is supported to query the result',
InvalidJSONPath = 'Invalid JSONPath query',
InvalidXPath = 'Invalid XPath query',
InvalidScript = 'Invalid Script',
}
44 changes: 35 additions & 9 deletions src/utils/requestVariableCacheValueProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const requestVariablePathRegex: RegExp = /^(\w+)(?:\.(request|response)(?:\.(bod

type HttpEntity = 'request' | 'response';
type HttpPart = 'headers' | 'body';
type BodyType = 'xml' | 'json' | 'js' | 'regex' | 'hex' | undefined;

export class RequestVariableCacheValueProcessor {
public static resolveRequestVariable(value: HttpResponse | undefined, path: string): ResolveResult {
Expand Down Expand Up @@ -57,17 +58,42 @@ export class RequestVariableCacheValueProcessor {
return { state: ResolveState.Success, value: body };
}

const contentTypeHeader = getContentType(headers);
if (MimeUtility.isJSON(contentTypeHeader) || (MimeUtility.isJavaScript(contentTypeHeader) && isJSONString(body as string))) {
const parsedBody = JSON.parse(body as string);

return this.resolveJsonHttpBody(parsedBody, nameOrPath);
} else if (MimeUtility.isXml(contentTypeHeader)) {
return this.resolveXmlHttpBody(body, nameOrPath);
let bodyType: BodyType = undefined;
const bodyType1 = /^#(json|xml|js|regex|hex)\./.exec(nameOrPath);
if (bodyType1) {
bodyType = bodyType1[1] as BodyType;
nameOrPath = nameOrPath.substring(bodyType1[0].length);
} else {
return { state: ResolveState.Warning, value: body, message: ResolveWarningMessage.UnsupportedBodyContentType };
const contentTypeHeader = getContentType(headers);
if (MimeUtility.isJSON(contentTypeHeader) || (MimeUtility.isJavaScript(contentTypeHeader) && isJSONString(body as string))) {
bodyType = "json";
} else if (MimeUtility.isXml(contentTypeHeader)) {
bodyType = "xml";
}
}

switch (bodyType) {
case "json":
const parsedBody = JSON.parse(body as string);
return this.resolveJsonHttpBody(parsedBody, nameOrPath);
case "xml":
return this.resolveXmlHttpBody(body, nameOrPath);
case "js":
try {
const msg = eval('(body) => ' + nameOrPath)(body) as string;
return {state : ResolveState.Success, value: msg};
} catch {
return { state: ResolveState.Warning, message: ResolveWarningMessage.InvalidScript };
}
case "regex":
// TODO: 待实现
break
case "hex":
// TODO: 待实现
break
default:
break
}
return { state: ResolveState.Warning, value: body, message: ResolveWarningMessage.UnsupportedBodyContentType };
} else {
const { headers } = http;
if (!nameOrPath) {
Expand Down