use of com.endava.cats.model.CatsResponse in project cats by Endava.
the class ExtraHeaderFuzzerTest method givenASetOfHeaders_whenCallingTheExtraHeadersFuzzer_thenTheResultsAreCorrectlyReported.
@Test
void givenASetOfHeaders_whenCallingTheExtraHeadersFuzzer_thenTheResultsAreCorrectlyReported() {
Map<String, List<String>> responses = new HashMap<>();
responses.put("200", Collections.singletonList("response"));
FuzzingData data = FuzzingData.builder().headers(Collections.singleton(CatsHeader.builder().name("header").value("value").build())).responses(responses).reqSchema(new StringSchema()).build();
CatsResponse catsResponse = CatsResponse.builder().body("{}").responseCode(200).build();
Mockito.when(serviceCaller.call(Mockito.any())).thenReturn(catsResponse);
Mockito.doNothing().when(testCaseListener).reportResult(Mockito.any(), Mockito.eq(data), Mockito.any(), Mockito.any());
extraHeaderFuzzer.fuzz(data);
Mockito.verify(testCaseListener, Mockito.times(1)).reportResult(Mockito.any(), Mockito.eq(data), Mockito.eq(catsResponse), Mockito.eq(ResponseCodeFamily.TWOXX));
}
use of com.endava.cats.model.CatsResponse in project cats by Endava.
the class ServiceCaller method callService.
public CatsResponse callService(CatsRequest catsRequest, Set<String> fuzzedFields) throws IOException {
long startTime = System.currentTimeMillis();
RequestBody requestBody = null;
Headers.Builder headers = new Headers.Builder();
catsRequest.getHeaders().forEach(header -> headers.addUnsafeNonAscii(header.getName(), header.getValue()));
if (HttpMethod.requiresBody(catsRequest.getHttpMethod())) {
requestBody = RequestBody.create(catsRequest.getPayload().getBytes(StandardCharsets.UTF_8));
}
Response response = okHttpClient.newCall(new Request.Builder().url(catsRequest.getUrl()).headers(headers.build()).method(catsRequest.getHttpMethod(), requestBody).build()).execute();
long endTime = System.currentTimeMillis();
LOGGER.complete("Protocol: {}, Method: {}, ReasonPhrase: {}, ResponseCode: {}, ResponseTimeInMs: {}", response.protocol(), catsRequest.getHttpMethod(), response.message(), response.code(), endTime - startTime);
String responseBody = this.getAsJson(response);
List<CatsHeader> responseHeaders = response.headers().toMultimap().entrySet().stream().map(header -> CatsHeader.builder().name(header.getKey()).value(header.getValue().get(0)).build()).collect(Collectors.toList());
return CatsResponse.from(response.code(), responseBody, catsRequest.getHttpMethod(), endTime - startTime, responseHeaders, fuzzedFields);
}
use of com.endava.cats.model.CatsResponse in project cats by Endava.
the class CustomFuzzerUtil method process.
public void process(FuzzingData data, String testName, Map<String, String> currentPathValues) {
String expectedResponseCode = currentPathValues.get(CatsDSLWords.EXPECTED_RESPONSE_CODE);
this.startCustomTest(testName, currentPathValues, expectedResponseCode);
String payloadWithCustomValuesReplaced = this.getJsonWithCustomValuesFromFile(data, currentPathValues);
catsUtil.setAdditionalPropertiesToPayload(currentPathValues, payloadWithCustomValuesReplaced);
String servicePath = this.replacePathVariablesWithCustomValues(data, currentPathValues);
CatsResponse response = serviceCaller.call(ServiceData.builder().relativePath(servicePath).replaceRefData(false).httpMethod(data.getMethod()).headers(data.getHeaders()).payload(payloadWithCustomValuesReplaced).queryParams(data.getQueryParams()).build());
this.setOutputVariables(currentPathValues, response, payloadWithCustomValuesReplaced);
String verify = currentPathValues.get(CatsDSLWords.VERIFY);
if (verify != null) {
this.checkVerifiesAndReport(payloadWithCustomValuesReplaced, response, verify, expectedResponseCode);
} else {
testCaseListener.reportResult(log, data, response, ResponseCodeFamily.from(expectedResponseCode));
}
}
use of com.endava.cats.model.CatsResponse in project cats by Endava.
the class CustomFuzzerUtil method checkVerifiesAndReport.
private void checkVerifiesAndReport(String request, CatsResponse response, String verify, String expectedResponseCode) {
Map<String, String> verifies = this.parseYmlEntryIntoMap(verify);
Map<String, String> responseValues = this.matchVariablesWithTheResponse(response, verifies, Map.Entry::getKey);
log.info("Parameters to verify: {}", verifies);
log.info("Parameters matched to response: {}", responseValues);
if (responseValues.entrySet().stream().anyMatch(entry -> entry.getValue().equalsIgnoreCase(NOT_SET))) {
log.error("There are Verify parameters which were not present in the response!");
testCaseListener.reportError(log, "The following Verify parameters were not present in the response: {}", responseValues.entrySet().stream().filter(entry -> entry.getValue().equalsIgnoreCase(NOT_SET)).map(Map.Entry::getKey).collect(Collectors.toList()));
} else {
StringBuilder errorMessages = new StringBuilder();
verifies.forEach((key, value) -> {
String valueToCheck = responseValues.get(key);
String parsedVerifyValue = this.getVerifyValue(request, response, value);
Matcher verifyMatcher = Pattern.compile(parsedVerifyValue).matcher(valueToCheck);
if (!verifyMatcher.matches()) {
errorMessages.append(String.format(NOT_MATCHING_ERROR, key, valueToCheck, parsedVerifyValue));
}
});
if (errorMessages.length() == 0 && expectedResponseCode.equalsIgnoreCase(response.responseCodeAsString())) {
testCaseListener.reportInfo(log, "Response matches all 'verify' parameters");
} else if (errorMessages.length() == 0) {
testCaseListener.reportWarn(log, "Response matches all 'verify' parameters, but response code doesn't match expected response code: expected [{}], actual [{}]", expectedResponseCode, response.responseCodeAsString());
} else {
testCaseListener.reportError(log, errorMessages.toString());
}
}
}
use of com.endava.cats.model.CatsResponse in project cats by Endava.
the class HappyFuzzer method process.
private void process(FuzzingData data) {
testCaseListener.addScenario(LOGGER, "Send a 'happy' flow request with all fields and all headers in: {}", data.getMethod());
testCaseListener.addExpectedResult(LOGGER, "Should get a 2XX response code");
CatsResponse response = serviceCaller.call(ServiceData.builder().relativePath(data.getPath()).headers(data.getHeaders()).payload(data.getPayload()).queryParams(data.getQueryParams()).httpMethod(data.getMethod()).build());
testCaseListener.reportResult(LOGGER, data, response, ResponseCodeFamily.TWOXX);
}
Aggregations