generateResponseMatch method
Implementation
Future<(List<ResponseRedaction>, List<ResponseMatch>)> generateResponseMatch(
String response,
List<ResponseSelection> responseSelections,
Map<String, String> params) async {
List<ResponseMatch> responseMatches = [];
List<ResponseRedaction> responseRedactions = [];
await Future.forEach(responseSelections, (responseSelection) async {
String element = response;
if (responseSelection.xPath.isNotEmpty) {
element = await _witnessWebview.extractHtmlElement(
element, responseSelection.xPath, false);
}
if (responseSelection.jsonPath.isNotEmpty) {
ExtractJSONValueIndexOutput bounds = await _witnessWebview
.extractJSONValueIndex(element, responseSelection.jsonPath);
element = element.substring(bounds.start, bounds.end);
}
var (responseMatchRegex, _, responseMatchParamKeys) =
convertTemplateToRegex(
template: responseSelection.responseMatch,
parameters: widget.parameters,
matchTypeOverride: responseSelection.matchType);
List<String?> responseSelectionParamValue = RegExp(responseMatchRegex)
.firstMatch(element)!
.groups(
List<int>.generate(responseMatchParamKeys.length, (i) => i + 1))
.toList();
responseMatchParamKeys.asMap().forEach((key, value) {
params[value] = responseSelectionParamValue[key]!;
});
responseRedactions.add(ResponseRedaction(
xPath: responseSelection.xPath,
jsonPath: responseSelection.jsonPath,
regex: responseMatchRegex));
responseMatches.add(ResponseMatch(
value: responseSelection.responseMatch, type: "contains"));
});
return (responseRedactions, responseMatches);
}