CustomCitrusHttpInterceptor

CustomCitrusHttpInterceptor

我想将请求-响应-http附加到柑橘框架的魅力报告中。
我写了这堂课。

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不在诱惑报告中。
在调试中,CustomCitrusHttpInterceptor中没有。
我希望request-response-http会出现在魅力报告中。

最佳答案

HttpClient build = CitrusEndpoints
.http()
.client()
.build();
build.getEndpointConfiguration().setClientInterceptors(Collections.singletonList(new CustomCitrusHttpInterceptor()));


request-response-http位于诱惑报告中。

关于java - HTTP客户端在柑桔框架中的CustomCitrusHttpInterceptor,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56236365/

10-09 22:15