首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过http-client在citrus框架中实现CustomCitrusHttpInterceptor

通过http-client在citrus框架中实现CustomCitrusHttpInterceptor
EN

Stack Overflow用户
提问于 2019-05-21 18:30:47
回答 1查看 139关注 0票数 0

我想将一个request-response-http附加到来自citrus-framework的诱惑报告中。我写了这个类。

代码语言:javascript
复制
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;
    }
}

和测试

代码语言:javascript
复制
@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"));
                    }
                }));
    }
}

运行测试后,请求-响应-http不在诱惑报告中。在debug中,CustomCitrusHttpInterceptor不会出现。我希望请求-响应-http会出现在这个诱人的报告中。

EN

回答 1

Stack Overflow用户

发布于 2019-05-22 15:07:56

代码语言:javascript
复制
HttpClient build = CitrusEndpoints
.http()
.client()
.build();
build.getEndpointConfiguration().setClientInterceptors(Collections.singletonList(new CustomCitrusHttpInterceptor()));

request-response-http在诱饵报告中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56236365

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档