Skip to content

Commit

Permalink
Merge branch 'main' into consider_rest_assured_blacklisted_headers
Browse files Browse the repository at this point in the history
# Conflicts:
#	allure-rest-assured/src/test/java/io/qameta/allure/restassured/AllureRestAssuredTest.java
  • Loading branch information
antonfilichkin committed Mar 13, 2024
2 parents e49b19b + 4434129 commit 43f81c4
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.qameta.allure.attachment.AttachmentData;
import io.qameta.allure.util.ObjectUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand All @@ -41,16 +42,26 @@ public class HttpRequestAttachment implements AttachmentData {

private final Map<String, String> cookies;

private final Map<String, String> formParams;

public HttpRequestAttachment(final String name, final String url, final String method,
final String body, final String curl, final Map<String, String> headers,
final Map<String, String> cookies) {
this(name, url, method, body, curl, headers, cookies, Collections.emptyMap());
}

@SuppressWarnings("checkstyle:parameternumber")
public HttpRequestAttachment(final String name, final String url, final String method,
final String body, final String curl, final Map<String, String> headers,
final Map<String, String> cookies, final Map<String, String> formParams) {
this.name = name;
this.url = url;
this.method = method;
this.body = body;
this.curl = curl;
this.headers = headers;
this.cookies = cookies;
this.formParams = formParams;
}

public String getUrl() {
Expand All @@ -73,6 +84,10 @@ public Map<String, String> getCookies() {
return cookies;
}

public Map<String, String> getFormParams() {
return formParams;
}

public String getCurl() {
return curl;
}
Expand All @@ -90,6 +105,7 @@ public String toString() {
+ ",\n\tbody=" + this.body
+ ",\n\theaders=" + ObjectUtils.mapToString(this.headers)
+ ",\n\tcookies=" + ObjectUtils.mapToString(this.cookies)
+ ",\n\tformParams=" + ObjectUtils.mapToString(this.formParams)
+ "\n)";
}

Expand All @@ -111,6 +127,8 @@ public static final class Builder {

private final Map<String, String> cookies = new HashMap<>();

private final Map<String, String> formParams = new HashMap<>();

private Builder(final String name, final String url) {
Objects.requireNonNull(name, "Name must not be null value");
Objects.requireNonNull(url, "Url must not be null value");
Expand Down Expand Up @@ -160,6 +178,12 @@ public Builder setBody(final String body) {
return this;
}

public Builder setFormParams(final Map<String, String> formParams) {
Objects.requireNonNull(formParams, "Form params must not be null value");
this.formParams.putAll(formParams);
return this;
}

/**
* Use setter method instead.
* @deprecated scheduled for removal in 3.0 release
Expand Down Expand Up @@ -215,7 +239,7 @@ public Builder withBody(final String body) {
}

public HttpRequestAttachment build() {
return new HttpRequestAttachment(name, url, method, body, getCurl(), headers, cookies);
return new HttpRequestAttachment(name, url, method, body, getCurl(), headers, cookies, formParams);
}

private String getCurl() {
Expand All @@ -226,6 +250,7 @@ private String getCurl() {
builder.append(" '").append(url).append('\'');
headers.forEach((key, value) -> appendHeader(builder, key, value));
cookies.forEach((key, value) -> appendCookie(builder, key, value));
formParams.forEach((key, value) -> appendFormParams(builder, key, value));

if (Objects.nonNull(body)) {
builder.append(" -d '").append(body).append('\'');
Expand All @@ -248,5 +273,13 @@ private static void appendCookie(final StringBuilder builder, final String key,
.append(value)
.append('\'');
}

private static void appendFormParams(final StringBuilder builder, final String key, final String value) {
builder.append(" --form '")
.append(key)
.append('=')
.append(value)
.append('\'');
}
}
}
47 changes: 28 additions & 19 deletions allure-attachments/src/main/resources/tpl/http-request.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,45 @@
<div><#if data.method??>${data.method}<#else>GET</#if> to <#if data.url??>${data.url}<#else>Unknown</#if></div>

<#if data.body??>
<h4>Body</h4>
<div>
<h4>Body</h4>
<div>
<pre class="preformated-text">
<#t>${data.body}
</pre>
</div>
</div>
</#if>

<#if (data.headers)?has_content>
<h4>Headers</h4>
<div>
<#list data.headers as name, value>
<div>${name}: ${value!"null"}</div>
</#list>
</div>
<h4>Headers</h4>
<div>
<#list data.headers as name, value>
<div>${name}: ${value!"null"}</div>
</#list>
</div>
</#if>


<#if (data.cookies)?has_content>
<h4>Cookies</h4>
<div>
<#list data.cookies as name, value>
<div>${name}: ${value!"null"}</div>
</#list>
</div>
<h4>Cookies</h4>
<div>
<#list data.cookies as name, value>
<div>${name}: ${value!"null"}</div>
</#list>
</div>
</#if>

<#if data.curl??>
<h4>Curl</h4>
<div>
${data.curl}
</div>
<h4>Curl</h4>
<div>
${data.curl}
</div>
</#if>

<#if (data.formParams)?has_content>
<h4>FormParams</h4>
<div>
<#list data.formParams as name, value>
<div>${name}: ${value!"null"}</div>
</#list>
</div>
</#if>
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public class AllureJunit4ListenerAspect {
@After("execution(org.junit.runner.notification.RunNotifier.new())")
public void addListener(final JoinPoint point) {
final RunNotifier notifier = (RunNotifier) point.getThis();
notifier.removeListener(allure);
notifier.addListener(allure);
if (RunNotifier.class.equals(notifier.getClass())) {
notifier.addListener(allure);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ public Response filter(final FilterableRequestSpecification requestSpec,
requestAttachmentBuilder.setBody(prettifier.getPrettifiedBodyIfPossible(requestSpec));
}

if (Objects.nonNull(requestSpec.getFormParams())) {
requestAttachmentBuilder.setFormParams(requestSpec.getFormParams());
}

final HttpRequestAttachment requestAttachment = requestAttachmentBuilder.build();

new DefaultAttachmentProcessor().addAttachment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import io.qameta.allure.model.TestResult;
import io.qameta.allure.test.AllureResults;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.config.LogConfig;
import io.restassured.config.RestAssuredConfig;
import io.restassured.specification.RequestSender;
import io.restassured.specification.RequestSpecification;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -174,20 +176,22 @@ protected final AllureResults execute() {
}

protected final AllureResults executeWithStub(ResponseDefinitionBuilder responseBuilder) {
return executeWithStub(responseBuilder, RestAssured.when());
return executeWithStub(responseBuilder, RestAssured.given());
}

protected final AllureResults executeWithStub(ResponseDefinitionBuilder responseBuilder, RequestSender rs) {
protected final AllureResults executeWithStub(ResponseDefinitionBuilder responseBuilder, RequestSpecification spec) {
final WireMockServer server = new WireMockServer(WireMockConfiguration.options().dynamicPort());
final int statusCode = responseBuilder.build().getStatus();

return runWithinTestContext(() -> {
server.start();
WireMock.configureFor(server.port());

WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/hello")).willReturn(responseBuilder));
WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/hello?Allure=Form")).willReturn(responseBuilder));
try {
rs.get(server.url("/hello")).then().statusCode(statusCode);
spec.contentType(ContentType.URLENC)
.formParams("Allure", "Form")
.get(server.url("/hello")).then().statusCode(statusCode);
} finally {
server.stop();
RestAssured.replaceFiltersWith(ImmutableList.of());
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ configure(libs) {
}
dependencies {
dependency("com.github.spotbugs:spotbugs:4.8.3")
dependency("com.github.tomakehurst:wiremock:2.27.2")
dependency("com.github.tomakehurst:wiremock:3.0.1")
dependency("com.google.inject:guice:5.1.0")
dependency("com.google.testing.compile:compile-testing:0.19")
dependency("com.puppycrawl.tools:checkstyle:10.13.0")
Expand Down

0 comments on commit 43f81c4

Please sign in to comment.