Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class ContentType {

private static final Pattern charsetPattern = Pattern
.compile("(?i)\\bcharset=\\s*\"?([^\\s;\"/]*)/?>");
.compile("(?i)\\bcharset=\\s*\"?([^\\s;\"/>]*)[\"?/>]?");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The updated regular expression is a definite improvement for handling quoted charsets. However, it could be made more robust. According to RFC 2616, the ? character is a separator and should not be part of the charset value. The current regex might incorrectly include it in the captured charset name.

To handle this correctly, I suggest adding ? to the negated character class in the capturing group. This will ensure that ? acts as a delimiter for the charset value.

Suggested change
.compile("(?i)\\bcharset=\\s*\"?([^\\s;\"/>]*)[\"?/>]?");
.compile("(?i)\\bcharset=\\s*\"?([^\\s;\"/>?]*)[\"?/>]?");



private final String mediaType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,44 @@

import org.junit.Test;

import java.nio.charset.StandardCharsets;

import static org.assertj.core.api.Assertions.assertThat;

public class ContentTypeTest {

@Test
public void shouldParseContentTypeWoCharset() {
//TODO
ContentType contentType = ContentType.parse("application/json");
assertThat(contentType.getMediaType()).isEqualTo("application/json");
assertThat(contentType.getCharset()).isEqualTo(StandardCharsets.UTF_8);
}

@Test
public void shouldParseContentTypeWithCharset() {
//TODO
ContentType contentType = ContentType.parse("application/json; charset=utf-8");
assertThat(contentType.getMediaType()).isEqualTo("application/json");
assertThat(contentType.getCharset()).isEqualTo(StandardCharsets.UTF_8);
}

@Test
public void shouldParseContentTypeWithCharsetQuoted() {
ContentType contentType = ContentType.parse("application/json; charset=\"UTF-8\"");
assertThat(contentType.getMediaType()).isEqualTo("application/json");
assertThat(contentType.getCharset()).isEqualTo(StandardCharsets.UTF_8);
}

@Test
public void shouldParseContentTypeWithDifferentCharset() {
ContentType contentType = ContentType.parse("text/plain; charset=iso-8859-1");
assertThat(contentType.getMediaType()).isEqualTo("text/plain");
assertThat(contentType.getCharset()).isEqualTo(StandardCharsets.ISO_8859_1);
}

@Test
public void shouldParseContentTypeWithUnsupportedCharset() {
ContentType contentType = ContentType.parse("application/json; charset=wrong");
assertThat(contentType.getMediaType()).isEqualTo("application/json");
assertThat(contentType.getCharset()).isEqualTo(StandardCharsets.UTF_8);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,65 @@
package reactivefeign.utils;

import org.junit.Test;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

public class FormUtilsTest {

//TODO
@Test
public void shouldSerializeSimpleForm() {
Map<String, Object> form = new LinkedHashMap<>();
form.put("key1", "value1");
form.put("key2", "value2");

SerializedFormData serialized = FormUtils.serializeForm(form, StandardCharsets.UTF_8);

assertThat(serialized.getFormDataString()).isEqualTo("key1=value1&key2=value2");
}

@Test
public void shouldSerializeFormWithCollection() {
Map<String, Object> form = new LinkedHashMap<>();
form.put("key", Arrays.asList("value1", "value2"));

SerializedFormData serialized = FormUtils.serializeForm(form, StandardCharsets.UTF_8);

assertThat(serialized.getFormDataString()).isEqualTo("key=value1&key=value2");
}

@Test
public void shouldSerializeFormWithSpecialCharacters() {
Map<String, Object> form = new LinkedHashMap<>();
form.put("key ", "value&");

SerializedFormData serialized = FormUtils.serializeForm(form, StandardCharsets.UTF_8);

assertThat(serialized.getFormDataString()).isEqualTo("key+=value%26");
}

@Test
public void shouldSerializeFormWithNullValue() {
Map<String, Object> form = new LinkedHashMap<>();
form.put("key", null);

SerializedFormData serialized = FormUtils.serializeForm(form, StandardCharsets.UTF_8);

assertThat(serialized.getFormDataString()).isEqualTo("key");
}

@Test
public void shouldSerializeFormWithDifferentCharset() {
Map<String, Object> form = new LinkedHashMap<>();
form.put("key", "value");

SerializedFormData serialized = FormUtils.serializeForm(form, StandardCharsets.ISO_8859_1);

assertThat(serialized.getFormDataString()).isEqualTo("key=value");
assertThat(serialized.getFormData()).isEqualTo(StandardCharsets.ISO_8859_1.encode("key=value"));
}
}
Loading