Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RestAssured: Add support for form params #1016

Merged
merged 5 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
baev marked this conversation as resolved.
Show resolved Hide resolved
}

@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));
baev marked this conversation as resolved.
Show resolved Hide resolved

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
Loading