Skip to content

Commit 41c99a9

Browse files
authored
handle undefined datacontenttype both in cli and webconsole output (#29)
* handle undefined datacontenttype both in cli and webconsole output * omit mutating actual values * update express version * Quarkus: add unit test * change message for undefined datacontenttype
1 parent 23d460f commit 41c99a9

File tree

5 files changed

+85
-6
lines changed

5 files changed

+85
-6
lines changed

Diff for: expressjs/src/routes/events/printer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function printData(ce) {
116116
let buf = ''
117117
if (ce.data) {
118118
buf += `Data,\n`
119-
if (ce.datacontenttype === 'application/json') {
119+
if (ce.datacontenttype === null || ce.datacontenttype === 'application/json') {
120120
const data = indent(JSON.stringify(ce.data, ' ', 2), 1, 2)
121121
buf += `${data}\n`
122122
return buf

Diff for: frontend/src/components/left-column.tsx

+10-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class LeftColumn extends React.Component<LeftColumnProps, LeftColumnState> {
7272
<div className="value">{ce.source}</div>
7373
</td>
7474
<th rowSpan={2}>
75-
<label>{ce.datacontenttype}</label>
75+
<label>{this.getDataContentType(ce)}</label>
7676
<div className="value">{this.stringifyData(ce)}</div>
7777
</th>
7878
</tr>
@@ -94,8 +94,16 @@ class LeftColumn extends React.Component<LeftColumnProps, LeftColumnState> {
9494
})
9595
}
9696

97+
private getDataContentType(ce: CloudEvent): string {
98+
if (ce.datacontenttype == null) {
99+
return "content type not specified"
100+
} else {
101+
return ce.datacontenttype
102+
}
103+
}
104+
97105
private stringifyData(ce: CloudEvent): React.ReactNode {
98-
if (ce.datacontenttype?.startsWith('application/json')) {
106+
if (ce.datacontenttype == null || ce.datacontenttype?.startsWith('application/json')) {
99107
return this.highlightCode(JSON.stringify(ce.data, null, 2), 'json')
100108
}
101109
return (

Diff for: frontend/src/events/u-cloudevents.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ export class CloudEvent<T = any> implements CloudEventV1<T> {
3636
this.specversion = (properties.specversion as string) || '1.0'
3737
delete properties.specversion
3838

39-
this.datacontenttype = properties.datacontenttype
39+
if (properties.datacontenttype) {
40+
this.datacontenttype = properties.datacontenttype
41+
}
42+
4043
delete properties.datacontenttype
4144

4245
this.subject = properties.subject

Diff for: quarkus/src/main/java/com/redhat/openshift/knative/showcase/events/Presenter.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ private CharSequence printData(CloudEvent ce) throws IOException {
8686
if (data != null) {
8787
buf.append("Data,\n");
8888
var contentType = ce.getDataContentType();
89-
assert contentType != null;
9089

9190
if ("application/json".equals(contentType)) {
9291
var json = mapper.readValue(data.toBytes(), Object.class);
@@ -99,7 +98,7 @@ private CharSequence printData(CloudEvent ce) throws IOException {
9998
}
10099
var repr = data.toString();
101100
var types = List.of("text", "xml", "html", "csv", "json", "yaml");
102-
if (types.stream().anyMatch(contentType::contains)) {
101+
if (contentType == null || types.stream().anyMatch(contentType::contains)) {
103102
repr = new String(data.toBytes(), StandardCharsets.UTF_8);
104103
}
105104
buf.append(repr.indent(INDENT_SIZE)).append("\n");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.redhat.openshift.knative.showcase.events;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import io.cloudevents.core.builder.CloudEventBuilder;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
import java.net.URI;
8+
import java.nio.charset.StandardCharsets;
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
class PresenterTest {
12+
private Presenter presenter;
13+
14+
@BeforeEach
15+
void setup() {
16+
presenter = new Presenter(new ObjectMapper());
17+
}
18+
19+
@Test
20+
void testAsHumanReadableWithNullTypeAndJson() {
21+
// Arrange
22+
String id = "123";
23+
String type = null;
24+
String data = "{\"msg\":\"Hello World!\"}";
25+
var ce = CloudEventBuilder.v1()
26+
.withId(id)
27+
.withSource(URI.create("http://localhost"))
28+
.withType("test")
29+
.withData(type, data.getBytes(StandardCharsets.UTF_8))
30+
.build();
31+
32+
String expected = """
33+
☁️ cloudevents.Event
34+
Validation: valid
35+
Context Attributes,
36+
specversion: 1.0
37+
id: 123
38+
source: http://localhost
39+
type: test
40+
Data,
41+
{"msg":"Hello World!"}
42+
43+
""";
44+
// Act
45+
CharSequence actual = presenter.asHumanReadable(ce).toString();
46+
// Assert
47+
assertEquals(expected, actual);
48+
}
49+
50+
@Test
51+
void testAsHumanReadableWithNullTypeAndBinary() {
52+
// Arrange
53+
String id = "123";
54+
String type = null;
55+
byte[] data={0xa, 0x2, (byte) 0xff};
56+
var ce = CloudEventBuilder.v1()
57+
.withId(id)
58+
.withSource(URI.create("http://localhost"))
59+
.withType("test")
60+
.withData(type, data)
61+
.build();
62+
63+
byte[] expected = new byte[] {-30, -104, -127, -17, -72, -113, 32, 32, 99, 108, 111, 117, 100, 101, 118, 101, 110, 116, 115, 46, 69, 118, 101, 110, 116, 10, 86, 97, 108, 105, 100, 97, 116, 105, 111, 110, 58, 32, 118, 97, 108, 105, 100, 10, 67, 111, 110, 116, 101, 120, 116, 32, 65, 116, 116, 114, 105, 98, 117, 116, 101, 115, 44, 10, 32, 32, 115, 112, 101, 99, 118, 101, 114, 115, 105, 111, 110, 58, 32, 49, 46, 48, 10, 32, 32, 105, 100, 58, 32, 49, 50, 51, 10, 32, 32, 115, 111, 117, 114, 99, 101, 58, 32, 104, 116, 116, 112, 58, 47, 47, 108, 111, 99, 97, 108, 104, 111, 115, 116, 10, 32, 32, 116, 121, 112, 101, 58, 32, 116, 101, 115, 116, 10, 68, 97, 116, 97, 44, 10, 32, 32, 10, 32, 32, 2, -17, -65, -67, 10, 10};
64+
// Act
65+
byte[] actual = presenter.asHumanReadable(ce).toString().getBytes();
66+
// Assert
67+
assertArrayEquals(expected, actual);
68+
}
69+
}

0 commit comments

Comments
 (0)