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:
parent
f08a099ed9
commit
6362bfbcd6
|
@ -47,7 +47,7 @@ public final class JsonFormat implements EventFormat {
|
||||||
/**
|
/**
|
||||||
* JSON Data Content Type Discriminator
|
* 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 ObjectMapper mapper;
|
||||||
private final JsonFormatOptions options;
|
private final JsonFormatOptions options;
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,22 @@ class JsonFormatTest {
|
||||||
|
|
||||||
private final ObjectMapper mapper = new ObjectMapper();
|
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
|
@ParameterizedTest
|
||||||
@MethodSource("serializeTestArgumentsDefault")
|
@MethodSource("serializeTestArgumentsDefault")
|
||||||
void serialize(CloudEvent input, String outputFile) throws IOException {
|
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() {
|
public static Stream<Arguments> serializeTestArgumentsDefault() {
|
||||||
return Stream.of(
|
return Stream.of(
|
||||||
Arguments.of(V03_MIN, "v03/min.json"),
|
Arguments.of(V03_MIN, "v03/min.json"),
|
||||||
|
|
Loading…
Reference in New Issue