Skip to content

Commit

Permalink
feat(rest-assured): add support for form params (via #1016)
Browse files Browse the repository at this point in the history
  • Loading branch information
Phoenix124 authored Mar 12, 2024
1 parent 1c69799 commit 4434129
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 23 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 @@ -98,6 +98,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,6 +24,7 @@
import io.qameta.allure.model.TestResult;
import io.qameta.allure.test.AllureResults;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -143,9 +144,12 @@ protected final AllureResults executeWithStub(ResponseDefinitionBuilder response
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 {
RestAssured.when().get(server.url("/hello")).then().statusCode(statusCode);
RestAssured.given()
.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 4434129

Please sign in to comment.