|
1 | 1 | import 'dart:convert';
|
| 2 | +import 'dart:typed_data'; |
2 | 3 |
|
3 | 4 | import 'package:functions_client/src/functions_client.dart';
|
4 | 5 | import 'package:functions_client/src/types.dart';
|
@@ -152,6 +153,214 @@ void main() {
|
152 | 153 | expect(req.body, '{"thekey":"thevalue"}');
|
153 | 154 | expect(req.headers["Content-Type"], contains("application/json"));
|
154 | 155 | });
|
| 156 | + |
| 157 | + test('Uint8List is properly encoded as binary data', () async { |
| 158 | + final binaryData = Uint8List.fromList([1, 2, 3, 4, 5]); |
| 159 | + await functionsCustomHttpClient.invoke('function', body: binaryData); |
| 160 | + |
| 161 | + final req = customHttpClient.receivedRequests.last; |
| 162 | + expect(req, isA<Request>()); |
| 163 | + |
| 164 | + req as Request; |
| 165 | + expect(req.bodyBytes, equals(binaryData)); |
| 166 | + expect(req.headers["Content-Type"], equals("application/octet-stream")); |
| 167 | + }); |
| 168 | + |
| 169 | + test('null body sends no content-type', () async { |
| 170 | + await functionsCustomHttpClient.invoke('function'); |
| 171 | + |
| 172 | + final req = customHttpClient.receivedRequests.last; |
| 173 | + expect(req, isA<Request>()); |
| 174 | + |
| 175 | + req as Request; |
| 176 | + expect(req.body, ''); |
| 177 | + expect(req.headers.containsKey("Content-Type"), isFalse); |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + group('HTTP methods', () { |
| 182 | + test('GET method', () async { |
| 183 | + await functionsCustomHttpClient.invoke( |
| 184 | + 'function', |
| 185 | + method: HttpMethod.get, |
| 186 | + ); |
| 187 | + |
| 188 | + final req = customHttpClient.receivedRequests.last; |
| 189 | + expect(req.method, 'get'); |
| 190 | + }); |
| 191 | + |
| 192 | + test('PUT method', () async { |
| 193 | + await functionsCustomHttpClient.invoke( |
| 194 | + 'function', |
| 195 | + method: HttpMethod.put, |
| 196 | + ); |
| 197 | + |
| 198 | + final req = customHttpClient.receivedRequests.last; |
| 199 | + expect(req.method, 'put'); |
| 200 | + }); |
| 201 | + |
| 202 | + test('DELETE method', () async { |
| 203 | + await functionsCustomHttpClient.invoke( |
| 204 | + 'function', |
| 205 | + method: HttpMethod.delete, |
| 206 | + ); |
| 207 | + |
| 208 | + final req = customHttpClient.receivedRequests.last; |
| 209 | + expect(req.method, 'delete'); |
| 210 | + }); |
| 211 | + |
| 212 | + test('PATCH method', () async { |
| 213 | + await functionsCustomHttpClient.invoke( |
| 214 | + 'function', |
| 215 | + method: HttpMethod.patch, |
| 216 | + ); |
| 217 | + |
| 218 | + final req = customHttpClient.receivedRequests.last; |
| 219 | + expect(req.method, 'patch'); |
| 220 | + }); |
| 221 | + }); |
| 222 | + |
| 223 | + group('Headers', () { |
| 224 | + test('setAuth updates authorization header', () async { |
| 225 | + functionsCustomHttpClient.setAuth('new-token'); |
| 226 | + |
| 227 | + await functionsCustomHttpClient.invoke('function'); |
| 228 | + |
| 229 | + final req = customHttpClient.receivedRequests.last; |
| 230 | + expect(req.headers['Authorization'], 'Bearer new-token'); |
| 231 | + }); |
| 232 | + |
| 233 | + test('headers getter returns current headers', () { |
| 234 | + functionsCustomHttpClient.setAuth('test-token'); |
| 235 | + |
| 236 | + final headers = functionsCustomHttpClient.headers; |
| 237 | + expect(headers['Authorization'], 'Bearer test-token'); |
| 238 | + expect(headers, contains('X-Client-Info')); |
| 239 | + }); |
| 240 | + |
| 241 | + test('custom headers override defaults', () async { |
| 242 | + await functionsCustomHttpClient.invoke( |
| 243 | + 'function', |
| 244 | + headers: {'Content-Type': 'custom/type'}, |
| 245 | + ); |
| 246 | + |
| 247 | + final req = customHttpClient.receivedRequests.last; |
| 248 | + expect(req.headers['Content-Type'], 'custom/type'); |
| 249 | + }); |
| 250 | + |
| 251 | + test('custom headers merge with defaults', () async { |
| 252 | + await functionsCustomHttpClient.invoke( |
| 253 | + 'function', |
| 254 | + headers: {'X-Custom': 'value'}, |
| 255 | + ); |
| 256 | + |
| 257 | + final req = customHttpClient.receivedRequests.last; |
| 258 | + expect(req.headers['X-Custom'], 'value'); |
| 259 | + expect(req.headers, contains('X-Client-Info')); |
| 260 | + }); |
| 261 | + }); |
| 262 | + |
| 263 | + group('Constructor variations', () { |
| 264 | + test('constructor with all parameters', () { |
| 265 | + final isolate = YAJsonIsolate(); |
| 266 | + final httpClient = CustomHttpClient(); |
| 267 | + final client = FunctionsClient( |
| 268 | + 'https://example.com', |
| 269 | + {'X-Test': 'value'}, |
| 270 | + httpClient: httpClient, |
| 271 | + isolate: isolate, |
| 272 | + ); |
| 273 | + |
| 274 | + expect(client.headers['X-Test'], 'value'); |
| 275 | + expect(client.headers, contains('X-Client-Info')); |
| 276 | + }); |
| 277 | + |
| 278 | + test('constructor with minimal parameters', () { |
| 279 | + final client = FunctionsClient('https://example.com', {}); |
| 280 | + |
| 281 | + expect(client.headers, contains('X-Client-Info')); |
| 282 | + }); |
| 283 | + }); |
| 284 | + |
| 285 | + group('Multipart requests', () { |
| 286 | + test('multipart with both files and fields', () async { |
| 287 | + await functionsCustomHttpClient.invoke( |
| 288 | + 'function', |
| 289 | + body: {'field1': 'value1', 'field2': 'value2'}, |
| 290 | + files: [ |
| 291 | + MultipartFile.fromString('file1', 'content1'), |
| 292 | + MultipartFile.fromString('file2', 'content2'), |
| 293 | + ], |
| 294 | + ); |
| 295 | + |
| 296 | + final req = customHttpClient.receivedRequests.last; |
| 297 | + expect(req.headers['Content-Type'], contains('multipart/form-data')); |
| 298 | + expect(req, isA<MultipartRequest>()); |
| 299 | + }); |
| 300 | + |
| 301 | + test('multipart with only files', () async { |
| 302 | + await functionsCustomHttpClient.invoke( |
| 303 | + 'function', |
| 304 | + files: [MultipartFile.fromString('file', 'content')], |
| 305 | + ); |
| 306 | + |
| 307 | + final req = customHttpClient.receivedRequests.last; |
| 308 | + expect(req.headers['Content-Type'], contains('multipart/form-data')); |
| 309 | + expect(req, isA<MultipartRequest>()); |
| 310 | + }); |
| 311 | + }); |
| 312 | + |
| 313 | + group('Response content types', () { |
| 314 | + test('handles application/octet-stream response', () async { |
| 315 | + final res = await functionsCustomHttpClient.invoke('binary'); |
| 316 | + |
| 317 | + expect(res.data, isA<Uint8List>()); |
| 318 | + expect(res.data, equals(Uint8List.fromList([1, 2, 3, 4, 5]))); |
| 319 | + expect(res.status, 200); |
| 320 | + }); |
| 321 | + |
| 322 | + test('handles text/plain response', () async { |
| 323 | + final res = await functionsCustomHttpClient.invoke('text'); |
| 324 | + |
| 325 | + expect(res.data, isA<String>()); |
| 326 | + expect(res.data, 'Hello World'); |
| 327 | + expect(res.status, 200); |
| 328 | + }); |
| 329 | + |
| 330 | + test('handles empty JSON response', () async { |
| 331 | + final res = await functionsCustomHttpClient.invoke('empty-json'); |
| 332 | + |
| 333 | + expect(res.data, ''); |
| 334 | + expect(res.status, 200); |
| 335 | + }); |
| 336 | + }); |
| 337 | + |
| 338 | + group('Error handling', () { |
| 339 | + test('FunctionException contains all error details', () async { |
| 340 | + try { |
| 341 | + await functionsCustomHttpClient.invoke('error-function'); |
| 342 | + fail('should throw'); |
| 343 | + } on FunctionException catch (e) { |
| 344 | + expect(e.status, 420); |
| 345 | + expect(e.details, isNotNull); |
| 346 | + expect(e.reasonPhrase, isNotNull); |
| 347 | + expect(e.toString(), contains('420')); |
| 348 | + } |
| 349 | + }); |
| 350 | + }); |
| 351 | + |
| 352 | + group('Edge cases', () { |
| 353 | + test('multipart request with invalid body type throws assertion', |
| 354 | + () async { |
| 355 | + expect( |
| 356 | + () => functionsCustomHttpClient.invoke( |
| 357 | + 'function', |
| 358 | + body: 42, // Invalid: should be Map<String, String> for multipart |
| 359 | + files: [MultipartFile.fromString('file', 'content')], |
| 360 | + ), |
| 361 | + throwsA(isA<AssertionError>()), |
| 362 | + ); |
| 363 | + }); |
155 | 364 | });
|
156 | 365 | });
|
157 | 366 | }
|
0 commit comments