Skip to content

Commit

Permalink
Merge pull request #4 from reportportal/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
HardNorth authored Aug 30, 2022
2 parents 6226697 + 3d28e96 commit b972156
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# Changelog

## [Unreleased]
### Added
- `HttpRequestFormatter.Builder.bodyParams(List<Param>)` method, by @HardNorth

## [5.0.2]
### Added
- Add `HttpFormatUtils.getBodyType` method, by @HardNorth
- `HttpFormatUtils.getBodyType` method, by @HardNorth

## [5.0.1]
### Added
- Add `application/x-www-form-urlencoded` body type handling, by @HardNorth
- `application/x-www-form-urlencoded` body type handling, by @HardNorth

## [5.0.0]
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public class Constants {

public static final Set<String> FORM_TYPES = Collections.singleton(ContentType.APPLICATION_FORM_URLENCODED.getMimeType());

public static final Map<String, BodyType> BODY_TYPE_MAP = Stream.of(
public static final Map<String, BodyType> BODY_TYPE_MAP = Collections.unmodifiableMap(Stream.of(
TEXT_TYPES.stream().collect(Collectors.toMap(k -> k, v -> BodyType.TEXT)),
FORM_TYPES.stream().collect(Collectors.toMap(k -> k, v -> BodyType.FORM)),
MULTIPART_TYPES.stream().collect(Collectors.toMap(k -> k, v -> BodyType.MULTIPART))
).flatMap(m -> m.entrySet().stream()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
).flatMap(m -> m.entrySet().stream()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));

public static final Map<String, Function<String, String>> DEFAULT_PRETTIERS = Collections.unmodifiableMap(new HashMap<String, Function<String, String>>() {{
put(ContentType.APPLICATION_XML.getMimeType(), XmlPrettier.INSTANCE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ public static List<Param> toForm(@Nullable Map<String, String> formParameters) {
.collect(Collectors.toList())).orElse(Collections.emptyList());
}

@Nonnull
public static BodyType getBodyType(@Nullable String contentType, @Nullable Map<String, BodyType> typeMap) {
if (contentType == null || contentType.isEmpty()) {
return BodyType.NONE;
Expand All @@ -187,6 +188,7 @@ public static BodyType getBodyType(@Nullable String contentType, @Nullable Map<S
return ofNullable(typeMap).map(m -> m.getOrDefault(mimeType, BodyType.BINARY)).orElse(BodyType.BINARY);
}

@Nonnull
public static BodyType getBodyType(@Nullable String contentType) {
return getBodyType(contentType, BODY_TYPE_MAP);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ public Builder bodyBytes(String mimeType, byte[] payload) {
return this;
}

public Builder bodyParams(List<Param> formParameters) {
type = BodyType.FORM;
this.mimeType = ContentType.APPLICATION_FORM_URLENCODED.getMimeType();
body = formParameters;
return this;
}

public Builder bodyParams(Map<String, String> formParameters) {
type = BodyType.FORM;
this.mimeType = ContentType.APPLICATION_FORM_URLENCODED.getMimeType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class HttpRequestFormatterTest {

public static final String REQUEST_METHOD = "POST";
public static final String REQUEST_URL = "https://example.com";
public static final String TEST_TEXT = "this is a test";

public static Iterable<Object[]> textBodies() {
return Arrays.asList(new Object[] { ContentType.APPLICATION_JSON, "{\"object\": {\"key\": \"value\"}}",
Expand All @@ -44,24 +45,27 @@ public static Iterable<Object[]> textBodies() {
);
}

private static String getRequestString() {
return REQUEST_TAG + LINE_DELIMITER + REQUEST_METHOD + " to " + REQUEST_URL + LINE_DELIMITER + LINE_DELIMITER;
}

private static String getTextBody(ContentType contentType, String bodyTag, String payload) {
return REQUEST_TAG + LINE_DELIMITER + REQUEST_METHOD + " to " + REQUEST_URL + LINE_DELIMITER + LINE_DELIMITER
+ HEADERS_TAG + LINE_DELIMITER + HttpHeaders.CONTENT_TYPE + ": " + contentType + LINE_DELIMITER
+ LINE_DELIMITER + bodyTag + LINE_DELIMITER + BODY_HIGHLIGHT + LINE_DELIMITER + payload + LINE_DELIMITER
+ BODY_HIGHLIGHT;
return getRequestString() + HEADERS_TAG + LINE_DELIMITER + HttpHeaders.CONTENT_TYPE + ": " + contentType
+ LINE_DELIMITER + LINE_DELIMITER + bodyTag + LINE_DELIMITER + BODY_HIGHLIGHT + LINE_DELIMITER + payload
+ LINE_DELIMITER + BODY_HIGHLIGHT;
}

@ParameterizedTest
@MethodSource("textBodies")
public void verify_text_body_format(ContentType contentType, String body, String expected) {
public void verify_request_text_body_format(ContentType contentType, String body, String expected) {
HttpRequestFormatter formatter = new HttpRequestFormatter.Builder(REQUEST_METHOD, REQUEST_URL).addHeader(HttpHeaders.CONTENT_TYPE,
contentType.toString()
).bodyText(contentType.getMimeType(), body).build();
assertThat(formatter.formatAsText(), equalTo(getTextBody(contentType, BODY_TAG, expected)));
}

@Test
public void verify_form_body_format() {
public void verify_request_form_body_format() {
String body = "test1=test1&test2=wefwfqwef%20df%20qwef%20%23%24%25&test3=&F%23%24%25DFFG=dclk%20345%25%2056%20";
String expectedBody = "test1: test1\ntest2: wefwfqwef df qwef #$%\ntest3: \nF#$%DFFG: dclk 345% 56 ";
ContentType contentType = ContentType.APPLICATION_FORM_URLENCODED;
Expand All @@ -70,4 +74,17 @@ public void verify_form_body_format() {
).bodyParams(body).build();
assertThat(formatter.formatAsText(), equalTo(getTextBody(contentType, BODY_FORM_TAG, expectedBody)));
}

@Test
public void verify_request_no_header_format() {
HttpRequestFormatter formatter = new HttpRequestFormatter.Builder(REQUEST_METHOD, REQUEST_URL).bodyText(
ContentType.TEXT_PLAIN.getMimeType(),
TEST_TEXT
).build();
assertThat(
formatter.formatAsText(),
equalTo(getRequestString() + BODY_TAG + LINE_DELIMITER + BODY_HIGHLIGHT + LINE_DELIMITER + TEST_TEXT
+ LINE_DELIMITER + BODY_HIGHLIGHT)
);
}
}

0 comments on commit b972156

Please sign in to comment.