Skip to content

Commit e2b1310

Browse files
authored
Throw exceptions when atempting to handling unsupport CloudEvent formats. (#362)
- Modfy logic that selects between structured and binary modes during reception. - Introduced new test scenarios to veify behavior. Signed-off-by: Day, Jeremy(jday) <[email protected]>
1 parent 5e3bfc8 commit e2b1310

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

core/src/main/java/io/cloudevents/core/message/impl/MessageUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ public static MessageReader parseStructuredOrBinaryMessage(
5959
EventFormat format = EventFormatProvider.getInstance().resolveFormat(ct);
6060
if (format != null) {
6161
return structuredMessageFactory.apply(format);
62+
} else {
63+
/**
64+
* The format wasn't one we support, but if it's part of the
65+
* CloudEvent family it indicates it's a structured
66+
* representation that we can't interpret.
67+
*/
68+
if (ct.startsWith("application/cloudevents")) {
69+
throw newUnknownEncodingException();
70+
}
6271
}
6372
}
6473

core/src/test/java/io/cloudevents/core/message/impl/MessageUtilsTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ void parseStructuredOrBinaryMessage_Exception() {
2727
.isEqualTo(CloudEventRWException.CloudEventRWExceptionKind.UNKNOWN_ENCODING);
2828
}
2929

30+
31+
/**
32+
* Verify an exception is thrown if an unsupported
33+
* application/cloudevents content-type family is
34+
* received.
35+
*/
36+
@ParameterizedTest
37+
@MethodSource
38+
void testBadContentTypes(String contentType) {
39+
40+
CloudEventRWException exception = assertThrows(CloudEventRWException.class, () ->
41+
{
42+
parseStructuredOrBinaryMessage(() -> contentType, eventFormat -> null, () -> "1.0", specVersion -> null);
43+
});
44+
45+
assertThat(exception.getKind()).isEqualTo(CloudEventRWException.CloudEventRWExceptionKind.UNKNOWN_ENCODING);
46+
47+
}
48+
3049
@Test
3150
void testParseStructuredOrBinaryMessage_StructuredMode() {
3251
MessageUtils.parseStructuredOrBinaryMessage(() -> "application/cloudevents+csv;",
@@ -54,4 +73,11 @@ private static Stream<Arguments> testParseStructuredOrBinaryMessage_BinaryMode()
5473
);
5574
}
5675

76+
private static Stream<Arguments> testBadContentTypes() {
77+
return Stream.of(
78+
Arguments.of("application/cloudevents"),
79+
Arguments.of("application/cloudevents+morse")
80+
);
81+
}
82+
5783
}

0 commit comments

Comments
 (0)