From 44341294f2f5840af9e7fa68aa20134b8d7f1691 Mon Sep 17 00:00:00 2001 From: Maksim Date: Tue, 12 Mar 2024 18:07:23 +0300 Subject: [PATCH] feat(rest-assured): add support for form params (via #1016) --- .../http/HttpRequestAttachment.java | 35 +++++++++++++- .../src/main/resources/tpl/http-request.ftl | 47 +++++++++++-------- .../allure/restassured/AllureRestAssured.java | 4 ++ .../restassured/AllureRestAssuredTest.java | 8 +++- build.gradle.kts | 2 +- 5 files changed, 73 insertions(+), 23 deletions(-) diff --git a/allure-attachments/src/main/java/io/qameta/allure/attachment/http/HttpRequestAttachment.java b/allure-attachments/src/main/java/io/qameta/allure/attachment/http/HttpRequestAttachment.java index 5d4514140..75cda2e96 100644 --- a/allure-attachments/src/main/java/io/qameta/allure/attachment/http/HttpRequestAttachment.java +++ b/allure-attachments/src/main/java/io/qameta/allure/attachment/http/HttpRequestAttachment.java @@ -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; @@ -41,9 +42,18 @@ public class HttpRequestAttachment implements AttachmentData { private final Map cookies; + private final Map formParams; + public HttpRequestAttachment(final String name, final String url, final String method, final String body, final String curl, final Map headers, final Map 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 headers, + final Map cookies, final Map formParams) { this.name = name; this.url = url; this.method = method; @@ -51,6 +61,7 @@ public HttpRequestAttachment(final String name, final String url, final String m this.curl = curl; this.headers = headers; this.cookies = cookies; + this.formParams = formParams; } public String getUrl() { @@ -73,6 +84,10 @@ public Map getCookies() { return cookies; } + public Map getFormParams() { + return formParams; + } + public String getCurl() { return curl; } @@ -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)"; } @@ -111,6 +127,8 @@ public static final class Builder { private final Map cookies = new HashMap<>(); + private final Map 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"); @@ -160,6 +178,12 @@ public Builder setBody(final String body) { return this; } + public Builder setFormParams(final Map 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 @@ -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() { @@ -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('\''); @@ -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('\''); + } } } diff --git a/allure-attachments/src/main/resources/tpl/http-request.ftl b/allure-attachments/src/main/resources/tpl/http-request.ftl index b4e31d632..935c1c91a 100644 --- a/allure-attachments/src/main/resources/tpl/http-request.ftl +++ b/allure-attachments/src/main/resources/tpl/http-request.ftl @@ -3,36 +3,45 @@
<#if data.method??>${data.method}<#else>GET to <#if data.url??>${data.url}<#else>Unknown
<#if data.body??> -

Body

-
+

Body

+
     <#t>${data.body}
     
-
+
<#if (data.headers)?has_content> -

Headers

-
- <#list data.headers as name, value> -
${name}: ${value!"null"}
- -
+

Headers

+
+ <#list data.headers as name, value> +
${name}: ${value!"null"}
+ +
<#if (data.cookies)?has_content> -

Cookies

-
- <#list data.cookies as name, value> -
${name}: ${value!"null"}
- -
+

Cookies

+
+ <#list data.cookies as name, value> +
${name}: ${value!"null"}
+ +
<#if data.curl??> -

Curl

-
-${data.curl} -
+

Curl

+
+ ${data.curl} +
+ + +<#if (data.formParams)?has_content> +

FormParams

+
+ <#list data.formParams as name, value> +
${name}: ${value!"null"}
+ +
diff --git a/allure-rest-assured/src/main/java/io/qameta/allure/restassured/AllureRestAssured.java b/allure-rest-assured/src/main/java/io/qameta/allure/restassured/AllureRestAssured.java index 6eebf322c..49b4c203b 100644 --- a/allure-rest-assured/src/main/java/io/qameta/allure/restassured/AllureRestAssured.java +++ b/allure-rest-assured/src/main/java/io/qameta/allure/restassured/AllureRestAssured.java @@ -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( diff --git a/allure-rest-assured/src/test/java/io/qameta/allure/restassured/AllureRestAssuredTest.java b/allure-rest-assured/src/test/java/io/qameta/allure/restassured/AllureRestAssuredTest.java index f38e778af..1a58504c9 100644 --- a/allure-rest-assured/src/test/java/io/qameta/allure/restassured/AllureRestAssuredTest.java +++ b/allure-rest-assured/src/test/java/io/qameta/allure/restassured/AllureRestAssuredTest.java @@ -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; @@ -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()); diff --git a/build.gradle.kts b/build.gradle.kts index 82d806791..69a89cf1b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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")