我想将一个request-response-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"));
}
}));
}
}运行测试后,请求-响应-http不在诱惑报告中。在debug中,CustomCitrusHttpInterceptor不会出现。我希望请求-响应-http会出现在这个诱人的报告中。
发布于 2019-05-22 15:07:56
HttpClient build = CitrusEndpoints
.http()
.client()
.build();
build.getEndpointConfiguration().setClientInterceptors(Collections.singletonList(new CustomCitrusHttpInterceptor()));request-response-http在诱饵报告中。
https://stackoverflow.com/questions/56236365
复制相似问题