Skip to content

Commit

Permalink
make AllureRestTemplate more configurable (via #873)
Browse files Browse the repository at this point in the history
  • Loading branch information
shalak committed May 23, 2023
1 parent bf0eec7 commit 343d0b8
Showing 1 changed file with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.qameta.allure.attachment.AttachmentData;
import io.qameta.allure.attachment.AttachmentProcessor;
import io.qameta.allure.attachment.AttachmentRenderer;
import io.qameta.allure.attachment.DefaultAttachmentProcessor;
import io.qameta.allure.attachment.FreemarkerAttachmentRenderer;
import io.qameta.allure.attachment.http.HttpRequestAttachment;
Expand All @@ -42,6 +43,14 @@ public class AllureRestTemplate implements ClientHttpRequestInterceptor {
private String requestTemplatePath = "http-request.ftl";
private String responseTemplatePath = "http-response.ftl";

public String getRequestTemplatePath() {
return requestTemplatePath;
}

public String getResponseTemplatePath() {
return responseTemplatePath;
}

public AllureRestTemplate setRequestTemplate(final String templatePath) {
this.requestTemplatePath = templatePath;
return this;
Expand All @@ -52,11 +61,23 @@ public AllureRestTemplate setResponseTemplate(final String templatePath) {
return this;
}

protected AttachmentRenderer<AttachmentData> getRequestRenderer() {
return new FreemarkerAttachmentRenderer(getRequestTemplatePath());
}

protected AttachmentRenderer<AttachmentData> getResponseRenderer() {
return new FreemarkerAttachmentRenderer(getResponseTemplatePath());
}

protected AttachmentProcessor<AttachmentData> getAttachmentProcessor() {
return new DefaultAttachmentProcessor();
}

@SuppressWarnings("NullableProblems")
@Override
public ClientHttpResponse intercept(@NonNull final HttpRequest request, final byte[] body,
@NonNull final ClientHttpRequestExecution execution) throws IOException {
final AttachmentProcessor<AttachmentData> processor = new DefaultAttachmentProcessor();
final AttachmentProcessor<AttachmentData> processor = getAttachmentProcessor();

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

final HttpRequestAttachment requestAttachment = requestAttachmentBuilder.build();
processor.addAttachment(requestAttachment, new FreemarkerAttachmentRenderer(requestTemplatePath));
processor.addAttachment(requestAttachment, getRequestRenderer());

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

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

return clientHttpResponse;
}

private static Map<String, String> toMapConverter(final Map<String, List<String>> items) {
protected static Map<String, String> toMapConverter(final Map<String, List<String>> items) {
final Map<String, String> result = new HashMap<>();
items.forEach((key, value) -> result.put(key, String.join("; ", value)));
return result;
Expand Down

0 comments on commit 343d0b8

Please sign in to comment.