Skip to content

Commit

Permalink
Merge pull request #18 from reportportal/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
HardNorth authored Sep 19, 2022
2 parents 7669366 + 1b89ca3 commit e8cd7c3
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## [Unreleased]
### Fixed
- Passing Multipart request part as `java.io.File` with text type, by @HardNorth

## [5.2.0]
### Added
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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

Expand All @@ -101,7 +102,7 @@ public class BaseTest {
RestAssured.filters(new ReportPortalRestAssuredLoggingFilter(
42,
LogLevel.INFO,
DefaultHttpHeaderConverter.HEADER_SANITIZING_CONVERTER,
SanitizingHttpHeaderConverter.INSTANCE,
DefaultHttpHeaderConverter.INSTANCE,
DefaultCookieConverter.INSTANCE,
DefaultUriConverter.INSTANCE
Expand Down
13 changes: 7 additions & 6 deletions README_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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

Expand All @@ -101,7 +102,7 @@ public class BaseTest {
RestAssured.filters(new ReportPortalRestAssuredLoggingFilter(
42,
LogLevel.INFO,
DefaultHttpHeaderConverter.HEADER_SANITIZING_CONVERTER,
SanitizingHttpHeaderConverter.INSTANCE,
DefaultHttpHeaderConverter.INSTANCE,
DefaultCookieConverter.INSTANCE,
DefaultUriConverter.INSTANCE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,8 @@ public void test_rest_assured_logger_empty_multipart(List<MultiPartSpecification
assertThat(logs.get(0), equalTo(EMPTY_REQUEST));
}

@SuppressWarnings("SameParameterValue")
private MultiPartSpecification getBinaryPart(String mimeType, String filePath, boolean file) {
private MultiPartSpecification getBinaryPart(String mimeType, String filePath, boolean file,
Map<String, String> headers) {
return new MultiPartSpecification() {
@Override
public Object getContent() {
Expand All @@ -442,7 +442,7 @@ public String getMimeType() {

@Override
public Map<String, String> getHeaders() {
return null;
return headers;
}

@Override
Expand All @@ -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 {
Expand Down Expand Up @@ -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<String>, List<String>, List<ReportPortalMessage>> 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));
}
}
1 change: 1 addition & 0 deletions src/test/resources/test.json
Original file line number Diff line number Diff line change
@@ -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"}]

0 comments on commit e8cd7c3

Please sign in to comment.