Skip to content

Commit 2c21ebe

Browse files
authored
fix response name caching for rest assured (fixes #678, via #679)
1 parent c8fa704 commit 2c21ebe

2 files changed

Lines changed: 55 additions & 7 deletions

File tree

allure-rest-assured/src/main/java/io/qameta/allure/restassured/AllureRestAssured.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import static io.qameta.allure.attachment.http.HttpRequestAttachment.Builder.create;
3535
import static io.qameta.allure.attachment.http.HttpResponseAttachment.Builder.create;
36+
import static java.util.Optional.ofNullable;
3637

3738
/**
3839
* Allure logger filter for Rest-assured.
@@ -105,10 +106,11 @@ public Response filter(final FilterableRequestSpecification requestSpec,
105106
);
106107

107108
final Response response = filterContext.next(requestSpec, responseSpec);
108-
if (Objects.isNull(responseAttachmentName)) {
109-
responseAttachmentName = response.getStatusLine();
110-
}
111-
final HttpResponseAttachment responseAttachment = create(responseAttachmentName)
109+
110+
final String attachmentName = ofNullable(responseAttachmentName)
111+
.orElse(response.getStatusLine());
112+
113+
final HttpResponseAttachment responseAttachment = create(attachmentName)
112114
.setResponseCode(response.getStatusCode())
113115
.setHeaders(toMapConverter(response.getHeaders()))
114116
.setBody(prettifier.getPrettifiedBodyIfPossible(response, response.getBody()))

allure-rest-assured/src/test/java/io/qameta/allure/restassured/AllureRestAssuredTest.java

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,27 @@
1616
package io.qameta.allure.restassured;
1717

1818
import com.github.tomakehurst.wiremock.WireMockServer;
19+
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
1920
import com.github.tomakehurst.wiremock.client.WireMock;
2021
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
2122
import com.google.common.collect.ImmutableList;
2223
import io.qameta.allure.model.Attachment;
2324
import io.qameta.allure.model.TestResult;
2425
import io.qameta.allure.test.AllureResults;
2526
import io.restassured.RestAssured;
27+
import org.junit.jupiter.api.Test;
2628
import org.junit.jupiter.api.extension.ExtensionContext;
2729
import org.junit.jupiter.params.ParameterizedTest;
2830
import org.junit.jupiter.params.provider.Arguments;
2931
import org.junit.jupiter.params.provider.ArgumentsProvider;
3032
import org.junit.jupiter.params.provider.ArgumentsSource;
31-
import java.util.*;
33+
34+
import java.util.ArrayList;
35+
import java.util.Collection;
36+
import java.util.List;
3237
import java.util.stream.Collectors;
3338
import java.util.stream.Stream;
39+
3440
import static io.qameta.allure.test.RunUtils.runWithinTestContext;
3541
import static org.assertj.core.api.Assertions.assertThat;
3642
import static org.junit.jupiter.params.provider.Arguments.arguments;
@@ -68,6 +74,41 @@ void shouldCreateAttachment(final List<String> attachmentNames, final AllureRest
6874
.isEqualTo(attachmentNames);
6975
}
7076

77+
@Test
78+
void shouldProperlySetAttachmentNameForSingleFilterInstance() {
79+
final AllureRestAssured filter = new AllureRestAssured();
80+
81+
final ResponseDefinitionBuilder responseBuilderOne = WireMock.aResponse()
82+
.withStatus(200)
83+
.withBody("some body");
84+
85+
final ResponseDefinitionBuilder responseBuilderTwo = WireMock.aResponse()
86+
.withStatus(400)
87+
.withBody("some other body");
88+
89+
RestAssured.replaceFiltersWith(filter);
90+
final AllureResults resultsOne = executeWithStub(responseBuilderOne);
91+
92+
RestAssured.replaceFiltersWith(filter);
93+
final AllureResults resultsTwo = executeWithStub(responseBuilderTwo);
94+
95+
assertThat(resultsOne.getTestResults()
96+
.stream()
97+
.map(TestResult::getAttachments)
98+
.flatMap(Collection::stream)
99+
.map(Attachment::getName))
100+
.hasSize(2)
101+
.anyMatch(res -> res.equals("HTTP/1.1 200 OK"));
102+
103+
assertThat(resultsTwo.getTestResults()
104+
.stream()
105+
.map(TestResult::getAttachments)
106+
.flatMap(Collection::stream)
107+
.map(Attachment::getName))
108+
.hasSize(2)
109+
.anyMatch(res -> res.equals("HTTP/1.1 400 Bad Request"));
110+
}
111+
71112
@ParameterizedTest
72113
@ArgumentsSource(AttachmentArgumentProvider.class)
73114
void shouldCatchAttachmentBody(final List<String> attachmentNames, final AllureRestAssured filter) {
@@ -91,15 +132,20 @@ void shouldCatchAttachmentBody(final List<String> attachmentNames, final AllureR
91132
}
92133

93134
protected final AllureResults execute() {
135+
return executeWithStub(WireMock.aResponse().withBody("some body"));
136+
}
137+
138+
protected final AllureResults executeWithStub(ResponseDefinitionBuilder responseBuilder) {
94139
final WireMockServer server = new WireMockServer(WireMockConfiguration.options().dynamicPort());
140+
final int statusCode = responseBuilder.build().getStatus();
95141

96142
return runWithinTestContext(() -> {
97143
server.start();
98144
WireMock.configureFor(server.port());
99145

100-
WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/hello")).willReturn(WireMock.aResponse().withBody("some body")));
146+
WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/hello")).willReturn(responseBuilder));
101147
try {
102-
RestAssured.when().get(server.url("/hello")).then().statusCode(200);
148+
RestAssured.when().get(server.url("/hello")).then().statusCode(statusCode);
103149
} finally {
104150
server.stop();
105151
RestAssured.replaceFiltersWith(ImmutableList.of());

0 commit comments

Comments
 (0)