Skip to content

Commit

Permalink
fix: add injection of query parameters in returned response (#24)
Browse files Browse the repository at this point in the history
This PR contains a little feature that will enable a great new option for Mocker system. With this feature it is possible to add the passed query parameters as injected parameter for the returned mock response.

#### List of Changes
 - Add injection of query parameters in mock response
  • Loading branch information
andrea-deri committed Jul 5, 2024
1 parent 6985c7b commit 4cadced
Showing 1 changed file with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public ExtractedResponse extract(ExtractedRequest requestData, MockResourceEntit
try {
UnmarshalledBody unmarshalledBody = extractBodyFields(requestData.getBody(), requestData.getContentType());
MockRuleEntity mockRule = getValidMockRule(mockResource, requestData, unmarshalledBody);
extractedResponse = getMockResponse(mockRule, unmarshalledBody);
extractedResponse = getMockResponse(mockRule, unmarshalledBody, requestData);
} catch (IOException | SAXException | JsonSyntaxException e) {
throw new MockerParseRequestException(e);
}
Expand Down Expand Up @@ -191,18 +191,25 @@ private boolean isContentCompliantToCondition(Object fieldValue, String conditio
}


private ExtractedResponse getMockResponse(MockRuleEntity mockRule, UnmarshalledBody unmarshalledBody) {
private ExtractedResponse getMockResponse(MockRuleEntity mockRule, UnmarshalledBody unmarshalledBody, ExtractedRequest requestData) {
MockResponseEntity mockResponse = mockRule.getResponse();
Map<String, String> dynamicParameters = executeScript(mockRule, unmarshalledBody);
List<String> parameters = mockResponse.getParameters();
String decodedBody = Utility.decodeBase64(mockResponse.getBody());
//
// injecting request body's fields in response
if (parameters != null && unmarshalledBody != null) {
for (String parameterName : parameters) {
String parameterValue = (String) unmarshalledBody.getFieldValue(parameterName);
decodedBody = injectParameterValueInDecodedBody(decodedBody, parameterName, parameterValue);
}
}
// injecting request query parameters in response
if (parameters != null && requestData.getQueryParameters() != null) {
for (String parameterName : parameters) {
String parameterValue = requestData.getQueryParameters().get(parameterName);
decodedBody = injectParameterValueInDecodedBody(decodedBody, parameterName, parameterValue);
}
}
//
for (Map.Entry<String, String> parameter : dynamicParameters.entrySet()) {
decodedBody = injectParameterValueInDecodedBody(decodedBody, "dynamic." + parameter.getKey(), parameter.getValue());
Expand Down

0 comments on commit 4cadced

Please sign in to comment.