From 0e87da711aa1e82336587226ea4b729a1501c22f Mon Sep 17 00:00:00 2001 From: "jason.liao" Date: Fri, 8 Jul 2022 13:30:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8C=87=E5=AE=9A=20response=20?= =?UTF-8?q?=E7=9A=84=E6=8F=90=E5=8F=96=E6=96=B9=E5=BC=8F=E5=88=B0=E5=8F=98?= =?UTF-8?q?=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * json: json 结构, * xml: xml结构包括 html,使用 xpath,如 @username = {{h.response.body.#xml.//*[@id="username"]/@value}} * js: 所有文本类型,如 @role = {{h.response.body.#js.new RegExp('角色:([^< ]+)').exec(body)[1]}} * regex: 纯正则匹配提取,待实现 * hex: 二进制数据提取,待实现 --- src/models/httpVariableResolveResult.ts | 1 + .../requestVariableCacheValueProcessor.ts | 44 +++++++++++++++---- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/models/httpVariableResolveResult.ts b/src/models/httpVariableResolveResult.ts index 33a61c98..c8ac51b6 100644 --- a/src/models/httpVariableResolveResult.ts +++ b/src/models/httpVariableResolveResult.ts @@ -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', } \ No newline at end of file diff --git a/src/utils/requestVariableCacheValueProcessor.ts b/src/utils/requestVariableCacheValueProcessor.ts index 0c1a08e8..8f385512 100644 --- a/src/utils/requestVariableCacheValueProcessor.ts +++ b/src/utils/requestVariableCacheValueProcessor.ts @@ -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 { @@ -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) {