Skip to content

Commit 4434129

Browse files
authored
feat(rest-assured): add support for form params (via #1016)
1 parent 1c69799 commit 4434129

File tree

5 files changed

+73
-23
lines changed

5 files changed

+73
-23
lines changed

allure-attachments/src/main/java/io/qameta/allure/attachment/http/HttpRequestAttachment.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.qameta.allure.attachment.AttachmentData;
1919
import io.qameta.allure.util.ObjectUtils;
2020

21+
import java.util.Collections;
2122
import java.util.HashMap;
2223
import java.util.Map;
2324
import java.util.Objects;
@@ -41,16 +42,26 @@ public class HttpRequestAttachment implements AttachmentData {
4142

4243
private final Map<String, String> cookies;
4344

45+
private final Map<String, String> formParams;
46+
4447
public HttpRequestAttachment(final String name, final String url, final String method,
4548
final String body, final String curl, final Map<String, String> headers,
4649
final Map<String, String> cookies) {
50+
this(name, url, method, body, curl, headers, cookies, Collections.emptyMap());
51+
}
52+
53+
@SuppressWarnings("checkstyle:parameternumber")
54+
public HttpRequestAttachment(final String name, final String url, final String method,
55+
final String body, final String curl, final Map<String, String> headers,
56+
final Map<String, String> cookies, final Map<String, String> formParams) {
4757
this.name = name;
4858
this.url = url;
4959
this.method = method;
5060
this.body = body;
5161
this.curl = curl;
5262
this.headers = headers;
5363
this.cookies = cookies;
64+
this.formParams = formParams;
5465
}
5566

5667
public String getUrl() {
@@ -73,6 +84,10 @@ public Map<String, String> getCookies() {
7384
return cookies;
7485
}
7586

87+
public Map<String, String> getFormParams() {
88+
return formParams;
89+
}
90+
7691
public String getCurl() {
7792
return curl;
7893
}
@@ -90,6 +105,7 @@ public String toString() {
90105
+ ",\n\tbody=" + this.body
91106
+ ",\n\theaders=" + ObjectUtils.mapToString(this.headers)
92107
+ ",\n\tcookies=" + ObjectUtils.mapToString(this.cookies)
108+
+ ",\n\tformParams=" + ObjectUtils.mapToString(this.formParams)
93109
+ "\n)";
94110
}
95111

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

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

130+
private final Map<String, String> formParams = new HashMap<>();
131+
114132
private Builder(final String name, final String url) {
115133
Objects.requireNonNull(name, "Name must not be null value");
116134
Objects.requireNonNull(url, "Url must not be null value");
@@ -160,6 +178,12 @@ public Builder setBody(final String body) {
160178
return this;
161179
}
162180

181+
public Builder setFormParams(final Map<String, String> formParams) {
182+
Objects.requireNonNull(formParams, "Form params must not be null value");
183+
this.formParams.putAll(formParams);
184+
return this;
185+
}
186+
163187
/**
164188
* Use setter method instead.
165189
* @deprecated scheduled for removal in 3.0 release
@@ -215,7 +239,7 @@ public Builder withBody(final String body) {
215239
}
216240

217241
public HttpRequestAttachment build() {
218-
return new HttpRequestAttachment(name, url, method, body, getCurl(), headers, cookies);
242+
return new HttpRequestAttachment(name, url, method, body, getCurl(), headers, cookies, formParams);
219243
}
220244

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

230255
if (Objects.nonNull(body)) {
231256
builder.append(" -d '").append(body).append('\'');
@@ -248,5 +273,13 @@ private static void appendCookie(final StringBuilder builder, final String key,
248273
.append(value)
249274
.append('\'');
250275
}
276+
277+
private static void appendFormParams(final StringBuilder builder, final String key, final String value) {
278+
builder.append(" --form '")
279+
.append(key)
280+
.append('=')
281+
.append(value)
282+
.append('\'');
283+
}
251284
}
252285
}

allure-attachments/src/main/resources/tpl/http-request.ftl

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,45 @@
33
<div><#if data.method??>${data.method}<#else>GET</#if> to <#if data.url??>${data.url}<#else>Unknown</#if></div>
44

55
<#if data.body??>
6-
<h4>Body</h4>
7-
<div>
6+
<h4>Body</h4>
7+
<div>
88
<pre class="preformated-text">
99
<#t>${data.body}
1010
</pre>
11-
</div>
11+
</div>
1212
</#if>
1313

1414
<#if (data.headers)?has_content>
15-
<h4>Headers</h4>
16-
<div>
17-
<#list data.headers as name, value>
18-
<div>${name}: ${value!"null"}</div>
19-
</#list>
20-
</div>
15+
<h4>Headers</h4>
16+
<div>
17+
<#list data.headers as name, value>
18+
<div>${name}: ${value!"null"}</div>
19+
</#list>
20+
</div>
2121
</#if>
2222

2323

2424
<#if (data.cookies)?has_content>
25-
<h4>Cookies</h4>
26-
<div>
27-
<#list data.cookies as name, value>
28-
<div>${name}: ${value!"null"}</div>
29-
</#list>
30-
</div>
25+
<h4>Cookies</h4>
26+
<div>
27+
<#list data.cookies as name, value>
28+
<div>${name}: ${value!"null"}</div>
29+
</#list>
30+
</div>
3131
</#if>
3232

3333
<#if data.curl??>
34-
<h4>Curl</h4>
35-
<div>
36-
${data.curl}
37-
</div>
34+
<h4>Curl</h4>
35+
<div>
36+
${data.curl}
37+
</div>
38+
</#if>
39+
40+
<#if (data.formParams)?has_content>
41+
<h4>FormParams</h4>
42+
<div>
43+
<#list data.formParams as name, value>
44+
<div>${name}: ${value!"null"}</div>
45+
</#list>
46+
</div>
3847
</#if>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ public Response filter(final FilterableRequestSpecification requestSpec,
9898
requestAttachmentBuilder.setBody(prettifier.getPrettifiedBodyIfPossible(requestSpec));
9999
}
100100

101+
if (Objects.nonNull(requestSpec.getFormParams())) {
102+
requestAttachmentBuilder.setFormParams(requestSpec.getFormParams());
103+
}
104+
101105
final HttpRequestAttachment requestAttachment = requestAttachmentBuilder.build();
102106

103107
new DefaultAttachmentProcessor().addAttachment(

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.qameta.allure.model.TestResult;
2525
import io.qameta.allure.test.AllureResults;
2626
import io.restassured.RestAssured;
27+
import io.restassured.http.ContentType;
2728
import org.junit.jupiter.api.Test;
2829
import org.junit.jupiter.api.extension.ExtensionContext;
2930
import org.junit.jupiter.params.ParameterizedTest;
@@ -143,9 +144,12 @@ protected final AllureResults executeWithStub(ResponseDefinitionBuilder response
143144
server.start();
144145
WireMock.configureFor(server.port());
145146

146-
WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/hello")).willReturn(responseBuilder));
147+
WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/hello?Allure=Form")).willReturn(responseBuilder));
147148
try {
148-
RestAssured.when().get(server.url("/hello")).then().statusCode(statusCode);
149+
RestAssured.given()
150+
.contentType(ContentType.URLENC)
151+
.formParams("Allure", "Form")
152+
.get(server.url("/hello")).then().statusCode(statusCode);
149153
} finally {
150154
server.stop();
151155
RestAssured.replaceFiltersWith(ImmutableList.of());

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ configure(libs) {
149149
}
150150
dependencies {
151151
dependency("com.github.spotbugs:spotbugs:4.8.3")
152-
dependency("com.github.tomakehurst:wiremock:2.27.2")
152+
dependency("com.github.tomakehurst:wiremock:3.0.1")
153153
dependency("com.google.inject:guice:5.1.0")
154154
dependency("com.google.testing.compile:compile-testing:0.19")
155155
dependency("com.puppycrawl.tools:checkstyle:10.13.0")

0 commit comments

Comments
 (0)