From cace60f57cc7e2b4c742a941861ecbd9b6b59cec Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Mon, 21 Oct 2024 15:58:17 -0700 Subject: [PATCH] Test --- .../mutable_url_request_test.dart | 7 +-- .../url_session_delegate_test.dart | 14 +++--- .../integration_test/url_session_test.dart | 2 +- .../cupertino_http/lib/src/cupertino_api.dart | 47 +++++++++++-------- .../lib/src/cupertino_web_socket.dart | 2 +- 5 files changed, 42 insertions(+), 30 deletions(-) diff --git a/pkgs/cupertino_http/example/integration_test/mutable_url_request_test.dart b/pkgs/cupertino_http/example/integration_test/mutable_url_request_test.dart index 6fde661f50..446b1ee95f 100644 --- a/pkgs/cupertino_http/example/integration_test/mutable_url_request_test.dart +++ b/pkgs/cupertino_http/example/integration_test/mutable_url_request_test.dart @@ -19,9 +19,10 @@ void main() { setUp(() => request = MutableURLRequest.fromUrl(uri)); test('set', () { - request.cachePolicy = NSURLRequestCachePolicy.NSURLRequestReturnCacheDataDontLoad; - expect( - request.cachePolicy, NSURLRequestCachePolicy.NSURLRequestReturnCacheDataDontLoad); + request.cachePolicy = + NSURLRequestCachePolicy.NSURLRequestReturnCacheDataDontLoad; + expect(request.cachePolicy, + NSURLRequestCachePolicy.NSURLRequestReturnCacheDataDontLoad); request.toString(); // Just verify that there is no crash. }); }); diff --git a/pkgs/cupertino_http/example/integration_test/url_session_delegate_test.dart b/pkgs/cupertino_http/example/integration_test/url_session_delegate_test.dart index a636098896..6baa5e6ca9 100644 --- a/pkgs/cupertino_http/example/integration_test/url_session_delegate_test.dart +++ b/pkgs/cupertino_http/example/integration_test/url_session_delegate_test.dart @@ -448,7 +448,7 @@ void testOnRedirect(URLSessionConfiguration Function() config) { .having( (r) => r.allHeaderFields['Location'], "r.allHeaderFields['Location']", - matches('http://localhost:${redirectServer.port}/' + + matches('http://localhost:${redirectServer.port}/' r'\d+')))); expect(error!.code, -1007); // kCFURLErrorHTTPTooManyRedirects session.finishTasksAndInvalidate(); @@ -698,11 +698,12 @@ void main() { group('backgroundSession', () { var count = 0; - final config = () { + URLSessionConfiguration config() { ++count; return URLSessionConfiguration.backgroundSession( 'backgroundSession{$count}'); - }; + } + testOnComplete(config); // onResponse is not called for background sessions. testOnData(config); @@ -712,7 +713,8 @@ void main() { }); group('defaultSessionConfiguration', () { - final config = () => URLSessionConfiguration.defaultSessionConfiguration(); + URLSessionConfiguration config() => + URLSessionConfiguration.defaultSessionConfiguration(); testOnComplete(config); testOnResponse(config); testOnData(config); @@ -723,8 +725,8 @@ void main() { }); group('ephemeralSessionConfiguration', () { - final config = - () => URLSessionConfiguration.ephemeralSessionConfiguration(); + URLSessionConfiguration config() => + URLSessionConfiguration.ephemeralSessionConfiguration(); testOnComplete(config); testOnResponse(config); testOnData(config); diff --git a/pkgs/cupertino_http/example/integration_test/url_session_test.dart b/pkgs/cupertino_http/example/integration_test/url_session_test.dart index 2fad014f08..96e09789f8 100644 --- a/pkgs/cupertino_http/example/integration_test/url_session_test.dart +++ b/pkgs/cupertino_http/example/integration_test/url_session_test.dart @@ -160,7 +160,7 @@ void testDataTaskWithCompletionHandler(URLSession session) { .having( (r) => r.allHeaderFields['Location'], "r.allHeaderFields['Location']", - matches('http://localhost:${redirectServer.port}/' + + matches('http://localhost:${redirectServer.port}/' r'\d+')))); expect(error!.code, -1007); // kCFURLErrorHTTPTooManyRedirects }); diff --git a/pkgs/cupertino_http/lib/src/cupertino_api.dart b/pkgs/cupertino_http/lib/src/cupertino_api.dart index 0c4b4c0204..99fb2ee041 100644 --- a/pkgs/cupertino_http/lib/src/cupertino_api.dart +++ b/pkgs/cupertino_http/lib/src/cupertino_api.dart @@ -32,6 +32,15 @@ import 'dart:isolate'; import 'package:objective_c/objective_c.dart' as objc; import 'native_cupertino_bindings.dart' as ncb; +import 'native_cupertino_bindings.dart' + show + NSHTTPCookieAcceptPolicy, + NSURLRequestCachePolicy, + NSURLRequestNetworkServiceType, + NSURLSessionMultipathServiceType, + NSURLSessionResponseDisposition, + NSURLSessionTaskState, + NSURLSessionWebSocketMessageType; import 'utils.dart'; export 'native_cupertino_bindings.dart' @@ -199,9 +208,9 @@ class URLSessionConfiguration /// What policy to use when deciding whether to accept cookies. /// /// See [NSURLSessionConfiguration.HTTPCookieAcceptPolicy](https://developer.apple.com/documentation/foundation/nsurlsessionconfiguration/1408933-httpcookieacceptpolicy). - ncb.NSHTTPCookieAcceptPolicy get httpCookieAcceptPolicy => + NSHTTPCookieAcceptPolicy get httpCookieAcceptPolicy => _nsObject.HTTPCookieAcceptPolicy; - set httpCookieAcceptPolicy(ncb.NSHTTPCookieAcceptPolicy value) => + set httpCookieAcceptPolicy(NSHTTPCookieAcceptPolicy value) => _nsObject.HTTPCookieAcceptPolicy = value; /// The maximum number of connections that a URLSession can have open to the @@ -230,26 +239,26 @@ class URLSessionConfiguration /// What type of Multipath TCP connections to use. /// /// See [NSURLSessionConfiguration.multipathServiceType](https://developer.apple.com/documentation/foundation/nsurlsessionconfiguration/2875967-multipathservicetype) - ncb.NSURLSessionMultipathServiceType get multipathServiceType => + NSURLSessionMultipathServiceType get multipathServiceType => _nsObject.multipathServiceType; - set multipathServiceType(ncb.NSURLSessionMultipathServiceType value) => + set multipathServiceType(NSURLSessionMultipathServiceType value) => _nsObject.multipathServiceType = value; /// Provides in indication to the operating system on what type of requests /// are being sent. /// /// See [NSURLSessionConfiguration.networkServiceType](https://developer.apple.com/documentation/foundation/nsurlsessionconfiguration/1411606-networkservicetype). - ncb.NSURLRequestNetworkServiceType get networkServiceType => + NSURLRequestNetworkServiceType get networkServiceType => _nsObject.networkServiceType; - set networkServiceType(ncb.NSURLRequestNetworkServiceType value) => + set networkServiceType(NSURLRequestNetworkServiceType value) => _nsObject.networkServiceType = value; /// Controls how to deal with response caching. /// /// See [NSURLSessionConfiguration.requestCachePolicy](https://developer.apple.com/documentation/foundation/nsurlsessionconfiguration/1411655-requestcachepolicy) - ncb.NSURLRequestCachePolicy get requestCachePolicy => + NSURLRequestCachePolicy get requestCachePolicy => _nsObject.requestCachePolicy; - set requestCachePolicy(ncb.NSURLRequestCachePolicy value) => + set requestCachePolicy(NSURLRequestCachePolicy value) => _nsObject.requestCachePolicy = value; /// Whether the app should be resumed when background tasks complete. @@ -407,7 +416,7 @@ class URLSessionWebSocketMessage /// The type of the WebSocket message. /// /// See [NSURLSessionWebSocketMessage.type](https://developer.apple.com/documentation/foundation/nsurlsessionwebsocketmessage/3181195-type) - ncb.NSURLSessionWebSocketMessageType get type => _nsObject.type; + NSURLSessionWebSocketMessageType get type => _nsObject.type; @override String toString() => @@ -444,7 +453,7 @@ class URLSessionTask extends _ObjectHolder { /// The current state of the task. /// /// See [NSURLSessionTask.state](https://developer.apple.com/documentation/foundation/nsurlsessiontask/1409888-state) - ncb.NSURLSessionTaskState get state => _nsObject.state; + NSURLSessionTaskState get state => _nsObject.state; /// The relative priority [0, 1] that the host should use to handle the /// request. @@ -679,7 +688,7 @@ class URLRequest extends _ObjectHolder { /// Controls how to deal with caching for the request. /// /// See [NSURLSession.cachePolicy](https://developer.apple.com/documentation/foundation/nsurlrequest/1407944-cachepolicy) - ncb.NSURLRequestCachePolicy get cachePolicy => _nsObject.cachePolicy; + NSURLRequestCachePolicy get cachePolicy => _nsObject.cachePolicy; /// The body of the request. /// @@ -742,7 +751,7 @@ class MutableURLRequest extends URLRequest { return MutableURLRequest._(ncb.NSMutableURLRequest.requestWithURL_(url)); } - set cachePolicy(ncb.NSURLRequestCachePolicy value) => + set cachePolicy(NSURLRequestCachePolicy value) => _mutableUrlRequest.cachePolicy = value; set httpBody(objc.NSData? data) { @@ -801,12 +810,10 @@ class URLSession extends _ObjectHolder { // `_taskCount` is decremented. When `_taskCount` is 0 then the `ReceivePort` // is closed. static var _taskCount = 0; - static ReceivePort? _port = null; + static ReceivePort? _port; static void _incrementTaskCount() { - if (_port == null) { - _port = ReceivePort(); - } + _port ??= ReceivePort(); ++_taskCount; } @@ -825,7 +832,7 @@ class URLSession extends _ObjectHolder { URLRequest? Function(URLSession session, URLSessionTask task, HTTPURLResponse response, URLRequest newRequest)? onRedirect, - ncb.NSURLSessionResponseDisposition Function( + NSURLSessionResponseDisposition Function( URLSession session, URLSessionTask task, URLResponse response)? onResponse, void Function(URLSession session, URLSessionTask task, objc.NSData error)? @@ -859,6 +866,7 @@ class URLSession extends _ObjectHolder { if (onRedirect != null) { ncb.NSURLSessionDataDelegate.addToBuilderAsListener(protoBuilder, + // ignore: lines_longer_than_80_chars URLSession_task_willPerformHTTPRedirection_newRequest_completionHandler_: (nsSession, nsTask, nsResponse, nsRequest, nsRequestCompleter) { final request = URLRequest._(nsRequest); @@ -909,6 +917,7 @@ class URLSession extends _ObjectHolder { if (onFinishedDownloading != null) { final asyncFinishDownloading = + // ignore: lines_longer_than_80_chars ncb.ObjCBlock_ffiVoid_NSCondition_NSURLSession_NSURLSessionDownloadTask_NSURL .listener((nsCondition, nsSession, nsTask, nsUrl) { try { @@ -987,7 +996,7 @@ class URLSession extends _ObjectHolder { /// [URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:](https://developer.apple.com/documentation/foundation/nsurlsessiontaskdelegate/1411626-urlsession) /// /// If [onResponse] is set then it will be called whenever a valid response - /// is received. The returned [URLSessionResponseDisposition] will decide + /// is received. The returned [NSURLSessionResponseDisposition] will decide /// how the content of the response is processed. See /// [URLSession:dataTask:didReceiveResponse:completionHandler:](https://developer.apple.com/documentation/foundation/nsurlsessiondatadelegate/1410027-urlsession) /// @@ -1018,7 +1027,7 @@ class URLSession extends _ObjectHolder { URLRequest? Function(URLSession session, URLSessionTask task, HTTPURLResponse response, URLRequest newRequest)? onRedirect, - ncb.NSURLSessionResponseDisposition Function( + NSURLSessionResponseDisposition Function( URLSession session, URLSessionTask task, URLResponse response)? onResponse, void Function(URLSession session, URLSessionTask task, objc.NSData data)? diff --git a/pkgs/cupertino_http/lib/src/cupertino_web_socket.dart b/pkgs/cupertino_http/lib/src/cupertino_web_socket.dart index d3bcafcd86..124e6c5ba2 100644 --- a/pkgs/cupertino_http/lib/src/cupertino_web_socket.dart +++ b/pkgs/cupertino_http/lib/src/cupertino_web_socket.dart @@ -6,8 +6,8 @@ import 'dart:async'; import 'dart:convert'; import 'dart:typed_data'; -import 'package:web_socket/web_socket.dart'; import 'package:objective_c/objective_c.dart' as objc; +import 'package:web_socket/web_socket.dart'; import 'cupertino_api.dart';