Skip to content

Commit

Permalink
feat: json decode for case detail
Browse files Browse the repository at this point in the history
  • Loading branch information
QizhengMo committed Dec 6, 2023
1 parent ceeaba8 commit c5902fa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public static List<CaseDetailMocker> convertMocker(List<AREXMocker> mockers) {
if (req != null) {
CaseDetailMocker.CaseDetailTarget caseDetailReq = new CaseDetailMocker.CaseDetailTarget();
BeanUtils.copyProperties(req, caseDetailReq);
caseDetailReq.setBody(JsonUtils.tryParseJson(req.getBody()));
caseDetailReq.setBody(JsonUtils.tryParseBase64Json(req.getBody()));
caseDetailMocker.setTargetRequest(caseDetailReq);
}

Target res = mocker.getTargetResponse();
if (res != null) {
CaseDetailMocker.CaseDetailTarget caseDetailRes = new CaseDetailMocker.CaseDetailTarget();
BeanUtils.copyProperties(res, caseDetailRes);
caseDetailRes.setBody(JsonUtils.tryParseJson(res.getBody()));
caseDetailRes.setBody(JsonUtils.tryParseBase64Json(res.getBody()));
caseDetailMocker.setTargetResponse(caseDetailRes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.arextest.web.model.dto.CompareResultDto;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Base64;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -44,6 +45,19 @@ public static Object tryParseJson(String jsonStr) {
}
}

public static Object tryParseBase64Json(String base64Str) {
try {
if (StringUtils.isBlank(base64Str)) {
return base64Str;
}
byte[] bytes = Base64.getDecoder().decode(base64Str);
return COMMON_MAPPER.readTree(bytes);
} catch (Exception e) {
// failed quite often, so don't log it
return tryParseJson(base64Str);
}
}

public static boolean isJsonStr(String obj) {
if (StringUtils.isBlank(obj)) {
return false;
Expand Down

0 comments on commit c5902fa

Please sign in to comment.