8000 Strip parameters from data content types to assess if it's JSON format (#484) by fredoboulo · Pull Request #485 · cloudevents/sdk-java · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -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;

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