Skip to content

Commit 343d0b8

Browse files
authored
make AllureRestTemplate more configurable (via #873)
1 parent bf0eec7 commit 343d0b8

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

allure-spring-web/src/main/java/io/qameta/allure/springweb/AllureRestTemplate.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.qameta.allure.attachment.AttachmentData;
1919
import io.qameta.allure.attachment.AttachmentProcessor;
20+
import io.qameta.allure.attachment.AttachmentRenderer;
2021
import io.qameta.allure.attachment.DefaultAttachmentProcessor;
2122
import io.qameta.allure.attachment.FreemarkerAttachmentRenderer;
2223
import io.qameta.allure.attachment.http.HttpRequestAttachment;
@@ -42,6 +43,14 @@ public class AllureRestTemplate implements ClientHttpRequestInterceptor {
4243
private String requestTemplatePath = "http-request.ftl";
4344
private String responseTemplatePath = "http-response.ftl";
4445

46+
public String getRequestTemplatePath() {
47+
return requestTemplatePath;
48+
}
49+
50+
public String getResponseTemplatePath() {
51+
return responseTemplatePath;
52+
}
53+
4554
public AllureRestTemplate setRequestTemplate(final String templatePath) {
4655
this.requestTemplatePath = templatePath;
4756
return this;
@@ -52,11 +61,23 @@ public AllureRestTemplate setResponseTemplate(final String templatePath) {
5261
return this;
5362
}
5463

64+
protected AttachmentRenderer<AttachmentData> getRequestRenderer() {
65+
return new FreemarkerAttachmentRenderer(getRequestTemplatePath());
66+
}
67+
68+
protected AttachmentRenderer<AttachmentData> getResponseRenderer() {
69+
return new FreemarkerAttachmentRenderer(getResponseTemplatePath());
70+
}
71+
72+
protected AttachmentProcessor<AttachmentData> getAttachmentProcessor() {
73+
return new DefaultAttachmentProcessor();
74+
}
75+
5576
@SuppressWarnings("NullableProblems")
5677
@Override
5778
public ClientHttpResponse intercept(@NonNull final HttpRequest request, final byte[] body,
5879
@NonNull final ClientHttpRequestExecution execution) throws IOException {
59-
final AttachmentProcessor<AttachmentData> processor = new DefaultAttachmentProcessor();
80+
final AttachmentProcessor<AttachmentData> processor = getAttachmentProcessor();
6081

6182
final HttpRequestAttachment.Builder requestAttachmentBuilder = HttpRequestAttachment.Builder
6283
.create("Request", request.getURI().toString())
@@ -67,7 +88,7 @@ public ClientHttpResponse intercept(@NonNull final HttpRequest request, final by
6788
}
6889

6990
final HttpRequestAttachment requestAttachment = requestAttachmentBuilder.build();
70-
processor.addAttachment(requestAttachment, new FreemarkerAttachmentRenderer(requestTemplatePath));
91+
processor.addAttachment(requestAttachment, getRequestRenderer());
7192

7293
final ClientHttpResponse clientHttpResponse = execution.execute(request, body);
7394

@@ -77,12 +98,12 @@ public ClientHttpResponse intercept(@NonNull final HttpRequest request, final by
7798
.setHeaders(toMapConverter(clientHttpResponse.getHeaders()))
7899
.setBody(StreamUtils.copyToString(clientHttpResponse.getBody(), StandardCharsets.UTF_8))
79100
.build();
80-
processor.addAttachment(responseAttachment, new FreemarkerAttachmentRenderer(responseTemplatePath));
101+
processor.addAttachment(responseAttachment, getResponseRenderer());
81102

82103
return clientHttpResponse;
83104
}
84105

85-
private static Map<String, String> toMapConverter(final Map<String, List<String>> items) {
106+
protected static Map<String, String> toMapConverter(final Map<String, List<String>> items) {
86107
final Map<String, String> result = new HashMap<>();
87108
items.forEach((key, value) -> result.put(key, String.join("; ", value)));
88109
return result;

0 commit comments

Comments
 (0)