|
| 1 | +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io'; |
| 6 | +import 'dart:typed_data'; |
| 7 | + |
| 8 | +import 'package:convert/convert.dart'; |
| 9 | +import 'package:crypto/crypto.dart'; |
| 10 | +import 'package:cupertino_http/cupertino_http.dart'; |
| 11 | +import 'package:flutter_test/flutter_test.dart'; |
| 12 | +import 'package:http/http.dart'; |
| 13 | +import 'package:integration_test/integration_test.dart'; |
| 14 | + |
| 15 | +void testClient(Client client) { |
| 16 | + group('client tests', () { |
| 17 | + late HttpServer server; |
| 18 | + late Uri uri; |
| 19 | + late List<int> serverHash; |
| 20 | + |
| 21 | + setUp(() async { |
| 22 | + server = (await HttpServer.bind('localhost', 0)) |
| 23 | + ..listen((request) async { |
| 24 | + var hashSink = AccumulatorSink<Digest>(); |
| 25 | + final hashConverter = sha1.startChunkedConversion(hashSink); |
| 26 | + await request.listen(hashConverter.add).asFuture<void>(); |
| 27 | + hashConverter.close(); |
| 28 | + serverHash = hashSink.events.single.bytes; |
| 29 | + await request.response.close(); |
| 30 | + }); |
| 31 | + uri = Uri.http('localhost:${server.port}'); |
| 32 | + }); |
| 33 | + tearDown(() { |
| 34 | + server.close(); |
| 35 | + }); |
| 36 | + |
| 37 | + test('large single item stream', () async { |
| 38 | + // This tests that `CUPHTTPStreamToNSInputStreamAdapter` correctly |
| 39 | + // handles calls to `read:maxLength:` where the maximum length |
| 40 | + // is smaller than the amount of data in the buffer. |
| 41 | + final size = (Platform.isIOS ? 10 : 100) * 1024 * 1024; |
| 42 | + final data = Uint8List(size); |
| 43 | + for (var i = 0; i < data.length; ++i) { |
| 44 | + data[i] = i % 256; |
| 45 | + } |
| 46 | + final request = StreamedRequest('POST', uri); |
| 47 | + request.sink.add(data); |
| 48 | + request.sink.close(); |
| 49 | + await client.send(request); |
| 50 | + expect(serverHash, sha1.convert(data).bytes); |
| 51 | + }); |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +void main() { |
| 56 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 57 | + |
| 58 | + group('defaultSessionConfiguration', () { |
| 59 | + testClient(CupertinoClient.defaultSessionConfiguration()); |
| 60 | + }); |
| 61 | + group('fromSessionConfiguration', () { |
| 62 | + final config = URLSessionConfiguration.ephemeralSessionConfiguration(); |
| 63 | + testClient(CupertinoClient.fromSessionConfiguration(config)); |
| 64 | + }); |
| 65 | +} |
0 commit comments