Skip to content

Commit

Permalink
fix: strip parameters from data content types to assess if it's JSON …
Browse files Browse the repository at this point in the history
…format

Signed-off-by: Frederic Delechamp <[email protected]>
  • Loading branch information
fdelechamp-gw committed Sep 30, 2022
1 parent f08a099 commit 5a1c42d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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(;[\\s\\w\"!#$%&'*+.^_`|~=-]*)*$");
private final ObjectMapper mapper;
private final JsonFormatOptions options;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -151,6 +167,39 @@ void verifyDeserializeError(String inputFile){

}

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"),
Expand Down

0 comments on commit 5a1c42d

Please sign in to comment.