From 1c843bf72b987a963ce18fad9c10a616d90c7a87 Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Wed, 7 Sep 2022 20:53:29 +0300 Subject: [PATCH 1/6] Misprint fix --- README.md | 4 ++-- README_TEMPLATE.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fb223e7..0077d53 100644 --- a/README.md +++ b/README.md @@ -68,8 +68,8 @@ public class BaseTest { } ``` -If you don't have a base class, you can pui initialization into one of the most general initialization block. E.G. for TestNG it may be -`@BeforeSuite`: +If you don't have a base class, you can put initialization into one of the most general initialization block. E.G. +for TestNG it may be `@BeforeSuite`: ```java public class ApiTest { diff --git a/README_TEMPLATE.md b/README_TEMPLATE.md index 2a21dab..7aebb7a 100644 --- a/README_TEMPLATE.md +++ b/README_TEMPLATE.md @@ -68,8 +68,8 @@ public class BaseTest { } ``` -If you don't have a base class, you can pui initialization into one of the most general initialization block. E.G. for TestNG it may be -`@BeforeSuite`: +If you don't have a base class, you can put initialization into one of the most general initialization block. E.G. for +TestNG it may be `@BeforeSuite`: ```java public class ApiTest { From 4af0c086687d79ea7a07978fba6f8518c22fda90 Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Wed, 7 Sep 2022 20:56:23 +0300 Subject: [PATCH 2/6] Documentation fix --- README.md | 2 +- README_TEMPLATE.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0077d53..ab48aaa 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ public class BaseTest { RestAssured.filters(new ReportPortalRestAssuredLoggingFilter( 42, LogLevel.INFO, - DefaultHttpHeaderConverter.HEADER_SANITIZING_CONVERTER, + SanitizingHttpHeaderConverter.INSTANCE, DefaultHttpHeaderConverter.INSTANCE, DefaultCookieConverter.INSTANCE, DefaultUriConverter.INSTANCE diff --git a/README_TEMPLATE.md b/README_TEMPLATE.md index 7aebb7a..2ddde0a 100644 --- a/README_TEMPLATE.md +++ b/README_TEMPLATE.md @@ -101,7 +101,7 @@ public class BaseTest { RestAssured.filters(new ReportPortalRestAssuredLoggingFilter( 42, LogLevel.INFO, - DefaultHttpHeaderConverter.HEADER_SANITIZING_CONVERTER, + SanitizingHttpHeaderConverter.INSTANCE, DefaultHttpHeaderConverter.INSTANCE, DefaultCookieConverter.INSTANCE, DefaultUriConverter.INSTANCE From 28f90f11b7c2e7af1a4ec092f8c8cf9c2d8947dd Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Wed, 7 Sep 2022 21:01:10 +0300 Subject: [PATCH 3/6] Misprint fix --- README.md | 7 ++++--- README_TEMPLATE.md | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ab48aaa..8b63d7a 100644 --- a/README.md +++ b/README.md @@ -80,9 +80,10 @@ public class ApiTest { } ``` -> **NOTE**: If you have more than one suite in your execution then it will mean REST Assured will be initialized over and over again with -> the Logger and you will get log duplication in the following suites. You can apply `RestAssured.reset();` before the filter adding to avod -> that. But this also means you will have to configure REST Assured anew each suite. +> **NOTE**: If you have more than one suite in your execution then it will mean REST Assured will be initialized over +> and over again with the Logger and you will get log duplication in the following suites. You can apply +> `RestAssured.reset();` before the filter adding to avoid that. But this also means you will have to configure +> REST Assured anew each suite. ### Sanitize Request / Response data diff --git a/README_TEMPLATE.md b/README_TEMPLATE.md index 2ddde0a..7b089b3 100644 --- a/README_TEMPLATE.md +++ b/README_TEMPLATE.md @@ -80,9 +80,10 @@ public class ApiTest { } ``` -> **NOTE**: If you have more than one suite in your execution then it will mean REST Assured will be initialized over and over again with -> the Logger and you will get log duplication in the following suites. You can apply `RestAssured.reset();` before the filter adding to avod -> that. But this also means you will have to configure REST Assured anew each suite. +> **NOTE**: If you have more than one suite in your execution then it will mean REST Assured will be initialized over +> and over again with the Logger and you will get log duplication in the following suites. You can apply +> `RestAssured.reset();` before the filter adding to avoid that. But this also means you will have to configure +> REST Assured anew each suite. ### Sanitize Request / Response data From 81fcb51772023aec72c6c60851eb7e9a7289e42a Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Fri, 9 Sep 2022 17:27:29 +0300 Subject: [PATCH 4/6] Version update --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 9c2cca4..de5e26f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -41,7 +41,7 @@ jobs: uses: actions/checkout@v2 - name: Generate versions - uses: HardNorth/github-version-generate@v1.1.2 + uses: HardNorth/github-version-generate@v1.2.0 with: version-source: file version-file: ${{ env.VERSION_FILE }} From 8664db80a3d42adb92648e804cec83e95fb02db6 Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Mon, 19 Sep 2022 21:43:24 +0300 Subject: [PATCH 5/6] Fix for passing Multipart request part as `java.io.File` with text type --- CHANGELOG.md | 2 + .../support/HttpEntityFactory.java | 42 +++++++++++++------ ...ortPortalRestAssuredLoggingFilterTest.java | 41 ++++++++++++++++-- src/test/resources/test.json | 1 + 4 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 src/test/resources/test.json diff --git a/CHANGELOG.md b/CHANGELOG.md index a5358b5..b785df5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog ## [Unreleased] +### Fixed +- Passing Multipart request part as `java.io.File` with text type, by @HardNorth ## [5.2.0] ### Added diff --git a/src/main/java/com/epam/reportportal/restassured/support/HttpEntityFactory.java b/src/main/java/com/epam/reportportal/restassured/support/HttpEntityFactory.java index 2b3cc14..18fb768 100644 --- a/src/main/java/com/epam/reportportal/restassured/support/HttpEntityFactory.java +++ b/src/main/java/com/epam/reportportal/restassured/support/HttpEntityFactory.java @@ -22,7 +22,9 @@ import com.epam.reportportal.formatting.http.entities.BodyType; import com.epam.reportportal.formatting.http.entities.Cookie; import com.epam.reportportal.formatting.http.entities.Header; +import com.epam.reportportal.message.TypeAwareByteSource; import com.epam.reportportal.service.ReportPortal; +import com.epam.reportportal.utils.files.Utils; import io.restassured.response.Response; import io.restassured.response.ResponseBodyData; import io.restassured.specification.FilterableRequestSpecification; @@ -32,6 +34,8 @@ import javax.annotation.Nullable; import java.io.File; import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.util.Calendar; import java.util.Map; import java.util.function.Function; @@ -88,24 +92,36 @@ public static HttpRequestFormatter createHttpRequestFormatter( String partMimeType = ofNullable(it.getMimeType()).orElse(ContentType.APPLICATION_OCTET_STREAM.getMimeType()); HttpPartFormatter.Builder partBuilder; try { + Object body = it.getContent(); + HttpPartFormatter.PartType partType; if (BodyType.TEXT == getBodyType(partMimeType, bodyTypeMap)) { - partBuilder = new HttpPartFormatter.Builder(HttpPartFormatter.PartType.TEXT, - partMimeType, - it.getContent() - ); + partType = HttpPartFormatter.PartType.TEXT; } else { - Object body = it.getContent(); - if (body instanceof File) { - partBuilder = new HttpPartFormatter.Builder(HttpPartFormatter.PartType.BINARY, - (File) it.getContent() - ); + partType = HttpPartFormatter.PartType.BINARY; + } + + Object content; + if (body instanceof File) { + TypeAwareByteSource file = Utils.getFile((File) body); + byte[] data = file.read(); + if (partType == HttpPartFormatter.PartType.TEXT) { + content = ofNullable(data).map(d -> { + try { + return new String(d, + ofNullable(it.getCharset()).orElse(StandardCharsets.UTF_8.name()) + ); + } catch (UnsupportedEncodingException e) { + throw new IllegalStateException(e); + } + }).orElse(""); } else { - partBuilder = new HttpPartFormatter.Builder(HttpPartFormatter.PartType.BINARY, - partMimeType, - it.getContent() - ); + content = data; } + } else { + content = body; } + partBuilder = new HttpPartFormatter.Builder(partType, partMimeType, content); + ofNullable(it.getHeaders()).ifPresent(headers -> headers.forEach((key, value) -> partBuilder.addHeader( new Header(key, value)))); partBuilder.controlName(it.getControlName()); diff --git a/src/test/java/com/epam/reportportal/restassured/ReportPortalRestAssuredLoggingFilterTest.java b/src/test/java/com/epam/reportportal/restassured/ReportPortalRestAssuredLoggingFilterTest.java index 64daeac..48fca8a 100644 --- a/src/test/java/com/epam/reportportal/restassured/ReportPortalRestAssuredLoggingFilterTest.java +++ b/src/test/java/com/epam/reportportal/restassured/ReportPortalRestAssuredLoggingFilterTest.java @@ -414,8 +414,8 @@ public void test_rest_assured_logger_empty_multipart(List headers) { return new MultiPartSpecification() { @Override public Object getContent() { @@ -442,7 +442,7 @@ public String getMimeType() { @Override public Map getHeaders() { - return null; + return headers; } @Override @@ -462,6 +462,11 @@ public boolean hasFileName() { }; } + @SuppressWarnings("SameParameterValue") + private MultiPartSpecification getBinaryPart(String mimeType, String filePath, boolean file) { + return getBinaryPart(mimeType, filePath, file, null); + } + @ParameterizedTest @ValueSource(booleans = { true, false }) public void test_rest_assured_logger_image_multipart(boolean isFile) throws IOException { @@ -609,4 +614,34 @@ public void test_rest_assured_log_filter_type() { ); assertThat(logs.getRight(), hasSize(0)); } + + @Test + public void test_rest_assured_logger_text_as_file_multipart() { + String textPath = "test.json"; + String text = JsonPrettier.INSTANCE.apply(new String(getResource(textPath))); + String requestType = ContentType.MULTIPART_FORM_DATA.getMimeType(); + String textType = ContentType.APPLICATION_JSON.getMimeType(); + + FilterableRequestSpecification requestSpecification = mockBasicRequest(requestType); + when(requestSpecification.getMultiPartParams()).thenReturn(Collections.singletonList(getBinaryPart(textType, + textPath, + true, + Collections.singletonMap(HttpHeaders.CONTENT_TYPE, textType) + ))); + + Triple, List, List> logs = runFilterComplexMessageCapture(requestSpecification, + null + ); + assertThat(logs.getLeft(), hasSize(1)); + assertThat(logs.getMiddle(), hasSize(2)); + assertThat(logs.getRight(), hasSize(0)); + + assertThat(logs.getLeft().get(0), equalTo(EMPTY_REQUEST)); + + assertThat(logs.getMiddle().get(0), + equalTo(Constants.HEADERS_TAG + "\n" + HttpHeaders.CONTENT_TYPE + ": " + textType + "\n\n" + + Constants.BODY_PART_TAG + "\n```\n" + text + "\n```") + ); + assertThat(logs.getMiddle().get(1), equalTo(NULL_RESPONSE)); + } } diff --git a/src/test/resources/test.json b/src/test/resources/test.json new file mode 100644 index 0000000..db17831 --- /dev/null +++ b/src/test/resources/test.json @@ -0,0 +1 @@ +[{"file" : {"name" : "UserPhotoPNG.png","level" : "INFO","time" : 1663610851592,"message" : "Test Log message 1663610851143-2-5342","launchUuid" : "3920fea5-c2e6-4e32-97fb-a2ea17a60540","itemUuid" : "8ed8c2c7-510e-45dc-8e6e-29be964e06b3"}}, {"file" : {"name" : "UserPhotoJPEG.jpeg"},"level" : "INFO","time" : 1663610851592,"message" : "Test Log message 1663610851143-2-5342","launchUuid" : "3920fea5-c2e6-4e32-97fb-a2ea17a60540","itemUuid" : "8ed8c2c7-510e-45dc-8e6e-29be964e06b3"}] \ No newline at end of file From 1b89ca338febdcd1daa5f015e84e7e60ba837873 Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Mon, 19 Sep 2022 21:48:14 +0300 Subject: [PATCH 6/6] line ending fix --- src/test/resources/test.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/test.json b/src/test/resources/test.json index db17831..d5f1b36 100644 --- a/src/test/resources/test.json +++ b/src/test/resources/test.json @@ -1 +1 @@ -[{"file" : {"name" : "UserPhotoPNG.png","level" : "INFO","time" : 1663610851592,"message" : "Test Log message 1663610851143-2-5342","launchUuid" : "3920fea5-c2e6-4e32-97fb-a2ea17a60540","itemUuid" : "8ed8c2c7-510e-45dc-8e6e-29be964e06b3"}}, {"file" : {"name" : "UserPhotoJPEG.jpeg"},"level" : "INFO","time" : 1663610851592,"message" : "Test Log message 1663610851143-2-5342","launchUuid" : "3920fea5-c2e6-4e32-97fb-a2ea17a60540","itemUuid" : "8ed8c2c7-510e-45dc-8e6e-29be964e06b3"}] \ No newline at end of file +[{"file" : {"name" : "UserPhotoPNG.png","level" : "INFO","time" : 1663610851592,"message" : "Test Log message 1663610851143-2-5342","launchUuid" : "3920fea5-c2e6-4e32-97fb-a2ea17a60540","itemUuid" : "8ed8c2c7-510e-45dc-8e6e-29be964e06b3"}}, {"file" : {"name" : "UserPhotoJPEG.jpeg"},"level" : "INFO","time" : 1663610851592,"message" : "Test Log message 1663610851143-2-5342","launchUuid" : "3920fea5-c2e6-4e32-97fb-a2ea17a60540","itemUuid" : "8ed8c2c7-510e-45dc-8e6e-29be964e06b3"}]