Strip parameters from data content types to assess if it's JSON format (#484)

Signed-off-by: Frederic Delechamp <fdelechamp@guidewire.com>
This commit is contained in:
Frédéric Déléchamp 2022-10-06 10:42:37 +02:00 committed by GitHub
parent f08a099ed9
commit 6362bfbcd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 1 deletions

View File

@ -47,7 +47,7 @@ public final class JsonFormat implements EventFormat {
/**
* JSON Data Content Type Discriminator
*/
private static final Pattern JSON_CONTENT_TYPE_PATTERN = Pattern.compile("^(application|text)\\/([a-zA-Z]+\\+)?json$");
private static final Pattern JSON_CONTENT_TYPE_PATTERN = Pattern.compile("^(application|text)\\/([a-zA-Z]+\\+)?json(;.*)*$");
private final ObjectMapper mapper;
private final JsonFormatOptions options;

View File

@ -47,6 +47,22 @@ class JsonFormatTest {
private final ObjectMapper mapper = new ObjectMapper();
@ParameterizedTest
@MethodSource("jsonContentTypes")
void isJsonContentType(String contentType) {
boolean json = JsonFormat.dataIsJsonContentType(contentType);
assertThat(json).isTrue();
}
@ParameterizedTest
@MethodSource("wrongJsonContentTypes")
void isNotJsonContentType(String contentType) {
boolean json = JsonFormat.dataIsJsonContentType(contentType);
assertThat(json).isFalse();
}
@ParameterizedTest
@MethodSource("serializeTestArgumentsDefault")
void serialize(CloudEvent input, String outputFile) throws IOException {
@ -151,6 +167,39 @@ class JsonFormatTest {
}
static Stream<Arguments> jsonContentTypes() {
return Stream.of(
Arguments.of("application/json"),
Arguments.of("application/json;charset=utf-8"),
Arguments.of("application/json;\tcharset = \"utf-8\""),
Arguments.of("application/cloudevents+json;charset=UTF-8"),
Arguments.of("text/json"),
Arguments.of("text/json;charset=utf-8"),
Arguments.of("text/cloudevents+json;charset=UTF-8"),
Arguments.of("text/json;\twhatever"),
Arguments.of("text/json; boundary=something"),
Arguments.of("text/json;foo=\"bar\""),
Arguments.of("text/json; charset = \"us-ascii\""),
Arguments.of("text/json; \t"),
Arguments.of("text/json;"),
//https://www.rfc-editor.org/rfc/rfc2045#section-5.1
// any us-ascii char can be part of parameters (except CTRLs and tspecials)
Arguments.of("text/json; char-set = $!#$%&'*+.^_`|"),
Arguments.of((Object) null)
);
}
static Stream<Arguments> wrongJsonContentTypes() {
return Stream.of(
Arguments.of("applications/json"),
Arguments.of("application/jsom"),
Arguments.of("application/jsonwrong"),
Arguments.of("text/json "),
Arguments.of("text/json ;"),
Arguments.of("test/json")
);
}
public static Stream<Arguments> serializeTestArgumentsDefault() {
return Stream.of(
Arguments.of(V03_MIN, "v03/min.json"),