use of com.endava.cats.model.FuzzingData in project cats by Endava.
the class TestCaseListenerTest method shouldReportInfoWhenResponseCode200IsExpectedAndResponseBodyIsEmpty.
@ParameterizedTest
@CsvSource({ "{}", "[]", "''", "' '" })
void shouldReportInfoWhenResponseCode200IsExpectedAndResponseBodyIsEmpty(String body) {
FuzzingData data = Mockito.mock(FuzzingData.class);
CatsResponse response = Mockito.mock(CatsResponse.class);
TestCaseListener spyListener = Mockito.spy(testCaseListener);
Mockito.when(response.getBody()).thenReturn(body);
Mockito.when(data.getResponseCodes()).thenReturn(Sets.newHashSet("200", "400"));
Mockito.when(data.getResponses()).thenReturn(Collections.emptyMap());
Mockito.when(response.responseCodeAsString()).thenReturn("400");
spyListener.createAndExecuteTest(logger, fuzzer, () -> spyListener.reportResult(logger, data, response, ResponseCodeFamily.FOURXX));
Mockito.verify(executionStatisticsListener, Mockito.times(1)).increaseSuccess();
Mockito.verify(spyListener, Mockito.times(1)).reportInfo(logger, "Response matches expected result. Response code [{}] is documented and response body matches the corresponding schema.", response.responseCodeAsString());
}
use of com.endava.cats.model.FuzzingData 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.FuzzingData in project cats by Endava.
the class RemoveHeadersFuzzerTest method givenASetOfHeaders_whenAnErrorOccursCallingTheService_thenTheErrorIsProperlyReported.
@Test
void givenASetOfHeaders_whenAnErrorOccursCallingTheService_thenTheErrorIsProperlyReported() {
FuzzingData data = FuzzingData.builder().headers(Collections.singleton(CatsHeader.builder().name("header").value("value").build())).build();
Mockito.when(serviceCaller.call(Mockito.any())).thenThrow(new RuntimeException());
Mockito.doNothing().when(testCaseListener).reportResult(Mockito.any(), Mockito.eq(data), Mockito.any(), Mockito.any());
removeHeadersFuzzer.fuzz(data);
Mockito.verify(testCaseListener, Mockito.times(2)).reportError(Mockito.any(), Mockito.anyString(), Mockito.any());
}
use of com.endava.cats.model.FuzzingData in project cats by Endava.
the class FuzzingDataFactory method getFuzzDataForNonBodyMethods.
/**
* A similar FuzzingData object will be created for GET or DELETE requests. The "payload" will be a JSON with all the query or path params.
* In order to achieve this a synthetic object is created that will act as a root object holding all the query or path params as child schemas.
* The method returns a list of FuzzingData as you might have oneOf operations which will create multiple payloads.
*
* @param path the current path
* @param item the current path item
* @param openAPI the full OpenAPI object
* @param operation the OpenApi operation
* @return a list of FuzzingData objects
*/
private List<FuzzingData> getFuzzDataForNonBodyMethods(String path, PathItem item, Operation operation, OpenAPI openAPI, HttpMethod method) {
ObjectSchema syntheticSchema = this.createSyntheticSchemaForGet(operation.getParameters());
globalContext.getSchemaMap().put(SYNTH_SCHEMA_NAME + operation.getOperationId(), syntheticSchema);
Set<String> queryParams = this.extractQueryParams(syntheticSchema);
List<String> payloadSamples = this.getRequestPayloadsSamples(null, SYNTH_SCHEMA_NAME + operation.getOperationId());
Map<String, List<String>> responses = this.getResponsePayloads(operation, operation.getResponses().keySet());
Map<String, List<String>> responsesContentTypes = this.getResponseContentTypes(operation, operation.getResponses().keySet());
List<String> requestContentTypes = this.getRequestContentTypes(operation, openAPI);
return payloadSamples.stream().map(payload -> FuzzingData.builder().method(method).path(path).headers(this.extractHeaders(operation)).payload(payload).responseCodes(operation.getResponses().keySet()).reqSchema(syntheticSchema).pathItem(item).schemaMap(globalContext.getSchemaMap()).responses(responses).responseContentTypes(responsesContentTypes).requestPropertyTypes(globalContext.getRequestDataTypes()).requestContentTypes(requestContentTypes).queryParams(queryParams).openApi(openAPI).tags(operation.getTags()).reqSchemaName(SYNTH_SCHEMA_NAME).build()).collect(Collectors.toList());
}
use of com.endava.cats.model.FuzzingData in project cats by Endava.
the class FuzzingDataFactory method getFuzzDataForHttpMethod.
/**
* The reason we get more than one {@code FuzzingData} objects is related to the usage of {@code anyOf, oneOf or allOf} elements inside the contract definition.
* The method will compute all the possible combinations so that it covers all payload definitions.
*
* @param path the current path
* @param item the current PathItem
* @param operation the current OpenAPI Operation
* @param method the current HTTP method
* @return a list of FuzzingData used to Fuzz
*/
private List<FuzzingData> getFuzzDataForHttpMethod(String path, PathItem item, Operation operation, HttpMethod method, OpenAPI openAPI) {
List<FuzzingData> fuzzingDataList = new ArrayList<>();
MediaType mediaType = this.getMediaType(operation, openAPI);
if (mediaType == null) {
return Collections.emptyList();
}
List<String> reqSchemaNames = this.getCurrentRequestSchemaName(mediaType);
Map<String, List<String>> responses = this.getResponsePayloads(operation, operation.getResponses().keySet());
Map<String, List<String>> responsesContentTypes = this.getResponseContentTypes(operation, operation.getResponses().keySet());
List<String> requestContentTypes = this.getRequestContentTypes(operation, openAPI);
for (String reqSchemaName : reqSchemaNames) {
List<String> payloadSamples = this.getRequestPayloadsSamples(mediaType, reqSchemaName);
fuzzingDataList.addAll(payloadSamples.stream().map(payload -> FuzzingData.builder().method(method).path(path).headers(this.extractHeaders(operation)).payload(payload).responseCodes(operation.getResponses().keySet()).reqSchema(globalContext.getSchemaMap().get(reqSchemaName)).pathItem(item).responseContentTypes(responsesContentTypes).requestContentTypes(requestContentTypes).schemaMap(globalContext.getSchemaMap()).responses(responses).requestPropertyTypes(globalContext.getRequestDataTypes()).openApi(openAPI).tags(operation.getTags()).reqSchemaName(reqSchemaName).build()).collect(Collectors.toList()));
}
return fuzzingDataList;
}
Aggregations