Search in sources :

Example 1 with CatsRequest

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");
}
Also used : CatsTestCase(com.endava.cats.model.report.CatsTestCase) CatsRequest(com.endava.cats.model.CatsRequest) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with CatsRequest

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");
}
Also used : CatsRequest(com.endava.cats.model.CatsRequest)

Example 3 with CatsRequest

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);
}
Also used : CatsResponse(com.endava.cats.model.CatsResponse) Response(okhttp3.Response) X509Certificate(java.security.cert.X509Certificate) ADDITIONAL_PROPERTIES(com.endava.cats.dsl.CatsDSLWords.ADDITIONAL_PROPERTIES) JsonObject(com.google.gson.JsonObject) SSLContext(javax.net.ssl.SSLContext) Arrays(java.util.Arrays) CatsGlobalContext(com.endava.cats.model.CatsGlobalContext) CatsDSLWords(com.endava.cats.dsl.CatsDSLWords) TrustManager(javax.net.ssl.TrustManager) HtmlEscapers(com.google.common.html.HtmlEscapers) CatsResponse(com.endava.cats.model.CatsResponse) PrettyLogger(io.github.ludovicianul.prettylogger.PrettyLogger) StringUtils(org.apache.commons.lang3.StringUtils) SecureRandom(java.security.SecureRandom) GeneralSecurityException(java.security.GeneralSecurityException) FilesArguments(com.endava.cats.args.FilesArguments) Map(java.util.Map) ResponseBody(okhttp3.ResponseBody) CatsUtil(com.endava.cats.util.CatsUtil) Request(okhttp3.Request) Set(java.util.Set) KeyStore(java.security.KeyStore) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) AuthArguments(com.endava.cats.args.AuthArguments) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) List(java.util.List) CatsDSLParser(com.endava.cats.dsl.CatsDSLParser) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) CatsRequest(com.endava.cats.model.CatsRequest) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) HttpUrl(okhttp3.HttpUrl) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) WordUtils(com.endava.cats.util.WordUtils) PrettyLoggerFactory(io.github.ludovicianul.prettylogger.PrettyLoggerFactory) HashMap(java.util.HashMap) RateLimiter(com.google.common.util.concurrent.RateLimiter) Headers(okhttp3.Headers) TreeSet(java.util.TreeSet) RequestBody(okhttp3.RequestBody) ArrayList(java.util.ArrayList) JsonElement(com.google.gson.JsonElement) Inject(javax.inject.Inject) NOT_SET(com.endava.cats.model.util.JsonUtils.NOT_SET) Response(okhttp3.Response) ProcessingArguments(com.endava.cats.args.ProcessingArguments) FuzzingStrategy(com.endava.cats.model.FuzzingStrategy) ApiArguments(com.endava.cats.args.ApiArguments) TestCaseListener(com.endava.cats.report.TestCaseListener) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) JsonUtils(com.endava.cats.model.util.JsonUtils) TimeUnit(java.util.concurrent.TimeUnit) OkHttpClient(okhttp3.OkHttpClient) HttpMethod(com.endava.cats.http.HttpMethod) X509TrustManager(javax.net.ssl.X509TrustManager) DryRun(com.endava.cats.annotations.DryRun) ConnectionPool(okhttp3.ConnectionPool) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) CatsHeader(com.endava.cats.model.CatsHeader) InputStream(java.io.InputStream) Headers(okhttp3.Headers) RequestBody(okhttp3.RequestBody) CatsHeader(com.endava.cats.model.CatsHeader)

Example 4 with CatsRequest

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);
    }
}
Also used : CatsResponse(com.endava.cats.model.CatsResponse) CatsHeader(com.endava.cats.model.CatsHeader) IOException(java.io.IOException) CatsRequest(com.endava.cats.model.CatsRequest) DryRun(com.endava.cats.annotations.DryRun)

Aggregations

CatsRequest (com.endava.cats.model.CatsRequest)4 DryRun (com.endava.cats.annotations.DryRun)2 CatsHeader (com.endava.cats.model.CatsHeader)2 CatsResponse (com.endava.cats.model.CatsResponse)2 IOException (java.io.IOException)2 ApiArguments (com.endava.cats.args.ApiArguments)1 AuthArguments (com.endava.cats.args.AuthArguments)1 FilesArguments (com.endava.cats.args.FilesArguments)1 ProcessingArguments (com.endava.cats.args.ProcessingArguments)1 CatsDSLParser (com.endava.cats.dsl.CatsDSLParser)1 CatsDSLWords (com.endava.cats.dsl.CatsDSLWords)1 ADDITIONAL_PROPERTIES (com.endava.cats.dsl.CatsDSLWords.ADDITIONAL_PROPERTIES)1 HttpMethod (com.endava.cats.http.HttpMethod)1 CatsGlobalContext (com.endava.cats.model.CatsGlobalContext)1 FuzzingStrategy (com.endava.cats.model.FuzzingStrategy)1 CatsTestCase (com.endava.cats.model.report.CatsTestCase)1 JsonUtils (com.endava.cats.model.util.JsonUtils)1 NOT_SET (com.endava.cats.model.util.JsonUtils.NOT_SET)1 TestCaseListener (com.endava.cats.report.TestCaseListener)1 CatsUtil (com.endava.cats.util.CatsUtil)1