Skip to content

Commit 8e19a1a

Browse files
committed
Add unit test for binary messages
Towards #1
1 parent 82084a1 commit 8e19a1a

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

test/hello/bin/server.dart

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/hello/lib/functions.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ Future<Response> function(Request request) async {
100100
}
101101
}
102102

103+
@CloudFunction()
104+
void basicCloudEventHandler(CloudEvent event) {
105+
stderr.writeln(_encoder.convert(event));
106+
}
107+
103108
@CloudFunction()
104109
Future<Response> conformanceHttp(Request request) async {
105110
final content = await request.readAsString();

test/hello/test/function_test.dart

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,65 @@ void main() {
308308
});
309309
});
310310

311-
group('cloudevent function tests', () {});
311+
group('cloudevent function tests', () {
312+
test('binary-mode message', () async {
313+
final proc = await _start(arguments: [
314+
'--target',
315+
'basicCloudEventHandler',
316+
'--signature-type',
317+
'cloudevent',
318+
]);
319+
320+
const requestUrl = 'http://localhost:$_defaultPort/';
321+
322+
const body = r'''
323+
{
324+
"subscription": "projects/my-project/subscriptions/my-subscription",
325+
"message": {
326+
"@type": "type.googleapis.com/google.pubsub.v1.PubsubMessage",
327+
"attributes": {
328+
"attr1":"attr1-value"
329+
},
330+
"data": "dGVzdCBtZXNzYWdlIDM="
331+
}
332+
}''';
333+
334+
final response = await post(
335+
requestUrl,
336+
body: body,
337+
headers: {
338+
'Content-Type': 'application/json; charset=utf-8',
339+
'ce-specversion': '1.0',
340+
'ce-type': 'google.cloud.pubsub.topic.publish',
341+
'ce-time': '2020-09-05T03:56:24Z',
342+
'ce-id': '1234-1234-1234',
343+
},
344+
);
345+
expect(response.statusCode, 200);
346+
expect(response.body, isEmpty);
347+
348+
await _finish(
349+
proc,
350+
requestOutput: endsWith('POST [200] /'),
351+
);
352+
353+
final stderrOutput = await proc.stderrStream().join('\n');
354+
355+
final json = jsonDecode(stderrOutput) as Map<String, dynamic>;
356+
357+
expect(
358+
json,
359+
{
360+
'id': '1234-1234-1234',
361+
'specversion': '1.0',
362+
'type': 'google.cloud.pubsub.topic.publish',
363+
'datacontenttype': 'application/json; charset=utf-8',
364+
'time': '2020-09-05T03:56:24.000Z',
365+
'data': jsonDecode(body),
366+
},
367+
);
368+
});
369+
});
312370
}
313371

314372
Future<TestProcess> _start({

0 commit comments

Comments
 (0)