Skip to content

Commit 544109c

Browse files
authored
test: add test for onCall and onCallWithData (#81)
1 parent 314c83b commit 544109c

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

test/e2e/e2e_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'helpers/pubsub_client.dart';
1616
import 'helpers/storage_client.dart';
1717
import 'tests/database_tests.dart';
1818
import 'tests/firestore_tests.dart';
19+
import 'tests/https_oncall_tests.dart';
1920
import 'tests/https_onrequest_tests.dart';
2021
import 'tests/identity_tests.dart';
2122
import 'tests/integration_tests.dart';
@@ -119,6 +120,7 @@ void main() {
119120

120121
// Run all test groups (pass closures to defer value access)
121122
runHttpsOnRequestTests(() => client, () => emulator);
123+
runHttpsOnCallTests(() => client);
122124
runIntegrationTests(() => examplePath);
123125
runPubSubTests(() => examplePath, () => pubsubClient, () => emulator);
124126
runFirestoreTests(() => examplePath, () => firestoreClient, () => emulator);
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
import 'dart:convert';
2+
3+
import 'package:test/test.dart';
4+
5+
import '../helpers/http_client.dart';
6+
7+
/// HTTPS onCall and onCallWithData test group
8+
void runHttpsOnCallTests(FunctionsHttpClient Function() getClient) {
9+
group('HTTPS onCall', () {
10+
late FunctionsHttpClient client;
11+
12+
setUpAll(() {
13+
client = getClient();
14+
});
15+
16+
test('greet returns expected response with name', () async {
17+
final response = await client.call('greet', data: {'name': 'Dart'});
18+
19+
expect(response.statusCode, equals(200));
20+
21+
final json = jsonDecode(response.body) as Map<String, dynamic>;
22+
expect(
23+
json['result'],
24+
equals(<String, dynamic>{'message': 'Hello, Dart!'}),
25+
);
26+
});
27+
28+
test('greet returns default name when no name provided', () async {
29+
final response = await client.call('greet', data: <String, dynamic>{});
30+
31+
expect(response.statusCode, equals(200));
32+
33+
final json = jsonDecode(response.body) as Map<String, dynamic>;
34+
expect(
35+
json['result'],
36+
equals(<String, dynamic>{'message': 'Hello, World!'}),
37+
);
38+
});
39+
40+
test('greet returns correct content type', () async {
41+
final response = await client.call('greet', data: {'name': 'Test'});
42+
43+
expect(response.statusCode, equals(200));
44+
expect(response.headers['content-type'], contains('application/json'));
45+
});
46+
47+
test('divide returns correct result', () async {
48+
final response = await client.call('divide', data: {'a': 10, 'b': 2});
49+
50+
expect(response.statusCode, equals(200));
51+
52+
final json = jsonDecode(response.body) as Map<String, dynamic>;
53+
expect(json['result'], equals(<String, dynamic>{'result': 5.0}));
54+
});
55+
56+
test('divide throws INVALID_ARGUMENT when args missing', () async {
57+
final response = await client.call('divide', data: <String, dynamic>{});
58+
59+
expect(response.statusCode, equals(400));
60+
61+
final json = jsonDecode(response.body) as Map<String, dynamic>;
62+
final error = json['error'] as Map<String, dynamic>;
63+
expect(error['status'], equals('INVALID_ARGUMENT'));
64+
expect(error['message'], contains('required'));
65+
});
66+
67+
test('divide throws FAILED_PRECONDITION on divide by zero', () async {
68+
final response = await client.call('divide', data: {'a': 10, 'b': 0});
69+
70+
expect(response.statusCode, equals(400));
71+
72+
final json = jsonDecode(response.body) as Map<String, dynamic>;
73+
final error = json['error'] as Map<String, dynamic>;
74+
expect(error['status'], equals('FAILED_PRECONDITION'));
75+
expect(error['message'], contains('divide by zero'));
76+
});
77+
78+
test('getAuthInfo returns unauthenticated when no auth token', () async {
79+
final response = await client.call(
80+
'get-auth-info',
81+
data: <String, dynamic>{},
82+
);
83+
84+
expect(response.statusCode, equals(200));
85+
86+
final json = jsonDecode(response.body) as Map<String, dynamic>;
87+
final result = json['result'] as Map<String, dynamic>;
88+
expect(result['authenticated'], equals(false));
89+
});
90+
91+
test('callable rejects GET requests', () async {
92+
final response = await client.get('greet');
93+
94+
// Callable functions only accept POST
95+
expect(response.statusCode, isNot(equals(200)));
96+
});
97+
98+
test('callable rejects invalid content type', () async {
99+
final response = await client.post(
100+
'greet',
101+
body: {'data': 'test'},
102+
headers: {'Content-Type': 'text/plain'},
103+
);
104+
105+
expect(response.statusCode, equals(400));
106+
});
107+
108+
test('countdown returns result without streaming', () async {
109+
final response = await client.call('countdown', data: {'start': 3});
110+
111+
expect(response.statusCode, equals(200));
112+
113+
final json = jsonDecode(response.body) as Map<String, dynamic>;
114+
expect(
115+
json['result'],
116+
equals(<String, dynamic>{'message': 'Countdown complete!'}),
117+
);
118+
});
119+
});
120+
121+
group('HTTPS onCallWithData', () {
122+
late FunctionsHttpClient client;
123+
124+
setUpAll(() {
125+
client = getClient();
126+
});
127+
128+
test('greetTyped returns expected response with name', () async {
129+
final response = await client.call('greet-typed', data: {'name': 'Dart'});
130+
131+
expect(response.statusCode, equals(200));
132+
133+
final json = jsonDecode(response.body) as Map<String, dynamic>;
134+
expect(
135+
json['result'],
136+
equals(<String, dynamic>{'message': 'Hello, Dart!'}),
137+
);
138+
});
139+
140+
test('greetTyped uses default name when not provided', () async {
141+
final response = await client.call(
142+
'greet-typed',
143+
data: <String, dynamic>{},
144+
);
145+
146+
expect(response.statusCode, equals(200));
147+
148+
final json = jsonDecode(response.body) as Map<String, dynamic>;
149+
expect(
150+
json['result'],
151+
equals(<String, dynamic>{'message': 'Hello, World!'}),
152+
);
153+
});
154+
155+
test('greetTyped returns correct content type', () async {
156+
final response = await client.call('greet-typed', data: {'name': 'Test'});
157+
158+
expect(response.statusCode, equals(200));
159+
expect(response.headers['content-type'], contains('application/json'));
160+
});
161+
162+
test('greetTyped rejects GET requests', () async {
163+
final response = await client.get('greet-typed');
164+
165+
expect(response.statusCode, isNot(equals(200)));
166+
});
167+
});
168+
}

0 commit comments

Comments
 (0)