use of com.endava.cats.model.CatsRequest in project cats by Endava.
the class TestCaseListenerTest method givenAFunction_whenExecutingATestCaseAndAddingDetails_thenTheDetailsAreCorrectlyAttachedToTheTestCase.
@Test
void givenAFunction_whenExecutingATestCaseAndAddingDetails_thenTheDetailsAreCorrectlyAttachedToTheTestCase() {
CatsTestCase testCase = testCaseListener.testCaseMap.get("Test 1");
Assertions.assertThat(testCase).isNull();
testCaseListener.createAndExecuteTest(logger, fuzzer, () -> {
testCaseListener.addScenario(logger, "Given a {} field", "string");
testCaseListener.addRequest(new CatsRequest());
testCaseListener.addResponse(CatsResponse.builder().build());
testCaseListener.addFullRequestPath("fullPath");
testCaseListener.addPath("path");
testCaseListener.addExpectedResult(logger, "Should return {}", "2XX");
});
testCase = testCaseListener.testCaseMap.get("Test 1");
Assertions.assertThat(testCase).isNotNull();
Assertions.assertThat(testCase.getRequest()).isNotNull();
Assertions.assertThat(testCase.getResponse()).isNotNull();
Assertions.assertThat(testCase.getFullRequestPath()).isEqualTo("fullPath");
Assertions.assertThat(testCase.getPath()).isEqualTo("path");
Assertions.assertThat(testCase.getScenario()).isEqualTo("Given a string field");
Assertions.assertThat(testCase.getExpectedResult()).isEqualTo("Should return 2XX");
}
use of com.endava.cats.model.CatsRequest in project cats by Endava.
the class TestCaseListenerTest method prepareTestCaseListenerSimpleSetup.
private void prepareTestCaseListenerSimpleSetup(CatsResponse build) {
testCaseListener.createAndExecuteTest(logger, fuzzer, () -> {
testCaseListener.addScenario(logger, "Given a {} field", "string");
testCaseListener.addRequest(new CatsRequest());
testCaseListener.addResponse(build);
testCaseListener.addFullRequestPath("fullPath");
testCaseListener.addPath("path");
testCaseListener.addExpectedResult(logger, "Should return {}", "2XX");
});
MDC.put(TestCaseListener.ID, "Test 1");
}
use of com.endava.cats.model.CatsRequest 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.CatsRequest in project cats by Endava.
the class ServiceCaller method call.
/**
* When in dryRun mode ServiceCaller won't do any actual calls.
*
* @param data the current context data
* @return the result of service invocation
*/
@DryRun
public CatsResponse call(ServiceData data) {
LOGGER.note("Proxy configuration to be used: {}", authArguments.getProxy());
rateLimiter.acquire();
String processedPayload = this.replacePayloadWithRefData(data);
List<CatsRequest.Header> headers = this.buildHeaders(data);
CatsRequest catsRequest = new CatsRequest();
catsRequest.setHeaders(headers);
catsRequest.setPayload(processedPayload);
catsRequest.setHttpMethod(data.getHttpMethod().name());
try {
String url = this.getPathWithRefDataReplacedForHttpEntityRequests(data, apiArguments.getServer() + data.getRelativePath());
if (!HttpMethod.requiresBody(data.getHttpMethod())) {
url = this.getPathWithRefDataReplacedForNonHttpEntityRequests(data, apiArguments.getServer() + data.getRelativePath());
url = this.addUriParams(processedPayload, data, url);
}
catsRequest.setUrl(url);
LOGGER.note("Final list of request headers: {}", headers);
LOGGER.note("Final payload: {}", processedPayload);
CatsResponse response = this.callService(catsRequest, data.getFuzzedFields());
this.recordRequestAndResponse(catsRequest, response, data);
return response;
} catch (IOException e) {
this.recordRequestAndResponse(catsRequest, CatsResponse.empty(), data);
throw new CatsIOException(e);
}
}
Aggregations