From 4ce78c1f68ac1fb550ba4e41f3cdc6297ccccdc7 Mon Sep 17 00:00:00 2001 From: provokateurin Date: Fri, 30 Aug 2024 09:13:02 +0200 Subject: [PATCH] chore(nextcloud)!: Remove deprecated userAgent and cookieJar parameters from NextcloudClient Signed-off-by: provokateurin --- packages/neon_framework/example/pubspec.lock | 8 - .../nextcloud/lib/src/nextcloud_client.dart | 18 +-- .../lib/src/utils/cookie_jar_client.dart | 77 ---------- packages/nextcloud/lib/src/utils/utils.dart | 1 - packages/nextcloud/pubspec.yaml | 1 - .../nextcloud/test/nextcloud_client_test.dart | 140 +----------------- .../test/utils/cookie_jar_client_test.dart | 22 --- 7 files changed, 6 insertions(+), 261 deletions(-) delete mode 100644 packages/nextcloud/lib/src/utils/cookie_jar_client.dart delete mode 100644 packages/nextcloud/test/utils/cookie_jar_client_test.dart diff --git a/packages/neon_framework/example/pubspec.lock b/packages/neon_framework/example/pubspec.lock index 919626b3cd0..89102372397 100644 --- a/packages/neon_framework/example/pubspec.lock +++ b/packages/neon_framework/example/pubspec.lock @@ -193,14 +193,6 @@ packages: url: "https://pub.dev" source: hosted version: "3.1.1" - cookie_jar: - dependency: transitive - description: - name: cookie_jar - sha256: a6ac027d3ed6ed756bfce8f3ff60cb479e266f3b0fdabd6242b804b6765e52de - url: "https://pub.dev" - source: hosted - version: "4.0.8" cookie_store: dependency: "direct overridden" description: diff --git a/packages/nextcloud/lib/src/nextcloud_client.dart b/packages/nextcloud/lib/src/nextcloud_client.dart index e8755522e14..4b62efe649c 100644 --- a/packages/nextcloud/lib/src/nextcloud_client.dart +++ b/packages/nextcloud/lib/src/nextcloud_client.dart @@ -1,8 +1,5 @@ -import 'package:cookie_jar/cookie_jar.dart' as cookie_jar; import 'package:dynamite_runtime/http_client.dart'; import 'package:http/http.dart' as http; -import 'package:nextcloud/src/utils/cookie_jar_client.dart'; -import 'package:universal_io/io.dart'; /// A client configuring the clients for all Nextcloud APIs. /// @@ -24,21 +21,8 @@ class NextcloudClient extends DynamiteClient with http.BaseClient { String? loginName, String? password, String? appPassword, - @Deprecated('Use a custom http client to set the user agent.') String? userAgent, http.Client? httpClient, - @Deprecated('Use a custom http client to persist cookies.') cookie_jar.CookieJar? cookieJar, }) { - var client = httpClient ?? http.Client(); - if (cookieJar != null || userAgent != null) { - client = CookieJarClient( - httpClient: httpClient, - cookieJar: cookieJar, - baseHeaders: { - if (userAgent != null) HttpHeaders.userAgentHeader: userAgent, - }, - ); - } - final authentications = [ if (appPassword != null) DynamiteHttpBearerAuthentication( @@ -53,7 +37,7 @@ class NextcloudClient extends DynamiteClient with http.BaseClient { return NextcloudClient._( baseURL, - httpClient: client, + httpClient: httpClient, authentications: authentications, ); } diff --git a/packages/nextcloud/lib/src/utils/cookie_jar_client.dart b/packages/nextcloud/lib/src/utils/cookie_jar_client.dart deleted file mode 100644 index 6204e891e23..00000000000 --- a/packages/nextcloud/lib/src/utils/cookie_jar_client.dart +++ /dev/null @@ -1,77 +0,0 @@ -import 'package:cookie_jar/cookie_jar.dart' as cookie_jar; -import 'package:http/http.dart' as http; -import 'package:meta/meta.dart'; -import 'package:universal_io/io.dart'; - -/// An [http.Client] that persists cookies in the given [cookieJar]. -/// -/// The [baseHeaders] will be attached to every request. -/// The headers of a request will override any header in the `baseHeaders`. -@internal -class CookieJarClient with http.BaseClient { - /// Creates a new http client with a [cookieJar] persisting cookies. - CookieJarClient({ - this.baseHeaders, - this.cookieJar, - http.Client? httpClient, - }) : httpClient = httpClient ?? http.Client(); - - /// The base http client. - final http.Client httpClient; - - /// The optional cookie jar to persist the response cookies. - final cookie_jar.CookieJar? cookieJar; - - /// The base headers added to each request. - final Map? baseHeaders; - - /// Sends an HTTP request and asynchronously returns the response. - /// - /// Cookies are persisted in the [cookieJar] and loaded for requests. - @override - Future send(http.BaseRequest request) async { - if (cookieJar != null) { - final cookies = await cookieJar!.loadForRequest(request.url); - if (cookies.isNotEmpty) { - final buffer = StringBuffer(); - - for (final entry in cookies.indexed) { - final cookie = entry.$2; - - buffer - ..write(cookie.name) - ..write('=') - ..write(cookie.value); - - if (entry.$1 < cookies.length - 1) { - buffer.write('; '); - } - } - - request.headers['cookie'] = buffer.toString(); - } - } - - // Do not overwrite request headers to avoid invalid requests. - baseHeaders?.forEach((key, value) { - request.headers.putIfAbsent(key, () => value); - }); - - final response = await httpClient.send(request); - - final cookieHeader = response.headersSplitValues['set-cookie']; - if (cookieHeader != null && cookieJar != null) { - final cookies = cookieHeader.map(Cookie.fromSetCookieValue).toList(); - await cookieJar!.saveFromResponse(request.url, cookies); - } - - return response; - } - - @override - void close() { - httpClient.close(); - - super.close(); - } -} diff --git a/packages/nextcloud/lib/src/utils/utils.dart b/packages/nextcloud/lib/src/utils/utils.dart index c028dcc11c8..1f992885cb8 100644 --- a/packages/nextcloud/lib/src/utils/utils.dart +++ b/packages/nextcloud/lib/src/utils/utils.dart @@ -1,3 +1,2 @@ -export 'cookie_jar_client.dart'; export 'date_time.dart'; export 'http_date_parser.dart'; diff --git a/packages/nextcloud/pubspec.yaml b/packages/nextcloud/pubspec.yaml index 4b19cd4c9f8..15a27878644 100644 --- a/packages/nextcloud/pubspec.yaml +++ b/packages/nextcloud/pubspec.yaml @@ -15,7 +15,6 @@ dependencies: built_collection: ^5.0.0 built_value: ^8.9.0 collection: ^1.0.0 - cookie_jar: ^4.0.7 crypto: ^3.0.0 crypton: ^2.0.0 dynamite_runtime: ^0.5.0 diff --git a/packages/nextcloud/test/nextcloud_client_test.dart b/packages/nextcloud/test/nextcloud_client_test.dart index c37d849c8a3..2523a7321c2 100644 --- a/packages/nextcloud/test/nextcloud_client_test.dart +++ b/packages/nextcloud/test/nextcloud_client_test.dart @@ -1,140 +1,10 @@ -import 'package:cookie_jar/cookie_jar.dart'; -import 'package:http/http.dart'; -import 'package:http/testing.dart'; import 'package:http_client_conformance_tests/http_client_conformance_tests.dart'; import 'package:nextcloud/nextcloud.dart'; -import 'package:test/test.dart'; void main() { - final uri = Uri.parse('http://example.com'); - group(NextcloudClient, () { - group( - 'Client conformance tests', - () { - testAll( - () => NextcloudClient(Uri()), - canReceiveSetCookieHeaders: true, - canSendCookieHeaders: true, - ); - }, - onPlatform: const { - 'browser': [Skip()], - }, - ); - - group('Cookies', () { - late CookieJar cookieJar; - setUp(() { - cookieJar = CookieJar(); - }); - - test('Cookies', () async { - final mockedClient = MockClient((request) async { - expect(request.headers['cookie'], equals('a=b; a2=b2; a3=b3')); - - return Response( - '', - 200, - headers: { - 'set-cookie': Cookie('c', 'd').toString(), - }, - ); - }); - - final client = NextcloudClient( - uri, - httpClient: mockedClient, - // ignore: deprecated_member_use_from_same_package - cookieJar: cookieJar, - ); - - await cookieJar.saveFromResponse(uri, [ - Cookie('a', 'b'), - Cookie('a2', 'b2'), - Cookie('a3', 'b3'), - ]); - await client.get(uri); - - final cookies = await cookieJar.loadForRequest(uri); - expect(cookies, hasLength(4)); - expect(cookies[0].name, 'a'); - expect(cookies[0].value, 'b'); - expect(cookies.last.name, 'c'); - expect(cookies.last.value, 'd'); - }); - - test('No cookies', () async { - final mockedClient = MockClient((request) async { - expect(request.headers['cookie'], isNull); - return Response('', 200); - }); - - final client = NextcloudClient( - uri, - httpClient: mockedClient, - // ignore: deprecated_member_use_from_same_package - cookieJar: cookieJar, - ); - - await client.get(uri); - }); - }); - - group('headers', () { - test('raw request base headers', () async { - final mockedClient = MockClient((request) async { - expect( - request.headers, - equals({ - 'user-agent': 'Neon', - }), - ); - - return Response( - '', - 200, - ); - }); - - final client = NextcloudClient( - uri, - httpClient: mockedClient, - // ignore: deprecated_member_use_from_same_package - userAgent: 'Neon', - ); - - await client.get(uri); - }); - - test('request overwrites base headers', () async { - final mockedClient = MockClient((request) async { - expect( - request.headers, - equals({ - 'user-agent': 'Cookbook', - }), - ); - - return Response( - '', - 200, - ); - }); - - final client = NextcloudClient( - uri, - httpClient: mockedClient, - // ignore: deprecated_member_use_from_same_package - userAgent: 'Neon', - ); - - await client.get( - uri, - headers: { - 'user-agent': 'Cookbook', - }, - ); - }); - }); - }); + testAll( + () => NextcloudClient(Uri()), + canReceiveSetCookieHeaders: true, + canSendCookieHeaders: true, + ); } diff --git a/packages/nextcloud/test/utils/cookie_jar_client_test.dart b/packages/nextcloud/test/utils/cookie_jar_client_test.dart deleted file mode 100644 index edf983c5dfc..00000000000 --- a/packages/nextcloud/test/utils/cookie_jar_client_test.dart +++ /dev/null @@ -1,22 +0,0 @@ -import 'package:cookie_jar/cookie_jar.dart'; -import 'package:http_client_conformance_tests/http_client_conformance_tests.dart'; -import 'package:nextcloud/src/utils/cookie_jar_client.dart'; -import 'package:test/test.dart'; - -void main() { - group( - 'Client conformance tests', - () { - testAll( - () => CookieJarClient( - cookieJar: CookieJar(), - ), - canReceiveSetCookieHeaders: true, - canSendCookieHeaders: true, - ); - }, - onPlatform: const { - 'browser': [Skip()], - }, - ); -}