From 9ba547198bcdf36a0e6d6b6838356264fd24dcc1 Mon Sep 17 00:00:00 2001 From: Seif Ashraf Date: Wed, 13 Mar 2024 15:56:38 +0200 Subject: [PATCH 01/37] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2ce0382dd2..03fbc455bf 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ A composable, Future-based library for making HTTP requests. easy to consume HTTP resources. It's multi-platform, and supports mobile, desktop, and the browser. -## Packages +## Packages | Package | Description | Version | |---|---|---| From 3a26def4aab94d4c4d02aef7e82ca1cacb27fe82 Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Sun, 2 Feb 2025 23:40:01 +0200 Subject: [PATCH 02/37] Added caching options for http package --- pkgs/http/lib/src/base_request.dart | 7 ++++++- pkgs/http/lib/src/browser_client.dart | 1 + pkgs/http/lib/src/multipart_request.dart | 15 ++++++++++++++- pkgs/http/lib/src/request.dart | 13 +++++++++++++ pkgs/http/lib/src/streamed_request.dart | 13 +++++++++++++ pkgs/http/lib/src/utils.dart | 12 ++++++++++++ 6 files changed, 59 insertions(+), 2 deletions(-) diff --git a/pkgs/http/lib/src/base_request.dart b/pkgs/http/lib/src/base_request.dart index 4b165c7587..b9363b7e3a 100644 --- a/pkgs/http/lib/src/base_request.dart +++ b/pkgs/http/lib/src/base_request.dart @@ -165,4 +165,9 @@ abstract class BaseRequest { @override String toString() => '$method $url'; -} + + /// [cache] property for caching the network request. + /// + /// Defaults to `default`. + String? get cache => HttpCacheUtils.defaultType; +} \ No newline at end of file diff --git a/pkgs/http/lib/src/browser_client.dart b/pkgs/http/lib/src/browser_client.dart index f19bddb464..b385e18465 100644 --- a/pkgs/http/lib/src/browser_client.dart +++ b/pkgs/http/lib/src/browser_client.dart @@ -69,6 +69,7 @@ class BrowserClient extends BaseClient { RequestInit( method: request.method, body: bodyBytes.isNotEmpty ? bodyBytes.toJS : null, + cache: request.cache!, credentials: withCredentials ? 'include' : 'same-origin', headers: { if (request.contentLength case final contentLength?) diff --git a/pkgs/http/lib/src/multipart_request.dart b/pkgs/http/lib/src/multipart_request.dart index 79525421fb..7bb7cdd012 100644 --- a/pkgs/http/lib/src/multipart_request.dart +++ b/pkgs/http/lib/src/multipart_request.dart @@ -159,4 +159,17 @@ class MultipartRequest extends BaseRequest { growable: false); return '$prefix${String.fromCharCodes(list)}'; } -} + + @override + String? get cache { + if(this._cache != null){ + return this._cache; + } + else return super.cache; + } + + String? _cache; + set cache(String? cacheType){ + this._cache = cacheType!; + } +} \ No newline at end of file diff --git a/pkgs/http/lib/src/request.dart b/pkgs/http/lib/src/request.dart index c15e55169d..535eb2271f 100644 --- a/pkgs/http/lib/src/request.dart +++ b/pkgs/http/lib/src/request.dart @@ -181,4 +181,17 @@ class Request extends BaseRequest { if (!finalized) return; throw StateError("Can't modify a finalized Request."); } + + @override + String? get cache { + if(this._cache != null){ + return this._cache; + } + else return super.cache; + } + + String? _cache; + set cache(String? cacheType){ + this._cache = cacheType!; + } } diff --git a/pkgs/http/lib/src/streamed_request.dart b/pkgs/http/lib/src/streamed_request.dart index d10386e263..77a62c44de 100644 --- a/pkgs/http/lib/src/streamed_request.dart +++ b/pkgs/http/lib/src/streamed_request.dart @@ -52,4 +52,17 @@ class StreamedRequest extends BaseRequest { super.finalize(); return ByteStream(_controller.stream); } + + @override + String? get cache { + if(this._cache != null){ + return this._cache; + } + else return super.cache; + } + + String? _cache; + set cache(String? cacheType){ + this._cache = cacheType!; + } } diff --git a/pkgs/http/lib/src/utils.dart b/pkgs/http/lib/src/utils.dart index 72ec1529f2..bd6a7796a8 100644 --- a/pkgs/http/lib/src/utils.dart +++ b/pkgs/http/lib/src/utils.dart @@ -68,3 +68,15 @@ Stream onDone(Stream stream, void Function() onDone) => sink.close(); onDone(); })); + +/// Caching utilities types for using cache for any http request. +/// +/// For more references, check (Caching types)[https://developer.mozilla.org/en-US/docs/Web/API/Request/cache] +mixin HttpCacheUtils{ + static const String defaultType = "default"; + static const String reloadType = "reload"; + static const String noStoringType = "no-store"; + static const String noCachingType = "no-cache"; + static const String forceCachingType = "force-cache"; + static const String onlyIfCachedType = "only-if-cached"; +} \ No newline at end of file From 3ba65df0d5c2a1262f01f27ad96a243c72176fbf Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Mon, 3 Feb 2025 00:26:22 +0200 Subject: [PATCH 03/37] Enabled lints --- pkgs/http/lib/src/base_request.dart | 2 +- pkgs/http/lib/src/multipart_request.dart | 10 +++++----- pkgs/http/lib/src/request.dart | 8 ++++---- pkgs/http/lib/src/streamed_request.dart | 8 ++++---- pkgs/http/lib/src/utils.dart | 4 ++-- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/pkgs/http/lib/src/base_request.dart b/pkgs/http/lib/src/base_request.dart index b9363b7e3a..2b67a423bd 100644 --- a/pkgs/http/lib/src/base_request.dart +++ b/pkgs/http/lib/src/base_request.dart @@ -170,4 +170,4 @@ abstract class BaseRequest { /// /// Defaults to `default`. String? get cache => HttpCacheUtils.defaultType; -} \ No newline at end of file +} diff --git a/pkgs/http/lib/src/multipart_request.dart b/pkgs/http/lib/src/multipart_request.dart index 7bb7cdd012..520fbb0352 100644 --- a/pkgs/http/lib/src/multipart_request.dart +++ b/pkgs/http/lib/src/multipart_request.dart @@ -162,14 +162,14 @@ class MultipartRequest extends BaseRequest { @override String? get cache { - if(this._cache != null){ + if (this._cache != null) { return this._cache; - } - else return super.cache; + } else + return super.cache; } String? _cache; - set cache(String? cacheType){ + set cache(String? cacheType) { this._cache = cacheType!; } -} \ No newline at end of file +} diff --git a/pkgs/http/lib/src/request.dart b/pkgs/http/lib/src/request.dart index 535eb2271f..45a2bb6d7f 100644 --- a/pkgs/http/lib/src/request.dart +++ b/pkgs/http/lib/src/request.dart @@ -184,14 +184,14 @@ class Request extends BaseRequest { @override String? get cache { - if(this._cache != null){ + if (this._cache != null) { return this._cache; - } - else return super.cache; + } else + return super.cache; } String? _cache; - set cache(String? cacheType){ + set cache(String? cacheType) { this._cache = cacheType!; } } diff --git a/pkgs/http/lib/src/streamed_request.dart b/pkgs/http/lib/src/streamed_request.dart index 77a62c44de..efe3a51d34 100644 --- a/pkgs/http/lib/src/streamed_request.dart +++ b/pkgs/http/lib/src/streamed_request.dart @@ -55,14 +55,14 @@ class StreamedRequest extends BaseRequest { @override String? get cache { - if(this._cache != null){ + if (this._cache != null) { return this._cache; - } - else return super.cache; + } else + return super.cache; } String? _cache; - set cache(String? cacheType){ + set cache(String? cacheType) { this._cache = cacheType!; } } diff --git a/pkgs/http/lib/src/utils.dart b/pkgs/http/lib/src/utils.dart index bd6a7796a8..68863d10dd 100644 --- a/pkgs/http/lib/src/utils.dart +++ b/pkgs/http/lib/src/utils.dart @@ -72,11 +72,11 @@ Stream onDone(Stream stream, void Function() onDone) => /// Caching utilities types for using cache for any http request. /// /// For more references, check (Caching types)[https://developer.mozilla.org/en-US/docs/Web/API/Request/cache] -mixin HttpCacheUtils{ +mixin HttpCacheUtils { static const String defaultType = "default"; static const String reloadType = "reload"; static const String noStoringType = "no-store"; static const String noCachingType = "no-cache"; static const String forceCachingType = "force-cache"; static const String onlyIfCachedType = "only-if-cached"; -} \ No newline at end of file +} From f5c9c11fab5afea244e2022642a970e8f2d2caba Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Mon, 3 Feb 2025 00:31:39 +0200 Subject: [PATCH 04/37] Fixed CI errors --- pkgs/http/lib/src/multipart_request.dart | 10 +++++----- pkgs/http/lib/src/request.dart | 10 +++++----- pkgs/http/lib/src/streamed_request.dart | 10 +++++----- pkgs/http/lib/src/utils.dart | 12 ++++++------ 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/pkgs/http/lib/src/multipart_request.dart b/pkgs/http/lib/src/multipart_request.dart index 520fbb0352..7c1a998e87 100644 --- a/pkgs/http/lib/src/multipart_request.dart +++ b/pkgs/http/lib/src/multipart_request.dart @@ -162,14 +162,14 @@ class MultipartRequest extends BaseRequest { @override String? get cache { - if (this._cache != null) { - return this._cache; - } else - return super.cache; + if (_cache != null) { + return _cache; + } + return super.cache; } String? _cache; set cache(String? cacheType) { - this._cache = cacheType!; + _cache = cacheType!; } } diff --git a/pkgs/http/lib/src/request.dart b/pkgs/http/lib/src/request.dart index 45a2bb6d7f..bec4ce7d72 100644 --- a/pkgs/http/lib/src/request.dart +++ b/pkgs/http/lib/src/request.dart @@ -184,14 +184,14 @@ class Request extends BaseRequest { @override String? get cache { - if (this._cache != null) { - return this._cache; - } else - return super.cache; + if (_cache != null) { + return _cache; + } + return super.cache; } String? _cache; set cache(String? cacheType) { - this._cache = cacheType!; + _cache = cacheType!; } } diff --git a/pkgs/http/lib/src/streamed_request.dart b/pkgs/http/lib/src/streamed_request.dart index efe3a51d34..41070a9bcb 100644 --- a/pkgs/http/lib/src/streamed_request.dart +++ b/pkgs/http/lib/src/streamed_request.dart @@ -55,14 +55,14 @@ class StreamedRequest extends BaseRequest { @override String? get cache { - if (this._cache != null) { - return this._cache; - } else - return super.cache; + if (_cache != null) { + return _cache; + } + return super.cache; } String? _cache; set cache(String? cacheType) { - this._cache = cacheType!; + _cache = cacheType!; } } diff --git a/pkgs/http/lib/src/utils.dart b/pkgs/http/lib/src/utils.dart index 68863d10dd..3acff57cfc 100644 --- a/pkgs/http/lib/src/utils.dart +++ b/pkgs/http/lib/src/utils.dart @@ -73,10 +73,10 @@ Stream onDone(Stream stream, void Function() onDone) => /// /// For more references, check (Caching types)[https://developer.mozilla.org/en-US/docs/Web/API/Request/cache] mixin HttpCacheUtils { - static const String defaultType = "default"; - static const String reloadType = "reload"; - static const String noStoringType = "no-store"; - static const String noCachingType = "no-cache"; - static const String forceCachingType = "force-cache"; - static const String onlyIfCachedType = "only-if-cached"; + static const String defaultType = 'default'; + static const String reloadType = 'reload'; + static const String noStoringType = 'no-store'; + static const String noCachingType = 'no-cache'; + static const String forceCachingType = 'force-cache'; + static const String onlyIfCachedType = 'only-if-cached'; } From 52ff2c55bd52812af43f8224a02133ab1efaad60 Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Mon, 3 Feb 2025 00:41:29 +0200 Subject: [PATCH 05/37] Changed from HttpCacheUtils to HttpCacheOptions --- pkgs/http/lib/src/base_request.dart | 2 +- pkgs/http/lib/src/utils.dart | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkgs/http/lib/src/base_request.dart b/pkgs/http/lib/src/base_request.dart index 2b67a423bd..577baf5bc7 100644 --- a/pkgs/http/lib/src/base_request.dart +++ b/pkgs/http/lib/src/base_request.dart @@ -169,5 +169,5 @@ abstract class BaseRequest { /// [cache] property for caching the network request. /// /// Defaults to `default`. - String? get cache => HttpCacheUtils.defaultType; + String? get cache => HttpCacheOptions.defaultType; } diff --git a/pkgs/http/lib/src/utils.dart b/pkgs/http/lib/src/utils.dart index 3acff57cfc..f263bdff0a 100644 --- a/pkgs/http/lib/src/utils.dart +++ b/pkgs/http/lib/src/utils.dart @@ -72,7 +72,7 @@ Stream onDone(Stream stream, void Function() onDone) => /// Caching utilities types for using cache for any http request. /// /// For more references, check (Caching types)[https://developer.mozilla.org/en-US/docs/Web/API/Request/cache] -mixin HttpCacheUtils { +mixin HttpCacheOptions { static const String defaultType = 'default'; static const String reloadType = 'reload'; static const String noStoringType = 'no-store'; From 7f704343647ed24179bd32ee0b9e1e7ccf1bd880 Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Mon, 3 Feb 2025 20:30:59 +0200 Subject: [PATCH 06/37] Reverted readme file --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 458a7b0d8f..78127a7e06 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ A composable, Future-based library for making HTTP requests. easy to consume HTTP resources. It's multi-platform, and supports mobile, desktop, and the browser. -## Packages +## Packages | Package | Description | Version | |---|---|---| From ae7be97fad654f19ff5dfa5448975c4e28ffeb72 Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Mon, 3 Feb 2025 22:08:16 +0200 Subject: [PATCH 07/37] Reverted to last commits for request files --- pkgs/http/lib/src/base_request.dart | 5 ----- pkgs/http/lib/src/multipart_request.dart | 13 ------------- pkgs/http/lib/src/request.dart | 13 ------------- pkgs/http/lib/src/streamed_request.dart | 13 ------------- 4 files changed, 44 deletions(-) diff --git a/pkgs/http/lib/src/base_request.dart b/pkgs/http/lib/src/base_request.dart index 577baf5bc7..4b165c7587 100644 --- a/pkgs/http/lib/src/base_request.dart +++ b/pkgs/http/lib/src/base_request.dart @@ -165,9 +165,4 @@ abstract class BaseRequest { @override String toString() => '$method $url'; - - /// [cache] property for caching the network request. - /// - /// Defaults to `default`. - String? get cache => HttpCacheOptions.defaultType; } diff --git a/pkgs/http/lib/src/multipart_request.dart b/pkgs/http/lib/src/multipart_request.dart index 7c1a998e87..79525421fb 100644 --- a/pkgs/http/lib/src/multipart_request.dart +++ b/pkgs/http/lib/src/multipart_request.dart @@ -159,17 +159,4 @@ class MultipartRequest extends BaseRequest { growable: false); return '$prefix${String.fromCharCodes(list)}'; } - - @override - String? get cache { - if (_cache != null) { - return _cache; - } - return super.cache; - } - - String? _cache; - set cache(String? cacheType) { - _cache = cacheType!; - } } diff --git a/pkgs/http/lib/src/request.dart b/pkgs/http/lib/src/request.dart index bec4ce7d72..c15e55169d 100644 --- a/pkgs/http/lib/src/request.dart +++ b/pkgs/http/lib/src/request.dart @@ -181,17 +181,4 @@ class Request extends BaseRequest { if (!finalized) return; throw StateError("Can't modify a finalized Request."); } - - @override - String? get cache { - if (_cache != null) { - return _cache; - } - return super.cache; - } - - String? _cache; - set cache(String? cacheType) { - _cache = cacheType!; - } } diff --git a/pkgs/http/lib/src/streamed_request.dart b/pkgs/http/lib/src/streamed_request.dart index 41070a9bcb..d10386e263 100644 --- a/pkgs/http/lib/src/streamed_request.dart +++ b/pkgs/http/lib/src/streamed_request.dart @@ -52,17 +52,4 @@ class StreamedRequest extends BaseRequest { super.finalize(); return ByteStream(_controller.stream); } - - @override - String? get cache { - if (_cache != null) { - return _cache; - } - return super.cache; - } - - String? _cache; - set cache(String? cacheType) { - _cache = cacheType!; - } } From 1ca0c3acdec610f36fceaaaa1871d1c4e649436c Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Mon, 3 Feb 2025 22:09:25 +0200 Subject: [PATCH 08/37] Reformated the committed files --- pkgs/http/lib/src/browser_client.dart | 10 ++++++++-- pkgs/http/lib/src/utils.dart | 23 ++++++++++++++++------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/pkgs/http/lib/src/browser_client.dart b/pkgs/http/lib/src/browser_client.dart index b385e18465..4171c2811e 100644 --- a/pkgs/http/lib/src/browser_client.dart +++ b/pkgs/http/lib/src/browser_client.dart @@ -4,7 +4,7 @@ import 'dart:async'; import 'dart:js_interop'; - +import 'package:http/src/utils.dart' show CacheOption; import 'package:web/web.dart' show AbortController, @@ -45,6 +45,10 @@ BaseClient createClient() { class BrowserClient extends BaseClient { final _abortController = AbortController(); + BrowserClient([CacheOption? cacheOption]) { + _cacheOption = cacheOption?.cacheType ?? CacheOption.defaultType.cacheType; + } + /// Whether to send credentials such as cookies or authorization headers for /// cross-site requests. /// @@ -53,6 +57,8 @@ class BrowserClient extends BaseClient { bool _isClosed = false; + String? _cacheOption; + /// Sends an HTTP request and asynchronously returns the response. @override Future send(BaseRequest request) async { @@ -69,7 +75,7 @@ class BrowserClient extends BaseClient { RequestInit( method: request.method, body: bodyBytes.isNotEmpty ? bodyBytes.toJS : null, - cache: request.cache!, + cache: _cacheOption!, credentials: withCredentials ? 'include' : 'same-origin', headers: { if (request.contentLength case final contentLength?) diff --git a/pkgs/http/lib/src/utils.dart b/pkgs/http/lib/src/utils.dart index f263bdff0a..2dc812cb04 100644 --- a/pkgs/http/lib/src/utils.dart +++ b/pkgs/http/lib/src/utils.dart @@ -72,11 +72,20 @@ Stream onDone(Stream stream, void Function() onDone) => /// Caching utilities types for using cache for any http request. /// /// For more references, check (Caching types)[https://developer.mozilla.org/en-US/docs/Web/API/Request/cache] -mixin HttpCacheOptions { - static const String defaultType = 'default'; - static const String reloadType = 'reload'; - static const String noStoringType = 'no-store'; - static const String noCachingType = 'no-cache'; - static const String forceCachingType = 'force-cache'; - static const String onlyIfCachedType = 'only-if-cached'; +enum CacheOption { + defaultType('default'), + reload('reload'), + noStore('no_store'), + noCache('no_cache'), + forceCache('force_cache'), + onlyIfCached('only_if_cached'); + + final String cacheType; + + const CacheOption(this.cacheType); + + static CacheOption fromString(String cacheType) => values.firstWhere( + (v) => v.cacheType == cacheType, + orElse: () => CacheOption.defaultType, + ); } From 7a5c30d2347c7bdf681727a23996945064ba558a Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Mon, 3 Feb 2025 22:14:51 +0200 Subject: [PATCH 09/37] Changed values --- pkgs/http/lib/src/utils.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkgs/http/lib/src/utils.dart b/pkgs/http/lib/src/utils.dart index 2dc812cb04..334954357f 100644 --- a/pkgs/http/lib/src/utils.dart +++ b/pkgs/http/lib/src/utils.dart @@ -75,10 +75,10 @@ Stream onDone(Stream stream, void Function() onDone) => enum CacheOption { defaultType('default'), reload('reload'), - noStore('no_store'), - noCache('no_cache'), - forceCache('force_cache'), - onlyIfCached('only_if_cached'); + noStore('no-store'), + noCache('no-cache'), + forceCache('force-cache'), + onlyIfCached('only-if-cached'); final String cacheType; From 85daade9f019c196bde2a7676ed58bd068ead306 Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Mon, 3 Feb 2025 22:20:18 +0200 Subject: [PATCH 10/37] Passing CI stages --- pkgs/http/lib/src/browser_client.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/http/lib/src/browser_client.dart b/pkgs/http/lib/src/browser_client.dart index 4171c2811e..5357e7dc40 100644 --- a/pkgs/http/lib/src/browser_client.dart +++ b/pkgs/http/lib/src/browser_client.dart @@ -4,7 +4,6 @@ import 'dart:async'; import 'dart:js_interop'; -import 'package:http/src/utils.dart' show CacheOption; import 'package:web/web.dart' show AbortController, @@ -18,6 +17,7 @@ import 'base_client.dart'; import 'base_request.dart'; import 'exception.dart'; import 'streamed_response.dart'; +import 'utils.dart'; /// Create a [BrowserClient]. /// From 7ece5aadb00fce1a3433856db02f1a4d8b13102a Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Tue, 4 Feb 2025 22:05:28 +0200 Subject: [PATCH 11/37] Changed the parameter to String type --- pkgs/http/lib/src/browser_client.dart | 22 ++++++++++++++++------ pkgs/http/lib/src/utils.dart | 12 +++++------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/pkgs/http/lib/src/browser_client.dart b/pkgs/http/lib/src/browser_client.dart index 5357e7dc40..22da881f71 100644 --- a/pkgs/http/lib/src/browser_client.dart +++ b/pkgs/http/lib/src/browser_client.dart @@ -17,7 +17,6 @@ import 'base_client.dart'; import 'base_request.dart'; import 'exception.dart'; import 'streamed_response.dart'; -import 'utils.dart'; /// Create a [BrowserClient]. /// @@ -45,9 +44,17 @@ BaseClient createClient() { class BrowserClient extends BaseClient { final _abortController = AbortController(); - BrowserClient([CacheOption? cacheOption]) { - _cacheOption = cacheOption?.cacheType ?? CacheOption.defaultType.cacheType; - } + /// Create a new browser-based HTTP Client. + /// + /// If [cacheMode] is provided then it can be used to cache the request + /// in the browser. + /// + /// For example: + /// ```dart + /// const mode = 'reload'; + /// final client = BrowserClient(cacheMode: mode); + /// ``` + BrowserClient({this.cacheMode = 'default'}); /// Whether to send credentials such as cookies or authorization headers for /// cross-site requests. @@ -57,7 +64,10 @@ class BrowserClient extends BaseClient { bool _isClosed = false; - String? _cacheOption; + /// Use different caching mode for a HTTP request. + /// Defaults to `default`. + // https://developer.mozilla.org/en-US/docs/Web/API/Request/cache + String cacheMode; /// Sends an HTTP request and asynchronously returns the response. @override @@ -75,7 +85,7 @@ class BrowserClient extends BaseClient { RequestInit( method: request.method, body: bodyBytes.isNotEmpty ? bodyBytes.toJS : null, - cache: _cacheOption!, + cache: cacheMode, credentials: withCredentials ? 'include' : 'same-origin', headers: { if (request.contentLength case final contentLength?) diff --git a/pkgs/http/lib/src/utils.dart b/pkgs/http/lib/src/utils.dart index 334954357f..0d48cb57b9 100644 --- a/pkgs/http/lib/src/utils.dart +++ b/pkgs/http/lib/src/utils.dart @@ -69,10 +69,8 @@ Stream onDone(Stream stream, void Function() onDone) => onDone(); })); -/// Caching utilities types for using cache for any http request. -/// -/// For more references, check (Caching types)[https://developer.mozilla.org/en-US/docs/Web/API/Request/cache] -enum CacheOption { +/// Caching modes for any http request in a browser. +enum CacheMode { defaultType('default'), reload('reload'), noStore('no-store'), @@ -82,10 +80,10 @@ enum CacheOption { final String cacheType; - const CacheOption(this.cacheType); + const CacheMode(this.cacheType); - static CacheOption fromString(String cacheType) => values.firstWhere( + static CacheMode fromString(String cacheType) => values.firstWhere( (v) => v.cacheType == cacheType, - orElse: () => CacheOption.defaultType, + orElse: () => CacheMode.defaultType, ); } From 4bca97800520c9016ece1c1122de05761a0790a8 Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Thu, 6 Feb 2025 00:00:26 +0200 Subject: [PATCH 12/37] Reverted utils.dart --- pkgs/http/lib/src/browser_client.dart | 31 +++++++++++++++++++-------- pkgs/http/lib/src/utils.dart | 19 ---------------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/pkgs/http/lib/src/browser_client.dart b/pkgs/http/lib/src/browser_client.dart index 22da881f71..4ec98290e4 100644 --- a/pkgs/http/lib/src/browser_client.dart +++ b/pkgs/http/lib/src/browser_client.dart @@ -44,6 +44,8 @@ BaseClient createClient() { class BrowserClient extends BaseClient { final _abortController = AbortController(); + final String? _cacheMode; + /// Create a new browser-based HTTP Client. /// /// If [cacheMode] is provided then it can be used to cache the request @@ -51,10 +53,12 @@ class BrowserClient extends BaseClient { /// /// For example: /// ```dart - /// const mode = 'reload'; - /// final client = BrowserClient(cacheMode: mode); + /// final client = BrowserClient(cacheMode: 'reload'); /// ``` - BrowserClient({this.cacheMode = 'default'}); + /// + /// Defaults to `default`. + /// https://developer.mozilla.org/en-US/docs/Web/API/Request/cache + BrowserClient([String? cacheMode]) : _cacheMode = cacheMode ?? "default"; /// Whether to send credentials such as cookies or authorization headers for /// cross-site requests. @@ -64,11 +68,6 @@ class BrowserClient extends BaseClient { bool _isClosed = false; - /// Use different caching mode for a HTTP request. - /// Defaults to `default`. - // https://developer.mozilla.org/en-US/docs/Web/API/Request/cache - String cacheMode; - /// Sends an HTTP request and asynchronously returns the response. @override Future send(BaseRequest request) async { @@ -85,7 +84,7 @@ class BrowserClient extends BaseClient { RequestInit( method: request.method, body: bodyBytes.isNotEmpty ? bodyBytes.toJS : null, - cache: cacheMode, + cache: _cacheMode!, credentials: withCredentials ? 'include' : 'same-origin', headers: { if (request.contentLength case final contentLength?) @@ -201,3 +200,17 @@ Stream> _readBody(BaseRequest request, Response response) async* { extension type _IterableHeaders._(JSObject _) implements JSObject { external void forEach(JSFunction fn); } + +/// Caching modes for any http request in a browser. +enum CacheMode { + defaultType('default'), + reload('reload'), + noStore('no-store'), + noCache('no-cache'), + forceCache('force-cache'), + onlyIfCached('only-if-cached'); + + final String cacheType; + + const CacheMode(this.cacheType); +} diff --git a/pkgs/http/lib/src/utils.dart b/pkgs/http/lib/src/utils.dart index 0d48cb57b9..72ec1529f2 100644 --- a/pkgs/http/lib/src/utils.dart +++ b/pkgs/http/lib/src/utils.dart @@ -68,22 +68,3 @@ Stream onDone(Stream stream, void Function() onDone) => sink.close(); onDone(); })); - -/// Caching modes for any http request in a browser. -enum CacheMode { - defaultType('default'), - reload('reload'), - noStore('no-store'), - noCache('no-cache'), - forceCache('force-cache'), - onlyIfCached('only-if-cached'); - - final String cacheType; - - const CacheMode(this.cacheType); - - static CacheMode fromString(String cacheType) => values.firstWhere( - (v) => v.cacheType == cacheType, - orElse: () => CacheMode.defaultType, - ); -} From dad889c6e87bcc0de5273bba0d91bfcac73cbac8 Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Thu, 6 Feb 2025 00:03:30 +0200 Subject: [PATCH 13/37] Changed CacheMode from utils to browser_client file --- pkgs/http/lib/src/browser_client.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/http/lib/src/browser_client.dart b/pkgs/http/lib/src/browser_client.dart index 4ec98290e4..5e6dc8822a 100644 --- a/pkgs/http/lib/src/browser_client.dart +++ b/pkgs/http/lib/src/browser_client.dart @@ -58,7 +58,7 @@ class BrowserClient extends BaseClient { /// /// Defaults to `default`. /// https://developer.mozilla.org/en-US/docs/Web/API/Request/cache - BrowserClient([String? cacheMode]) : _cacheMode = cacheMode ?? "default"; + BrowserClient([String? cacheMode]) : _cacheMode = cacheMode ?? 'default'; /// Whether to send credentials such as cookies or authorization headers for /// cross-site requests. From 2889cb29889b2332687302af9bfd531fcdd4ad3f Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Thu, 6 Feb 2025 00:49:45 +0200 Subject: [PATCH 14/37] Enhanced BrowserClient class --- pkgs/http/lib/src/browser_client.dart | 45 ++++++++++++++------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/pkgs/http/lib/src/browser_client.dart b/pkgs/http/lib/src/browser_client.dart index 5e6dc8822a..e5c2408059 100644 --- a/pkgs/http/lib/src/browser_client.dart +++ b/pkgs/http/lib/src/browser_client.dart @@ -29,6 +29,23 @@ BaseClient createClient() { return BrowserClient(); } +/// Caching mode used by the [BrowserClient]. +/// +/// Sets the request cache value of the browser Fetch API. +/// [`Request.cache`](https://developer.mozilla.org/en-US/docs/Web/API/Request/cache) property. +enum CacheMode { + defaultType('default'), + reload('reload'), + noStore('no-store'), + noCache('no-cache'), + forceCache('force-cache'), + onlyIfCached('only-if-cached'); + + final String cacheType; + + const CacheMode(this.cacheType); +} + /// A `package:web`-based HTTP client that runs in the browser and is backed by /// [`window.fetch`](https://fetch.spec.whatwg.org/). /// @@ -44,7 +61,7 @@ BaseClient createClient() { class BrowserClient extends BaseClient { final _abortController = AbortController(); - final String? _cacheMode; + final CacheMode _cacheMode; /// Create a new browser-based HTTP Client. /// @@ -53,12 +70,10 @@ class BrowserClient extends BaseClient { /// /// For example: /// ```dart - /// final client = BrowserClient(cacheMode: 'reload'); + /// final client = BrowserClient(cacheMode: CacheMode.reload); /// ``` - /// - /// Defaults to `default`. - /// https://developer.mozilla.org/en-US/docs/Web/API/Request/cache - BrowserClient([String? cacheMode]) : _cacheMode = cacheMode ?? 'default'; + BrowserClient({CacheMode cacheMode = CacheMode.defaultType}) + : _cacheMode = cacheMode; /// Whether to send credentials such as cookies or authorization headers for /// cross-site requests. @@ -84,7 +99,7 @@ class BrowserClient extends BaseClient { RequestInit( method: request.method, body: bodyBytes.isNotEmpty ? bodyBytes.toJS : null, - cache: _cacheMode!, + cache: _cacheMode.cacheType, credentials: withCredentials ? 'include' : 'same-origin', headers: { if (request.contentLength case final contentLength?) @@ -199,18 +214,4 @@ Stream> _readBody(BaseRequest request, Response response) async* { @JS() extension type _IterableHeaders._(JSObject _) implements JSObject { external void forEach(JSFunction fn); -} - -/// Caching modes for any http request in a browser. -enum CacheMode { - defaultType('default'), - reload('reload'), - noStore('no-store'), - noCache('no-cache'), - forceCache('force-cache'), - onlyIfCached('only-if-cached'); - - final String cacheType; - - const CacheMode(this.cacheType); -} +} \ No newline at end of file From 3d1795707dfaf6171e74108173c9b3311c7357c5 Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Thu, 6 Feb 2025 01:00:39 +0200 Subject: [PATCH 15/37] Formatted for CI passover --- pkgs/http/lib/src/browser_client.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/http/lib/src/browser_client.dart b/pkgs/http/lib/src/browser_client.dart index e5c2408059..234fab568f 100644 --- a/pkgs/http/lib/src/browser_client.dart +++ b/pkgs/http/lib/src/browser_client.dart @@ -214,4 +214,4 @@ Stream> _readBody(BaseRequest request, Response response) async* { @JS() extension type _IterableHeaders._(JSObject _) implements JSObject { external void forEach(JSFunction fn); -} \ No newline at end of file +} From a1f47dba688370455a4a818a1c5ba42d186195ac Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Fri, 7 Feb 2025 00:04:01 +0200 Subject: [PATCH 16/37] Export CacheMode --- pkgs/http/lib/browser_client.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/http/lib/browser_client.dart b/pkgs/http/lib/browser_client.dart index 2cd0e5c7a4..a9f4d0a8a4 100644 --- a/pkgs/http/lib/browser_client.dart +++ b/pkgs/http/lib/browser_client.dart @@ -2,4 +2,4 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -export 'src/browser_client.dart' show BrowserClient; +export 'src/browser_client.dart' show BrowserClient, CacheMode; From cab59fca09316a4f86594a4f48aeebcc51159381 Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Wed, 12 Feb 2025 00:20:31 +0200 Subject: [PATCH 17/37] Added some cache tests --- pkgs/http/test/html/cache_test.dart | 70 +++++++++++++++++++ pkgs/http/test/html/utils.dart | 11 --- pkgs/http/test/stub_server.dart | 100 ---------------------------- 3 files changed, 70 insertions(+), 111 deletions(-) create mode 100644 pkgs/http/test/html/cache_test.dart delete mode 100644 pkgs/http/test/html/utils.dart delete mode 100644 pkgs/http/test/stub_server.dart diff --git a/pkgs/http/test/html/cache_test.dart b/pkgs/http/test/html/cache_test.dart new file mode 100644 index 0000000000..e23f160948 --- /dev/null +++ b/pkgs/http/test/html/cache_test.dart @@ -0,0 +1,70 @@ +// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +@TestOn('browser') +library; + +import 'dart:async'; +import 'dart:convert'; + +import 'package:http/browser_client.dart'; +import 'package:http/http.dart' as http; +import 'package:test/test.dart'; + +import 'utils.dart'; + +void main() { + + test('#send a StreamedRequest with default type', () async { + var client = BrowserClient(cacheMode: CacheMode.defaultType); + var request = http.StreamedRequest('POST',echoUrl); + + var responseFuture = client.send(request); + request.sink.add('{"hello": "world"}'.codeUnits); + unawaited(request.sink.close()); + var response = await responseFuture; + var bytesString = await response.stream.bytesToString(); + + final jsonResponse = jsonDecode(bytesString); + var bodyUnits = jsonResponse['body'] as List; + client.close(); + + expect(bodyUnits, + equals('{"hello": "world"}'.codeUnits)); + }); + + test('#send a StreamedRequest with reload type', () async { + var client = BrowserClient(cacheMode: CacheMode.reload); + var request = http.StreamedRequest('POST',echoUrl); + + var responseFuture = client.send(request); + request.sink.add('{"hello": "world"}'.codeUnits); + unawaited(request.sink.close()); + var response = await responseFuture; + var bytesString = await response.stream.bytesToString(); + + final jsonResponse = jsonDecode(bytesString); + client.close(); + + expect(jsonResponse["headers"]["cache-control"], + contains('no-cache')); + }); + + test('#send a StreamedRequest with no-cache type', () async { + var client = BrowserClient(cacheMode: CacheMode.noCache); + var request = http.StreamedRequest('POST',echoUrl); + + var responseFuture = client.send(request); + request.sink.add('{"hello": "world"}'.codeUnits); + unawaited(request.sink.close()); + var response = await responseFuture; + var bytesString = await response.stream.bytesToString(); + + final jsonResponse = jsonDecode(bytesString); + client.close(); + + expect(jsonResponse["headers"]["cache-control"], + contains('max-age=0')); + }); +} diff --git a/pkgs/http/test/html/utils.dart b/pkgs/http/test/html/utils.dart deleted file mode 100644 index 3d92698a54..0000000000 --- a/pkgs/http/test/html/utils.dart +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -import 'package:web/web.dart' show window; - -export '../utils.dart'; - -/// The test server's echo URL. -Uri get echoUrl => - Uri.parse('${window.location.protocol}//${window.location.host}/echo'); diff --git a/pkgs/http/test/stub_server.dart b/pkgs/http/test/stub_server.dart deleted file mode 100644 index a53f77d375..0000000000 --- a/pkgs/http/test/stub_server.dart +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -import 'dart:async'; -import 'dart:convert'; -import 'dart:io'; - -import 'package:http/http.dart'; -import 'package:http/src/utils.dart'; -import 'package:stream_channel/stream_channel.dart'; - -void hybridMain(StreamChannel channel) async { - final server = await HttpServer.bind('localhost', 0); - final url = Uri.http('localhost:${server.port}', ''); - server.listen((request) async { - var path = request.uri.path; - var response = request.response; - - if (path == '/error') { - response - ..statusCode = 400 - ..contentLength = 0; - unawaited(response.close()); - return; - } - - if (path == '/loop') { - var n = int.parse(request.uri.query); - response - ..statusCode = 302 - ..headers.set('location', url.resolve('/loop?${n + 1}').toString()) - ..contentLength = 0; - unawaited(response.close()); - return; - } - - if (path == '/redirect') { - response - ..statusCode = 302 - ..headers.set('location', url.resolve('/').toString()) - ..contentLength = 0; - unawaited(response.close()); - return; - } - - if (path == '/no-content-length') { - response - ..statusCode = 200 - ..contentLength = -1 - ..write('body'); - unawaited(response.close()); - return; - } - - var requestBodyBytes = await ByteStream(request).toBytes(); - var encodingName = request.uri.queryParameters['response-encoding']; - var outputEncoding = - encodingName == null ? ascii : requiredEncodingForCharset(encodingName); - - response.headers.contentType = - ContentType('application', 'json', charset: outputEncoding.name); - response.headers.set('single', 'value'); - - dynamic requestBody; - if (requestBodyBytes.isEmpty) { - requestBody = null; - } else if (request.headers.contentType?.charset != null) { - var encoding = - requiredEncodingForCharset(request.headers.contentType!.charset!); - requestBody = encoding.decode(requestBodyBytes); - } else { - requestBody = requestBodyBytes; - } - - final headers = >{}; - - request.headers.forEach((name, values) { - // These headers are automatically generated by dart:io, so we don't - // want to test them here. - if (name == 'cookie' || name == 'host') return; - - headers[name] = values; - }); - - var content = { - 'method': request.method, - 'path': request.uri.path, - if (requestBody != null) 'body': requestBody, - 'headers': headers, - }; - - var body = json.encode(content); - response - ..contentLength = body.length - ..write(body); - unawaited(response.close()); - }); - channel.sink.add(server.port); -} From 583157834794867f2bf47fb40f6494f00df37f7b Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Wed, 12 Feb 2025 00:23:02 +0200 Subject: [PATCH 18/37] Reverted utils.dart --- pkgs/http/test/html/utils.dart | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 pkgs/http/test/html/utils.dart diff --git a/pkgs/http/test/html/utils.dart b/pkgs/http/test/html/utils.dart new file mode 100644 index 0000000000..3d92698a54 --- /dev/null +++ b/pkgs/http/test/html/utils.dart @@ -0,0 +1,11 @@ +// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:web/web.dart' show window; + +export '../utils.dart'; + +/// The test server's echo URL. +Uri get echoUrl => + Uri.parse('${window.location.protocol}//${window.location.host}/echo'); From 2fd11c13547ff1b329c0bc45c7df1062a11c003a Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Wed, 12 Feb 2025 00:30:52 +0200 Subject: [PATCH 19/37] Reverted stub server --- pkgs/http/test/html/cache_test.dart | 6 +- pkgs/http/test/stub_server.dart | 100 ++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 pkgs/http/test/stub_server.dart diff --git a/pkgs/http/test/html/cache_test.dart b/pkgs/http/test/html/cache_test.dart index e23f160948..c2e7bf9883 100644 --- a/pkgs/http/test/html/cache_test.dart +++ b/pkgs/http/test/html/cache_test.dart @@ -32,7 +32,7 @@ void main() { expect(bodyUnits, equals('{"hello": "world"}'.codeUnits)); - }); + }, skip: 'Need to fix server tests for browser'); test('#send a StreamedRequest with reload type', () async { var client = BrowserClient(cacheMode: CacheMode.reload); @@ -49,7 +49,7 @@ void main() { expect(jsonResponse["headers"]["cache-control"], contains('no-cache')); - }); + }, skip: 'Need to fix server tests for browser'); test('#send a StreamedRequest with no-cache type', () async { var client = BrowserClient(cacheMode: CacheMode.noCache); @@ -66,5 +66,5 @@ void main() { expect(jsonResponse["headers"]["cache-control"], contains('max-age=0')); - }); + }, skip: 'Need to fix server tests for browser'); } diff --git a/pkgs/http/test/stub_server.dart b/pkgs/http/test/stub_server.dart new file mode 100644 index 0000000000..a53f77d375 --- /dev/null +++ b/pkgs/http/test/stub_server.dart @@ -0,0 +1,100 @@ +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; + +import 'package:http/http.dart'; +import 'package:http/src/utils.dart'; +import 'package:stream_channel/stream_channel.dart'; + +void hybridMain(StreamChannel channel) async { + final server = await HttpServer.bind('localhost', 0); + final url = Uri.http('localhost:${server.port}', ''); + server.listen((request) async { + var path = request.uri.path; + var response = request.response; + + if (path == '/error') { + response + ..statusCode = 400 + ..contentLength = 0; + unawaited(response.close()); + return; + } + + if (path == '/loop') { + var n = int.parse(request.uri.query); + response + ..statusCode = 302 + ..headers.set('location', url.resolve('/loop?${n + 1}').toString()) + ..contentLength = 0; + unawaited(response.close()); + return; + } + + if (path == '/redirect') { + response + ..statusCode = 302 + ..headers.set('location', url.resolve('/').toString()) + ..contentLength = 0; + unawaited(response.close()); + return; + } + + if (path == '/no-content-length') { + response + ..statusCode = 200 + ..contentLength = -1 + ..write('body'); + unawaited(response.close()); + return; + } + + var requestBodyBytes = await ByteStream(request).toBytes(); + var encodingName = request.uri.queryParameters['response-encoding']; + var outputEncoding = + encodingName == null ? ascii : requiredEncodingForCharset(encodingName); + + response.headers.contentType = + ContentType('application', 'json', charset: outputEncoding.name); + response.headers.set('single', 'value'); + + dynamic requestBody; + if (requestBodyBytes.isEmpty) { + requestBody = null; + } else if (request.headers.contentType?.charset != null) { + var encoding = + requiredEncodingForCharset(request.headers.contentType!.charset!); + requestBody = encoding.decode(requestBodyBytes); + } else { + requestBody = requestBodyBytes; + } + + final headers = >{}; + + request.headers.forEach((name, values) { + // These headers are automatically generated by dart:io, so we don't + // want to test them here. + if (name == 'cookie' || name == 'host') return; + + headers[name] = values; + }); + + var content = { + 'method': request.method, + 'path': request.uri.path, + if (requestBody != null) 'body': requestBody, + 'headers': headers, + }; + + var body = json.encode(content); + response + ..contentLength = body.length + ..write(body); + unawaited(response.close()); + }); + channel.sink.add(server.port); +} From 53dc82fc34d1547ad35ee13e0a3acb51396acba7 Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Wed, 12 Feb 2025 02:02:43 +0200 Subject: [PATCH 20/37] Add tests cache including fixing CI passover --- pkgs/http/test/html/cache_test.dart | 47 ++++++++++++++++++----------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/pkgs/http/test/html/cache_test.dart b/pkgs/http/test/html/cache_test.dart index c2e7bf9883..6a97086877 100644 --- a/pkgs/http/test/html/cache_test.dart +++ b/pkgs/http/test/html/cache_test.dart @@ -6,37 +6,33 @@ library; import 'dart:async'; -import 'dart:convert'; import 'package:http/browser_client.dart'; import 'package:http/http.dart' as http; +import 'package:http/src/exception.dart'; import 'package:test/test.dart'; import 'utils.dart'; void main() { - test('#send a StreamedRequest with default type', () async { var client = BrowserClient(cacheMode: CacheMode.defaultType); - var request = http.StreamedRequest('POST',echoUrl); - + var request = http.StreamedRequest('POST', echoUrl); var responseFuture = client.send(request); request.sink.add('{"hello": "world"}'.codeUnits); unawaited(request.sink.close()); + var response = await responseFuture; - var bytesString = await response.stream.bytesToString(); - final jsonResponse = jsonDecode(bytesString); - var bodyUnits = jsonResponse['body'] as List; client.close(); - expect(bodyUnits, - equals('{"hello": "world"}'.codeUnits)); + expect(response.statusCode, 200); + expect(response.reasonPhrase, 'OK'); }, skip: 'Need to fix server tests for browser'); test('#send a StreamedRequest with reload type', () async { var client = BrowserClient(cacheMode: CacheMode.reload); - var request = http.StreamedRequest('POST',echoUrl); + var request = http.StreamedRequest('POST', echoUrl); var responseFuture = client.send(request); request.sink.add('{"hello": "world"}'.codeUnits); @@ -44,16 +40,14 @@ void main() { var response = await responseFuture; var bytesString = await response.stream.bytesToString(); - final jsonResponse = jsonDecode(bytesString); client.close(); - expect(jsonResponse["headers"]["cache-control"], - contains('no-cache')); + expect(bytesString, contains('no-cache')); }, skip: 'Need to fix server tests for browser'); test('#send a StreamedRequest with no-cache type', () async { var client = BrowserClient(cacheMode: CacheMode.noCache); - var request = http.StreamedRequest('POST',echoUrl); + var request = http.StreamedRequest('POST', echoUrl); var responseFuture = client.send(request); request.sink.add('{"hello": "world"}'.codeUnits); @@ -61,10 +55,29 @@ void main() { var response = await responseFuture; var bytesString = await response.stream.bytesToString(); - final jsonResponse = jsonDecode(bytesString); client.close(); + expect(bytesString, contains('max-age=0')); + }, skip: 'Need to fix server tests for browser'); + + test('#send a StreamedRequest with only-if-cached type', () { + var client = BrowserClient(cacheMode: CacheMode.onlyIfCached); + var request = http.StreamedRequest('POST', echoUrl); - expect(jsonResponse["headers"]["cache-control"], - contains('max-age=0')); + expectLater(client.send(request), throwsA(isA())); + request.sink.add('{"hello": "world"}'.codeUnits); + unawaited(request.sink.close()); + + client.close(); + }, skip: 'Need to fix server tests for browser'); + + test('#send with an invalid URL', () { + var client = BrowserClient(cacheMode: CacheMode.onlyIfCached); + var url = Uri.http('http.invalid', ''); + var request = http.StreamedRequest('POST', url); + + expect(client.send(request), throwsClientException()); + + request.sink.add('{"hello": "world"}'.codeUnits); + request.sink.close(); }, skip: 'Need to fix server tests for browser'); } From 7d503287e26edb14335f7c65b2495009c5d3cb6a Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Sun, 16 Feb 2025 11:54:34 +0200 Subject: [PATCH 21/37] Update browser_client with formatted style --- pkgs/http/lib/src/browser_client.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/pkgs/http/lib/src/browser_client.dart b/pkgs/http/lib/src/browser_client.dart index 072a8082d6..64a8840cd5 100644 --- a/pkgs/http/lib/src/browser_client.dart +++ b/pkgs/http/lib/src/browser_client.dart @@ -99,7 +99,6 @@ class BrowserClient extends BaseClient { final bodyBytes = await request.finalize().toBytes(); try { - final response = await _fetch( '${request.url}'.toJS, RequestInit( From 410d2272a972918ab96a0ef42fe7ec9e5557e5b3 Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Wed, 19 Feb 2025 22:39:36 +0200 Subject: [PATCH 22/37] Added for tests branch --- pkgs/http/test/html/cache_test.dart | 35 ++++++++++++++++++++++------- pkgs/http/test/stub_server.dart | 13 ++++++++++- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/pkgs/http/test/html/cache_test.dart b/pkgs/http/test/html/cache_test.dart index 6a97086877..c0e3b159d7 100644 --- a/pkgs/http/test/html/cache_test.dart +++ b/pkgs/http/test/html/cache_test.dart @@ -15,6 +15,31 @@ import 'package:test/test.dart'; import 'utils.dart'; void main() { + late int port; + setUp(() async { + final channel = spawnHybridUri(Uri(path: '/test/stub_server.dart'), + stayAlive: true); + port = await channel.stream.first as int; + }); + + test('#send a POST Request with default type', () async { + var client = BrowserClient(); + + var request = http.StreamedRequest('POST', echoUrl.replace(port: port)); + var responseFuture = client.send(request); + request.sink.add('{"hello": "world"}'.codeUnits); + unawaited(request.sink.close()); + + var response = await responseFuture; + + client.close(); + + expect(response.statusCode, 200); + expect('response', response.contentLength); + client.close(); + + }); + test('#send a StreamedRequest with default type', () async { var client = BrowserClient(cacheMode: CacheMode.defaultType); var request = http.StreamedRequest('POST', echoUrl); @@ -30,19 +55,13 @@ void main() { expect(response.reasonPhrase, 'OK'); }, skip: 'Need to fix server tests for browser'); - test('#send a StreamedRequest with reload type', () async { + test('#send a POST Request with reload type', () async { var client = BrowserClient(cacheMode: CacheMode.reload); - var request = http.StreamedRequest('POST', echoUrl); - var responseFuture = client.send(request); - request.sink.add('{"hello": "world"}'.codeUnits); - unawaited(request.sink.close()); - var response = await responseFuture; - var bytesString = await response.stream.bytesToString(); + var responseFuture = client.post(echoUrl.replace(port: port)); client.close(); - expect(bytesString, contains('no-cache')); }, skip: 'Need to fix server tests for browser'); test('#send a StreamedRequest with no-cache type', () async { diff --git a/pkgs/http/test/stub_server.dart b/pkgs/http/test/stub_server.dart index a53f77d375..9b242e8661 100644 --- a/pkgs/http/test/stub_server.dart +++ b/pkgs/http/test/stub_server.dart @@ -11,7 +11,7 @@ import 'package:http/src/utils.dart'; import 'package:stream_channel/stream_channel.dart'; void hybridMain(StreamChannel channel) async { - final server = await HttpServer.bind('localhost', 0); + final server = await HttpServer.bind('localhost', 63618); final url = Uri.http('localhost:${server.port}', ''); server.listen((request) async { var path = request.uri.path; @@ -35,6 +35,17 @@ void hybridMain(StreamChannel channel) async { return; } + if (path == '/echo') { + response + ..headers.add("Access-Control-Allow-Origin", "localhost") + ..headers.add("Access-Control-Allow-Methods", "POST, GET") + ..statusCode = 200 + ..contentLength = 31 + ..write('object'); + unawaited(response.close()); + return; + } + if (path == '/redirect') { response ..statusCode = 302 From 83672f8b085dff269f787bfd9962cb33a5e7095f Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Mon, 10 Mar 2025 16:10:27 +0200 Subject: [PATCH 23/37] Added browser clients tests including test fixes --- pkgs/http/CHANGELOG.md | 4 + pkgs/http/test/html/cache_test.dart | 98 ++++++++----------- pkgs/http/test/html/client_test.dart | 16 ++- .../http/test/html/streamed_request_test.dart | 22 +++-- pkgs/http/test/stub_server.dart | 21 ++-- 5 files changed, 82 insertions(+), 79 deletions(-) diff --git a/pkgs/http/CHANGELOG.md b/pkgs/http/CHANGELOG.md index 4a82828e9f..ff9c7a6340 100644 --- a/pkgs/http/CHANGELOG.md +++ b/pkgs/http/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.5.0 + +* Added caching options for `BrowserClient` with fixing tests. + ## 1.4.0-wip * Fixed default encoding for application/json without a charset diff --git a/pkgs/http/test/html/cache_test.dart b/pkgs/http/test/html/cache_test.dart index c0e3b159d7..3082a6367d 100644 --- a/pkgs/http/test/html/cache_test.dart +++ b/pkgs/http/test/html/cache_test.dart @@ -9,94 +9,76 @@ import 'dart:async'; import 'package:http/browser_client.dart'; import 'package:http/http.dart' as http; -import 'package:http/src/exception.dart'; +import 'package:http/http.dart'; import 'package:test/test.dart'; import 'utils.dart'; void main() { - late int port; + late Uri url; setUp(() async { - final channel = spawnHybridUri(Uri(path: '/test/stub_server.dart'), - stayAlive: true); - port = await channel.stream.first as int; + final channel = spawnHybridUri(Uri(path: '/test/stub_server.dart')); + var port = await channel.stream.first as int; + url = echoUrl.replace(port: port); }); - test('#send a POST Request with default type', () async { - var client = BrowserClient(); - - var request = http.StreamedRequest('POST', echoUrl.replace(port: port)); - var responseFuture = client.send(request); - request.sink.add('{"hello": "world"}'.codeUnits); - unawaited(request.sink.close()); - - var response = await responseFuture; - - client.close(); - - expect(response.statusCode, 200); - expect('response', response.contentLength); - client.close(); - - }); - - test('#send a StreamedRequest with default type', () async { + test('#send a GET with default type', () async { var client = BrowserClient(cacheMode: CacheMode.defaultType); - var request = http.StreamedRequest('POST', echoUrl); - var responseFuture = client.send(request); - request.sink.add('{"hello": "world"}'.codeUnits); - unawaited(request.sink.close()); - - var response = await responseFuture; - + await client.get(url); + var response = await client.get(url); client.close(); expect(response.statusCode, 200); expect(response.reasonPhrase, 'OK'); - }, skip: 'Need to fix server tests for browser'); + expect(response.body, parse(allOf(containsPair('numOfRequests', 2)))); + }); - test('#send a POST Request with reload type', () async { + test('#send a GET Request with reload type', () async { var client = BrowserClient(cacheMode: CacheMode.reload); + await client.get(url); + var response = await client.get(url); + expect(response.body, parse(allOf(containsPair('numOfRequests', 2)))); + client.close(); + }); - var responseFuture = client.post(echoUrl.replace(port: port)); + test('#send a POST with no-cache type', () async { + var client = BrowserClient(cacheMode: CacheMode.noCache); + await client.post(url); + var response = await client.post(url); client.close(); + expect( + response.body, + parse(anyOf(containsPair('numOfRequests', 2), + containsPair('cache-control', ['max-age=0'])))); + }); - }, skip: 'Need to fix server tests for browser'); + test('#send a POST with no-store type', () async { + var client = BrowserClient(cacheMode: CacheMode.noStore); - test('#send a StreamedRequest with no-cache type', () async { - var client = BrowserClient(cacheMode: CacheMode.noCache); - var request = http.StreamedRequest('POST', echoUrl); + await client.post(url); + var response = await client.post(url); + client.close(); + expect(response.body, parse(allOf(containsPair('numOfRequests', 2)))); + }); - var responseFuture = client.send(request); - request.sink.add('{"hello": "world"}'.codeUnits); - unawaited(request.sink.close()); - var response = await responseFuture; - var bytesString = await response.stream.bytesToString(); + test('#send a POST with force-store type', () async { + var client = BrowserClient(cacheMode: CacheMode.forceCache); + await client.post(url); + var response = await client.post(url); client.close(); - expect(bytesString, contains('max-age=0')); - }, skip: 'Need to fix server tests for browser'); + expect(response.body, parse(allOf(containsPair('numOfRequests', 2)))); + }); test('#send a StreamedRequest with only-if-cached type', () { var client = BrowserClient(cacheMode: CacheMode.onlyIfCached); - var request = http.StreamedRequest('POST', echoUrl); + var request = http.StreamedRequest('POST', url); expectLater(client.send(request), throwsA(isA())); request.sink.add('{"hello": "world"}'.codeUnits); unawaited(request.sink.close()); client.close(); - }, skip: 'Need to fix server tests for browser'); - - test('#send with an invalid URL', () { - var client = BrowserClient(cacheMode: CacheMode.onlyIfCached); - var url = Uri.http('http.invalid', ''); - var request = http.StreamedRequest('POST', url); - - expect(client.send(request), throwsClientException()); - - request.sink.add('{"hello": "world"}'.codeUnits); - request.sink.close(); - }, skip: 'Need to fix server tests for browser'); + }); } diff --git a/pkgs/http/test/html/client_test.dart b/pkgs/http/test/html/client_test.dart index f62e75c119..dfce611820 100644 --- a/pkgs/http/test/html/client_test.dart +++ b/pkgs/http/test/html/client_test.dart @@ -14,20 +14,30 @@ import 'package:test/test.dart'; import 'utils.dart'; void main() { + late int port; + setUpAll(() async { + final channel = + spawnHybridUri(Uri(path: '/test/stub_server.dart'), stayAlive: true); + port = await channel.stream.first as int; + }); + test('#send a StreamedRequest', () async { var client = BrowserClient(); - var request = http.StreamedRequest('POST', echoUrl); + var request = http.StreamedRequest('POST', echoUrl.replace(port: port)); var responseFuture = client.send(request); request.sink.add('{"hello": "world"}'.codeUnits); unawaited(request.sink.close()); var response = await responseFuture; + var bytesString = await response.stream.bytesToString(); + client.close(); - expect(bytesString, equals('{"hello": "world"}')); - }, skip: 'Need to fix server tests for browser'); + expect(bytesString, + parse(allOf(containsPair('body', '{"hello": "world"}'.codeUnits)))); + }); test('#send with an invalid URL', () { var client = BrowserClient(); diff --git a/pkgs/http/test/html/streamed_request_test.dart b/pkgs/http/test/html/streamed_request_test.dart index 1668656046..277b32b89e 100644 --- a/pkgs/http/test/html/streamed_request_test.dart +++ b/pkgs/http/test/html/streamed_request_test.dart @@ -14,27 +14,35 @@ import 'package:test/test.dart'; import 'utils.dart'; void main() { + late Uri url; + setUpAll(() async { + final channel = + spawnHybridUri(Uri(path: '/test/stub_server.dart'), stayAlive: true); + var port = await channel.stream.first as int; + url = echoUrl.replace(port: port); + }); group('contentLength', () { test("works when it's set", () async { - var request = http.StreamedRequest('POST', echoUrl) + var request = http.StreamedRequest('POST', url) ..contentLength = 10 ..sink.add([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); unawaited(request.sink.close()); final response = await BrowserClient().send(request); - expect(await response.stream.toBytes(), - equals([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])); + expect(await response.stream.bytesToString(), + parse(allOf(containsPair('body', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])))); }); test("works when it's not set", () async { - var request = http.StreamedRequest('POST', echoUrl); + var request = http.StreamedRequest('POST', url); request.sink.add([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); unawaited(request.sink.close()); final response = await BrowserClient().send(request); - expect(await response.stream.toBytes(), - equals([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])); + + expect(await response.stream.bytesToString(), + parse(allOf(containsPair('body', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])))); }); - }, skip: 'Need to fix server tests for browser'); + }); } diff --git a/pkgs/http/test/stub_server.dart b/pkgs/http/test/stub_server.dart index 9b242e8661..c58c7ce438 100644 --- a/pkgs/http/test/stub_server.dart +++ b/pkgs/http/test/stub_server.dart @@ -11,6 +11,7 @@ import 'package:http/src/utils.dart'; import 'package:stream_channel/stream_channel.dart'; void hybridMain(StreamChannel channel) async { + var numOfRequests = 0; final server = await HttpServer.bind('localhost', 63618); final url = Uri.http('localhost:${server.port}', ''); server.listen((request) async { @@ -35,17 +36,6 @@ void hybridMain(StreamChannel channel) async { return; } - if (path == '/echo') { - response - ..headers.add("Access-Control-Allow-Origin", "localhost") - ..headers.add("Access-Control-Allow-Methods", "POST, GET") - ..statusCode = 200 - ..contentLength = 31 - ..write('object'); - unawaited(response.close()); - return; - } - if (path == '/redirect') { response ..statusCode = 302 @@ -64,6 +54,14 @@ void hybridMain(StreamChannel channel) async { return; } + // For browser runtime testing... + if (path == '/echo') { + ++numOfRequests; + response + ..headers.add('Access-Control-Allow-Origin', '*') + ..headers.add('Access-Control-Allow-Methods', 'POST, GET'); + } + var requestBodyBytes = await ByteStream(request).toBytes(); var encodingName = request.uri.queryParameters['response-encoding']; var outputEncoding = @@ -99,6 +97,7 @@ void hybridMain(StreamChannel channel) async { 'path': request.uri.path, if (requestBody != null) 'body': requestBody, 'headers': headers, + if (path == '/echo') 'numOfRequests': numOfRequests }; var body = json.encode(content); From 40609bcc4779c455c6dd26fac49d4ec377b50934 Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Mon, 10 Mar 2025 16:13:44 +0200 Subject: [PATCH 24/37] Updated pubspec.yaml file --- pkgs/http/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/http/pubspec.yaml b/pkgs/http/pubspec.yaml index 85461c1aa2..2eeab6a695 100644 --- a/pkgs/http/pubspec.yaml +++ b/pkgs/http/pubspec.yaml @@ -1,5 +1,5 @@ name: http -version: 1.4.0-wip +version: 1.5.0 description: A composable, multi-platform, Future-based API for HTTP requests. repository: https://github.com/dart-lang/http/tree/master/pkgs/http From ff7f7e8620dc30064571f91dcacf796f91ccff30 Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Mon, 10 Mar 2025 16:16:24 +0200 Subject: [PATCH 25/37] Reset the server port to zero --- pkgs/http/test/stub_server.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/http/test/stub_server.dart b/pkgs/http/test/stub_server.dart index c58c7ce438..f98c6daee4 100644 --- a/pkgs/http/test/stub_server.dart +++ b/pkgs/http/test/stub_server.dart @@ -12,7 +12,7 @@ import 'package:stream_channel/stream_channel.dart'; void hybridMain(StreamChannel channel) async { var numOfRequests = 0; - final server = await HttpServer.bind('localhost', 63618); + final server = await HttpServer.bind('localhost', 0); final url = Uri.http('localhost:${server.port}', ''); server.listen((request) async { var path = request.uri.path; From a63d6ceddbdd73bf9eb55a4567cf99856f96d5a4 Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Mon, 10 Mar 2025 16:42:30 +0200 Subject: [PATCH 26/37] Fix the dart.yml --- .github/workflows/dart.yml | 40 +++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/.github/workflows/dart.yml b/.github/workflows/dart.yml index 331256a9b4..7f1b5d4667 100644 --- a/.github/workflows/dart.yml +++ b/.github/workflows/dart.yml @@ -21,7 +21,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:ubuntu-latest;pub-cache-hosted;sdk:stable" @@ -44,7 +44,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:ubuntu-latest;pub-cache-hosted;sdk:3.4.0;packages:pkgs/http-pkgs/http_client_conformance_tests-pkgs/http_profile-pkgs/web_socket-pkgs/web_socket_conformance_tests;commands:analyze_1" @@ -110,7 +110,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:ubuntu-latest;pub-cache-hosted;sdk:dev;packages:pkgs/http-pkgs/http_client_conformance_tests-pkgs/http_profile-pkgs/web_socket-pkgs/web_socket_conformance_tests;commands:analyze_1" @@ -176,7 +176,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:ubuntu-latest;pub-cache-hosted;sdk:dev;packages:pkgs/http-pkgs/http_client_conformance_tests-pkgs/http_profile-pkgs/web_socket-pkgs/web_socket_conformance_tests;commands:format" @@ -242,7 +242,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:pkgs/flutter_http_example;commands:format" @@ -272,7 +272,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:pkgs/flutter_http_example;commands:analyze_0" @@ -302,7 +302,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:ubuntu-latest;pub-cache-hosted;sdk:3.4.0;packages:pkgs/http;commands:command_1" @@ -339,7 +339,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:ubuntu-latest;pub-cache-hosted;sdk:3.4.0;packages:pkgs/http;commands:test_3" @@ -376,7 +376,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:ubuntu-latest;pub-cache-hosted;sdk:3.4.0;packages:pkgs/http-pkgs/http_profile;commands:test_2" @@ -422,7 +422,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:ubuntu-latest;pub-cache-hosted;sdk:3.4.0;packages:pkgs/web_socket;commands:test_6" @@ -459,7 +459,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:ubuntu-latest;pub-cache-hosted;sdk:3.4.0;packages:pkgs/web_socket;commands:test_5" @@ -496,7 +496,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:ubuntu-latest;pub-cache-hosted;sdk:dev;packages:pkgs/http;commands:command_1" @@ -533,7 +533,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:ubuntu-latest;pub-cache-hosted;sdk:dev;packages:pkgs/http;commands:test_3" @@ -570,7 +570,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:ubuntu-latest;pub-cache-hosted;sdk:dev;packages:pkgs/http-pkgs/http_profile;commands:test_2" @@ -616,7 +616,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:ubuntu-latest;pub-cache-hosted;sdk:dev;packages:pkgs/http;commands:test_4" @@ -653,7 +653,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:ubuntu-latest;pub-cache-hosted;sdk:dev;packages:pkgs/web_socket;commands:test_6" @@ -690,7 +690,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:ubuntu-latest;pub-cache-hosted;sdk:dev;packages:pkgs/web_socket;commands:test_5" @@ -727,7 +727,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:pkgs/flutter_http_example;commands:test_1" @@ -764,7 +764,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:pkgs/flutter_http_example;commands:command_0" @@ -801,7 +801,7 @@ jobs: runs-on: macos-latest steps: - name: Cache Pub hosted dependencies - uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a + uses: actions/cache@v4 with: path: "~/.pub-cache/hosted" key: "os:macos-latest;pub-cache-hosted;sdk:stable;packages:pkgs/flutter_http_example;commands:command_0" From 7e1bc9f3cc122f2699ce6bf4c7dd926ae136dcdc Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Mon, 10 Mar 2025 16:54:08 +0200 Subject: [PATCH 27/37] Fixed WASM runtime --- pkgs/http/test/html/cache_test.dart | 2 +- pkgs/http/test/html/client_test.dart | 2 +- pkgs/http/test/html/streamed_request_test.dart | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkgs/http/test/html/cache_test.dart b/pkgs/http/test/html/cache_test.dart index 3082a6367d..4f86d178fb 100644 --- a/pkgs/http/test/html/cache_test.dart +++ b/pkgs/http/test/html/cache_test.dart @@ -18,7 +18,7 @@ void main() { late Uri url; setUp(() async { final channel = spawnHybridUri(Uri(path: '/test/stub_server.dart')); - var port = await channel.stream.first as int; + var port = await (channel.stream.first as num).toInt(); url = echoUrl.replace(port: port); }); diff --git a/pkgs/http/test/html/client_test.dart b/pkgs/http/test/html/client_test.dart index dfce611820..199eb23d52 100644 --- a/pkgs/http/test/html/client_test.dart +++ b/pkgs/http/test/html/client_test.dart @@ -18,7 +18,7 @@ void main() { setUpAll(() async { final channel = spawnHybridUri(Uri(path: '/test/stub_server.dart'), stayAlive: true); - port = await channel.stream.first as int; + port = await (channel.stream.first as num).toInt(); }); test('#send a StreamedRequest', () async { diff --git a/pkgs/http/test/html/streamed_request_test.dart b/pkgs/http/test/html/streamed_request_test.dart index 277b32b89e..a9f23fc463 100644 --- a/pkgs/http/test/html/streamed_request_test.dart +++ b/pkgs/http/test/html/streamed_request_test.dart @@ -18,7 +18,7 @@ void main() { setUpAll(() async { final channel = spawnHybridUri(Uri(path: '/test/stub_server.dart'), stayAlive: true); - var port = await channel.stream.first as int; + var port = await (channel.stream.first as num).toInt(); url = echoUrl.replace(port: port); }); group('contentLength', () { From 0eabc8d66342510f240d2ff1eddba71b612175b1 Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Mon, 10 Mar 2025 16:58:46 +0200 Subject: [PATCH 28/37] Fixed WASM runtime v2 --- pkgs/http/test/html/cache_test.dart | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkgs/http/test/html/cache_test.dart b/pkgs/http/test/html/cache_test.dart index 4f86d178fb..54f670c659 100644 --- a/pkgs/http/test/html/cache_test.dart +++ b/pkgs/http/test/html/cache_test.dart @@ -10,15 +10,22 @@ import 'dart:async'; import 'package:http/browser_client.dart'; import 'package:http/http.dart' as http; import 'package:http/http.dart'; +import 'package:stream_channel/stream_channel.dart'; import 'package:test/test.dart'; import 'utils.dart'; +extension on StreamChannel { + /// Handles the Wasm case where the runtime type is actually [double] instead + /// of the JS case where its [int]. + Future get firstAsInt async => ((await stream.first) as num).toInt(); +} + void main() { late Uri url; setUp(() async { final channel = spawnHybridUri(Uri(path: '/test/stub_server.dart')); - var port = await (channel.stream.first as num).toInt(); + var port = await channel.firstAsInt; url = echoUrl.replace(port: port); }); From c14430714e7fc7f7cf02dce281bd23b71bdf6997 Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Mon, 10 Mar 2025 17:04:14 +0200 Subject: [PATCH 29/37] Fixed WASM runtime v4 --- pkgs/http/test/html/cache_test.dart | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/pkgs/http/test/html/cache_test.dart b/pkgs/http/test/html/cache_test.dart index 54f670c659..9e30e883fd 100644 --- a/pkgs/http/test/html/cache_test.dart +++ b/pkgs/http/test/html/cache_test.dart @@ -15,17 +15,11 @@ import 'package:test/test.dart'; import 'utils.dart'; -extension on StreamChannel { - /// Handles the Wasm case where the runtime type is actually [double] instead - /// of the JS case where its [int]. - Future get firstAsInt async => ((await stream.first) as num).toInt(); -} - void main() { late Uri url; setUp(() async { final channel = spawnHybridUri(Uri(path: '/test/stub_server.dart')); - var port = await channel.firstAsInt; + var port = await (channel.stream.first as num).toInt(); url = echoUrl.replace(port: port); }); From 34a87fba66e9104455af612b7f29c79d5f6707d9 Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Mon, 10 Mar 2025 17:07:19 +0200 Subject: [PATCH 30/37] Fixed WASM runtime v5 --- pkgs/http/test/html/cache_test.dart | 2 +- pkgs/http/test/html/client_test.dart | 2 +- pkgs/http/test/html/streamed_request_test.dart | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkgs/http/test/html/cache_test.dart b/pkgs/http/test/html/cache_test.dart index 9e30e883fd..873b5e2d1e 100644 --- a/pkgs/http/test/html/cache_test.dart +++ b/pkgs/http/test/html/cache_test.dart @@ -19,7 +19,7 @@ void main() { late Uri url; setUp(() async { final channel = spawnHybridUri(Uri(path: '/test/stub_server.dart')); - var port = await (channel.stream.first as num).toInt(); + var port = (await channel.stream.first as num).toInt(); url = echoUrl.replace(port: port); }); diff --git a/pkgs/http/test/html/client_test.dart b/pkgs/http/test/html/client_test.dart index 199eb23d52..10f0c0bfb5 100644 --- a/pkgs/http/test/html/client_test.dart +++ b/pkgs/http/test/html/client_test.dart @@ -18,7 +18,7 @@ void main() { setUpAll(() async { final channel = spawnHybridUri(Uri(path: '/test/stub_server.dart'), stayAlive: true); - port = await (channel.stream.first as num).toInt(); + port = (await channel.stream.first as num).toInt(); }); test('#send a StreamedRequest', () async { diff --git a/pkgs/http/test/html/streamed_request_test.dart b/pkgs/http/test/html/streamed_request_test.dart index a9f23fc463..63729451eb 100644 --- a/pkgs/http/test/html/streamed_request_test.dart +++ b/pkgs/http/test/html/streamed_request_test.dart @@ -18,7 +18,7 @@ void main() { setUpAll(() async { final channel = spawnHybridUri(Uri(path: '/test/stub_server.dart'), stayAlive: true); - var port = await (channel.stream.first as num).toInt(); + var port = (await channel.stream.first as num).toInt(); url = echoUrl.replace(port: port); }); group('contentLength', () { From 5294e96772b846e53416a117c5b048b57d09a86d Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Mon, 10 Mar 2025 17:08:33 +0200 Subject: [PATCH 31/37] Fixed WASM runtime v5 --- pkgs/http/test/html/cache_test.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/pkgs/http/test/html/cache_test.dart b/pkgs/http/test/html/cache_test.dart index 873b5e2d1e..117f553241 100644 --- a/pkgs/http/test/html/cache_test.dart +++ b/pkgs/http/test/html/cache_test.dart @@ -10,7 +10,6 @@ import 'dart:async'; import 'package:http/browser_client.dart'; import 'package:http/http.dart' as http; import 'package:http/http.dart'; -import 'package:stream_channel/stream_channel.dart'; import 'package:test/test.dart'; import 'utils.dart'; From d12c0a45da8636f3af0af00227612c4c53310c15 Mon Sep 17 00:00:00 2001 From: Seif Ashraf Date: Tue, 11 Mar 2025 00:41:39 +0200 Subject: [PATCH 32/37] Update CHANGELOG.md --- pkgs/http/CHANGELOG.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pkgs/http/CHANGELOG.md b/pkgs/http/CHANGELOG.md index ff9c7a6340..58556e2381 100644 --- a/pkgs/http/CHANGELOG.md +++ b/pkgs/http/CHANGELOG.md @@ -1,14 +1,12 @@ -## 1.5.0 - -* Added caching options for `BrowserClient` with fixing tests. - ## 1.4.0-wip * Fixed default encoding for application/json without a charset to use utf8 instead of latin1, ensuring proper JSON decoding. * Avoid references to `window` in `BrowserClient`, restoring support for web workers and NodeJS. - + +* Added caching options for `BrowserClient` with fixing tests. + ## 1.3.0 * Fixed unintended HTML tags in doc comments. From 3c53220e35ddc14eefaaec3a3498a791991a6901 Mon Sep 17 00:00:00 2001 From: Seif Ashraf Date: Tue, 11 Mar 2025 00:42:34 +0200 Subject: [PATCH 33/37] Update pubspec.yaml --- pkgs/http/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/http/pubspec.yaml b/pkgs/http/pubspec.yaml index 2eeab6a695..85461c1aa2 100644 --- a/pkgs/http/pubspec.yaml +++ b/pkgs/http/pubspec.yaml @@ -1,5 +1,5 @@ name: http -version: 1.5.0 +version: 1.4.0-wip description: A composable, multi-platform, Future-based API for HTTP requests. repository: https://github.com/dart-lang/http/tree/master/pkgs/http From b4852138e713e41af99db1bec0a917eb6d071515 Mon Sep 17 00:00:00 2001 From: Seif Ashraf Date: Tue, 11 Mar 2025 00:44:00 +0200 Subject: [PATCH 34/37] Update CHANGELOG.md --- pkgs/http/CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkgs/http/CHANGELOG.md b/pkgs/http/CHANGELOG.md index 58556e2381..aaee9e5e67 100644 --- a/pkgs/http/CHANGELOG.md +++ b/pkgs/http/CHANGELOG.md @@ -4,8 +4,7 @@ to use utf8 instead of latin1, ensuring proper JSON decoding. * Avoid references to `window` in `BrowserClient`, restoring support for web workers and NodeJS. - -* Added caching options for `BrowserClient` with fixing tests. +* Added caching options for `BrowserClient`. ## 1.3.0 From f65dd4f7640ff9277e3c8fd5fc70f91562b629c1 Mon Sep 17 00:00:00 2001 From: Seif Ashraf Date: Tue, 11 Mar 2025 00:50:22 +0200 Subject: [PATCH 35/37] Update cache_test.dart --- pkgs/http/test/html/cache_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/http/test/html/cache_test.dart b/pkgs/http/test/html/cache_test.dart index 117f553241..62520cdb74 100644 --- a/pkgs/http/test/html/cache_test.dart +++ b/pkgs/http/test/html/cache_test.dart @@ -1,4 +1,4 @@ -// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. From 42251ea6265a2a756fe53475f8dbdfc9f4755a9a Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Tue, 11 Mar 2025 01:13:16 +0200 Subject: [PATCH 36/37] Changed POST to GET and ensured of some tests --- pkgs/http/test/html/cache_test.dart | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/pkgs/http/test/html/cache_test.dart b/pkgs/http/test/html/cache_test.dart index 62520cdb74..0cd4360fc9 100644 --- a/pkgs/http/test/html/cache_test.dart +++ b/pkgs/http/test/html/cache_test.dart @@ -41,11 +41,11 @@ void main() { client.close(); }); - test('#send a POST with no-cache type', () async { + test('#send a GET with no-cache type', () async { var client = BrowserClient(cacheMode: CacheMode.noCache); - await client.post(url); - var response = await client.post(url); + await client.get(url); + var response = await client.get(url); client.close(); expect( response.body, @@ -53,30 +53,29 @@ void main() { containsPair('cache-control', ['max-age=0'])))); }); - test('#send a POST with no-store type', () async { + test('#send a GET with no-store type', () async { var client = BrowserClient(cacheMode: CacheMode.noStore); - await client.post(url); - var response = await client.post(url); + await client.get(url); + var response = await client.get(url); client.close(); expect(response.body, parse(allOf(containsPair('numOfRequests', 2)))); }); - test('#send a POST with force-store type', () async { + test('#send a GET with force-store type', () async { var client = BrowserClient(cacheMode: CacheMode.forceCache); - await client.post(url); - var response = await client.post(url); + await client.get(url); + var response = await client.get(url); client.close(); - expect(response.body, parse(allOf(containsPair('numOfRequests', 2)))); + expect(response.body, parse(allOf(containsPair('numOfRequests', 1)))); }); test('#send a StreamedRequest with only-if-cached type', () { var client = BrowserClient(cacheMode: CacheMode.onlyIfCached); - var request = http.StreamedRequest('POST', url); + var request = http.StreamedRequest('GET', url); expectLater(client.send(request), throwsA(isA())); - request.sink.add('{"hello": "world"}'.codeUnits); unawaited(request.sink.close()); client.close(); From 4c7471f77d527b4c03a3a08e57ed2c567589146b Mon Sep 17 00:00:00 2001 From: seifibrahim32 Date: Tue, 11 Mar 2025 14:29:28 +0200 Subject: [PATCH 37/37] Added etags and cache control --- pkgs/http/test/html/cache_test.dart | 2 +- pkgs/http/test/stub_server.dart | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pkgs/http/test/html/cache_test.dart b/pkgs/http/test/html/cache_test.dart index 0cd4360fc9..1dae3fba84 100644 --- a/pkgs/http/test/html/cache_test.dart +++ b/pkgs/http/test/html/cache_test.dart @@ -30,7 +30,7 @@ void main() { expect(response.statusCode, 200); expect(response.reasonPhrase, 'OK'); - expect(response.body, parse(allOf(containsPair('numOfRequests', 2)))); + expect(response.body, parse(allOf(containsPair('numOfRequests', 1)))); }); test('#send a GET Request with reload type', () async { diff --git a/pkgs/http/test/stub_server.dart b/pkgs/http/test/stub_server.dart index f98c6daee4..a70e64e529 100644 --- a/pkgs/http/test/stub_server.dart +++ b/pkgs/http/test/stub_server.dart @@ -17,6 +17,9 @@ void hybridMain(StreamChannel channel) async { server.listen((request) async { var path = request.uri.path; var response = request.response; + response.headers + ..set('Cache-Control', 'public, max-age=30, immutable') + ..set('etag', '312424'); if (path == '/error') { response @@ -57,6 +60,7 @@ void hybridMain(StreamChannel channel) async { // For browser runtime testing... if (path == '/echo') { ++numOfRequests; + response ..headers.add('Access-Control-Allow-Origin', '*') ..headers.add('Access-Control-Allow-Methods', 'POST, GET');