gpt4 book ai didi

java - http-client 在 citrus-framework 中的 CustomCitrusHttpInterceptor

转载 作者:行者123 更新时间:2023-12-02 05:25:25 26 4
gpt4 key购买 nike

我想将请求-响应-http 附加到来自 citrus-framework 的 allure-report。我写了这门课。

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中不来。我希望 request-response-http 出现在 allure-report 中。

最佳答案

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

请求-响应-http 位于 allure-report 中。

关于java - http-client 在 citrus-framework 中的 CustomCitrusHttpInterceptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56236365/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com