CustomCitrusHttpInterceptor в цитрусовых фреймворках по http-клиенту - PullRequest
0 голосов

Я хочу прикрепить запрос-ответ-http к отчету о привлекательности из citrus-framework. Я написал этот класс.

package com.cdek.qa_auto.common;

import io.qameta.allure.attachment.DefaultAttachmentProcessor;
import io.qameta.allure.attachment.FreemarkerAttachmentRenderer;
import io.qameta.allure.attachment.http.HttpRequestAttachment;
import io.qameta.allure.attachment.http.HttpResponseAttachment;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.FileCopyUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

public class CustomCitrusHttpInterceptor implements ClientHttpRequestInterceptor {

    private String requestTemplatePath = "http-request.ftl";
    private String responseTemplatePath = "http-response.ftl";

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body,
                                        ClientHttpRequestExecution execution) throws IOException {
        handleRequest(getRequestContent(request, new String(body)));

        ClientHttpResponse response = execution.execute(request, body);
        CachingClientHttpResponseWrapper bufferedResponse = new CachingClientHttpResponseWrapper(response);
        handleResponse(getResponseContent(bufferedResponse));

        return bufferedResponse;
    }
}

и тест

@SpringBootTest
@ExtendWith(CitrusExtension.class)
public class CitrusLogTest {

    @Test
    @com.consol.citrus.annotations.CitrusTest
    void testPost1(@CitrusResource TestRunner runner) {
        HttpClient todoClient = CitrusEndpoints
                .http()
                .client()
                .interceptor(new CustomCitrusHttpInterceptor())
                .requestUrl("http://address")
                .build();
        runner.http(action -> action
            .client(todoClient)
            .send()
            .post("/api/tokenauth/authorize")
            .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
            .payload("{  \n" +
                    "   \"user\":\"User\",\n" +
                    "   \"hashedPass\":\"hashedPass\"\n" +
                    "}"));

        runner.http(action -> action
                .client(todoClient)
                .receive()
                .response(HttpStatus.OK)
                .messageType(MessageType.JSON)
                .validationCallback(new AbstractValidationCallback<String>() {
                    @Override
                    public void validate(String payload, Map<String, Object> headers, TestContext context) {
                        assertTrue(payload.contains("token"));
                    }
                }));
    }
}

После запуска теста request-response-http не было в allure-report. В отладке, в CustomCitrusHttpInterceptor не заходит. Я ожидаю, что запрос-ответ-http будет в отчете очарования.

1 Ответ

0 голосов
HttpClient build = CitrusEndpoints
.http()
.client()
.build();
build.getEndpointConfiguration().setClientInterceptors(Collections.singletonList(new CustomCitrusHttpInterceptor()));

Запрос-ответ-http был в отчете очарования.

...