-
Notifications
You must be signed in to change notification settings - Fork 69
/
message_test.ts
288 lines (258 loc) · 11.9 KB
/
message_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import path from "path";
import fs from "fs";
import { expect } from "chai";
import { CloudEvent, CONSTANTS, Version } from "../../src";
import { asBase64 } from "../../src/event/validation";
import { Message, HTTP } from "../../src/message";
const type = "org.cncf.cloudevents.example";
const source = "urn:event:from:myapi/resource/123";
const time = new Date().toISOString();
const subject = "subject.ext";
const dataschema = "http://cloudevents.io/schema.json";
const datacontenttype = "application/json";
const id = "b46cf653-d48a-4b90-8dfa-355c01061361";
const data = {
foo: "bar",
};
// Attributes for v03 events
const schemaurl = "https://cloudevents.io/schema.json";
const ext1Name = "extension1";
const ext1Value = "foobar";
const ext2Name = "extension2";
const ext2Value = "acme";
// Binary data as base64
const dataBinary = Uint32Array.from(JSON.stringify(data), (c) => c.codePointAt(0) as number);
// Since the above is a special case (string as binary), let's test
// with a real binary file one is likely to encounter in the wild
const imageData = new Uint32Array(fs.readFileSync(path.join(process.cwd(), "test", "integration", "ce.png")));
const image_base64 = asBase64(imageData);
describe("HTTP transport", () => {
it("Can detect invalid CloudEvent Messages", () => {
// Create a message that is not an actual event
const message: Message = {
body: "Hello world!",
headers: {
"Content-type": "text/plain",
},
};
expect(HTTP.isEvent(message)).to.be.false;
});
it("Can detect valid CloudEvent Messages", () => {
// Now create a message that is an event
const message = HTTP.binary(
new CloudEvent({
source: "/message-test",
type: "example",
data,
}),
);
expect(HTTP.isEvent(message)).to.be.true;
});
// Allow for external systems to send bad events - do what we can
// to accept them
it("Does not throw an exception when converting an invalid Message to a CloudEvent", () => {
const message: Message = {
body: `"hello world"`,
headers: {
"content-type": "application/json",
"ce-id": "1234",
"ce-type": "example.bad.event",
"ce-specversion": "1.0",
// no required ce-source header, thus an invalid event
},
};
const event = HTTP.toEvent(message);
expect(event).to.be.instanceOf(CloudEvent);
// ensure that we actually now have an invalid event
expect(event.validate).to.throw;
});
it("Does not allow an invalid CloudEvent to be converted to a Message", () => {
const badEvent = new CloudEvent(
{
source: "/example.source",
type: "", // type is required, empty string will throw with strict validation
},
false, // turn off strict validation
);
expect(() => {
HTTP.binary(badEvent);
}).to.throw;
expect(() => {
HTTP.structured(badEvent);
}).to.throw;
});
describe("Specification version V1", () => {
const fixture: CloudEvent = new CloudEvent({
specversion: Version.V1,
id,
type,
source,
datacontenttype,
subject,
time,
dataschema,
data,
[ext1Name]: ext1Value,
[ext2Name]: ext2Value,
});
it("Binary Messages can be created from a CloudEvent", () => {
const message: Message = HTTP.binary(fixture);
expect(message.body).to.equal(JSON.stringify(data));
// validate all headers
expect(message.headers[CONSTANTS.HEADER_CONTENT_TYPE]).to.equal(datacontenttype);
expect(message.headers[CONSTANTS.CE_HEADERS.SPEC_VERSION]).to.equal(Version.V1);
expect(message.headers[CONSTANTS.CE_HEADERS.ID]).to.equal(id);
expect(message.headers[CONSTANTS.CE_HEADERS.TYPE]).to.equal(type);
expect(message.headers[CONSTANTS.CE_HEADERS.SOURCE]).to.equal(source);
expect(message.headers[CONSTANTS.CE_HEADERS.SUBJECT]).to.equal(subject);
expect(message.headers[CONSTANTS.CE_HEADERS.TIME]).to.equal(fixture.time);
expect(message.headers[CONSTANTS.BINARY_HEADERS_1.DATA_SCHEMA]).to.equal(dataschema);
expect(message.headers[`ce-${ext1Name}`]).to.equal(ext1Value);
expect(message.headers[`ce-${ext2Name}`]).to.equal(ext2Value);
});
it("Structured Messages can be created from a CloudEvent", () => {
const message: Message = HTTP.structured(fixture);
expect(message.headers[CONSTANTS.HEADER_CONTENT_TYPE]).to.equal(CONSTANTS.DEFAULT_CE_CONTENT_TYPE);
// Parse the message body as JSON, then validate the attributes
const body = JSON.parse(message.body as string);
expect(body[CONSTANTS.CE_ATTRIBUTES.SPEC_VERSION]).to.equal(Version.V1);
expect(body[CONSTANTS.CE_ATTRIBUTES.ID]).to.equal(id);
expect(body[CONSTANTS.CE_ATTRIBUTES.TYPE]).to.equal(type);
expect(body[CONSTANTS.CE_ATTRIBUTES.SOURCE]).to.equal(source);
expect(body[CONSTANTS.CE_ATTRIBUTES.SUBJECT]).to.equal(subject);
expect(body[CONSTANTS.CE_ATTRIBUTES.TIME]).to.equal(fixture.time);
expect(body[CONSTANTS.STRUCTURED_ATTRS_1.DATA_SCHEMA]).to.equal(dataschema);
expect(body[ext1Name]).to.equal(ext1Value);
expect(body[ext2Name]).to.equal(ext2Value);
});
it("A CloudEvent can be converted from a binary Message", () => {
const message = HTTP.binary(fixture);
const event = HTTP.toEvent(message);
expect(event).to.deep.equal(fixture);
});
it("A CloudEvent can be converted from a structured Message", () => {
const message = HTTP.structured(fixture);
const event = HTTP.toEvent(message);
expect(event).to.deep.equal(fixture);
});
it("Converts binary data to base64 when serializing structured messages", () => {
const event = fixture.cloneWith({ data: imageData, datacontenttype: "image/png" });
expect(event.data).to.equal(imageData);
const message = HTTP.structured(event);
const messageBody = JSON.parse(message.body as string);
expect(messageBody.data_base64).to.equal(image_base64);
});
it("Converts base64 encoded data to binary when deserializing structured messages", () => {
const message = HTTP.structured(fixture.cloneWith({ data: imageData, datacontenttype: "image/png" }));
const eventDeserialized = HTTP.toEvent(message);
expect(eventDeserialized.data).to.deep.equal(imageData);
expect(eventDeserialized.data_base64).to.equal(image_base64);
});
it("Parses binary data from structured messages with content type application/json", () => {
const message = HTTP.structured(fixture.cloneWith({ data: dataBinary }));
const eventDeserialized = HTTP.toEvent(message);
expect(eventDeserialized.data).to.deep.equal({ foo: "bar" });
expect(eventDeserialized.data_base64).to.be.undefined;
});
it("Converts base64 encoded data to binary when deserializing binary messages", () => {
const message = HTTP.binary(fixture.cloneWith({ data: imageData, datacontenttype: "image/png" }));
const eventDeserialized = HTTP.toEvent(message);
expect(eventDeserialized.data).to.deep.equal(imageData);
expect(eventDeserialized.data_base64).to.equal(image_base64);
});
it("Keeps binary data binary when serializing binary messages", () => {
const event = fixture.cloneWith({ data: dataBinary });
expect(event.data).to.equal(dataBinary);
const message = HTTP.binary(event);
expect(message.body).to.equal(dataBinary);
});
it("Parses binary data from binary messages with content type application/json", () => {
const message = HTTP.binary(fixture.cloneWith({ data: dataBinary }));
const eventDeserialized = HTTP.toEvent(message);
expect(eventDeserialized.data).to.deep.equal({ foo: "bar" });
expect(eventDeserialized.data_base64).to.be.undefined;
});
});
describe("Specification version V03", () => {
const fixture: CloudEvent = new CloudEvent({
specversion: Version.V03,
id,
type,
source,
datacontenttype,
subject,
time,
schemaurl,
data,
[ext1Name]: ext1Value,
[ext2Name]: ext2Value,
});
it("Binary Messages can be created from a CloudEvent", () => {
const message: Message = HTTP.binary(fixture);
expect(message.body).to.equal(JSON.stringify(data));
// validate all headers
expect(message.headers[CONSTANTS.HEADER_CONTENT_TYPE]).to.equal(datacontenttype);
expect(message.headers[CONSTANTS.CE_HEADERS.SPEC_VERSION]).to.equal(Version.V03);
expect(message.headers[CONSTANTS.CE_HEADERS.ID]).to.equal(id);
expect(message.headers[CONSTANTS.CE_HEADERS.TYPE]).to.equal(type);
expect(message.headers[CONSTANTS.CE_HEADERS.SOURCE]).to.equal(source);
expect(message.headers[CONSTANTS.CE_HEADERS.SUBJECT]).to.equal(subject);
expect(message.headers[CONSTANTS.CE_HEADERS.TIME]).to.equal(fixture.time);
expect(message.headers[CONSTANTS.BINARY_HEADERS_03.SCHEMA_URL]).to.equal(schemaurl);
expect(message.headers[`ce-${ext1Name}`]).to.equal(ext1Value);
expect(message.headers[`ce-${ext2Name}`]).to.equal(ext2Value);
});
it("Structured Messages can be created from a CloudEvent", () => {
const message: Message = HTTP.structured(fixture);
expect(message.headers[CONSTANTS.HEADER_CONTENT_TYPE]).to.equal(CONSTANTS.DEFAULT_CE_CONTENT_TYPE);
// Parse the message body as JSON, then validate the attributes
const body = JSON.parse(message.body as string);
expect(body[CONSTANTS.CE_ATTRIBUTES.SPEC_VERSION]).to.equal(Version.V03);
expect(body[CONSTANTS.CE_ATTRIBUTES.ID]).to.equal(id);
expect(body[CONSTANTS.CE_ATTRIBUTES.TYPE]).to.equal(type);
expect(body[CONSTANTS.CE_ATTRIBUTES.SOURCE]).to.equal(source);
expect(body[CONSTANTS.CE_ATTRIBUTES.SUBJECT]).to.equal(subject);
expect(body[CONSTANTS.CE_ATTRIBUTES.TIME]).to.equal(fixture.time);
expect(body[CONSTANTS.STRUCTURED_ATTRS_03.SCHEMA_URL]).to.equal(schemaurl);
expect(body[ext1Name]).to.equal(ext1Value);
expect(body[ext2Name]).to.equal(ext2Value);
});
it("A CloudEvent can be converted from a binary Message", () => {
const message = HTTP.binary(fixture);
const event = HTTP.toEvent(message);
expect(event).to.deep.equal(fixture);
});
it("A CloudEvent can be converted from a structured Message", () => {
const message = HTTP.structured(fixture);
const event = HTTP.toEvent(message);
expect(event).to.deep.equal(fixture);
});
it("Converts binary data to base64 when serializing structured messages", () => {
const event = fixture.cloneWith({ data: imageData, datacontenttype: "image/png" });
expect(event.data).to.equal(imageData);
const message = HTTP.structured(event);
const messageBody = JSON.parse(message.body as string);
expect(messageBody.data_base64).to.equal(image_base64);
});
it("Converts base64 encoded data to binary when deserializing structured messages", () => {
// Creating an event with binary data automatically produces base64 encoded data
// which is then set as the 'data' attribute on the message body
const message = HTTP.structured(fixture.cloneWith({ data: imageData, datacontenttype: "image/png" }));
const eventDeserialized = HTTP.toEvent(message);
expect(eventDeserialized.data).to.deep.equal(imageData);
expect(eventDeserialized.data_base64).to.equal(image_base64);
});
it("Converts base64 encoded data to binary when deserializing binary messages", () => {
const message = HTTP.binary(fixture.cloneWith({ data: imageData, datacontenttype: "image/png" }));
const eventDeserialized = HTTP.toEvent(message);
expect(eventDeserialized.data).to.deep.equal(imageData);
expect(eventDeserialized.data_base64).to.equal(image_base64);
});
it("Keeps binary data binary when serializing binary messages", () => {
const event = fixture.cloneWith({ data: dataBinary });
expect(event.data).to.equal(dataBinary);
const message = HTTP.binary(event);
expect(message.body).to.equal(dataBinary);
});
});
});