Skip to content

Added request mode within BrowserClient #1719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9ba5471
Update README.md
seifibrahim32 Mar 13, 2024
ee01325
Merge branch 'dart-lang:master' into master
seifibrahim32 Jan 31, 2025
3a26def
Added caching options for http package
seifibrahim32 Feb 2, 2025
3ba65df
Enabled lints
seifibrahim32 Feb 2, 2025
f5c9c11
Fixed CI errors
seifibrahim32 Feb 2, 2025
52ff2c5
Changed from HttpCacheUtils to HttpCacheOptions
seifibrahim32 Feb 2, 2025
7f70434
Reverted readme file
seifibrahim32 Feb 3, 2025
ae7be97
Reverted to last commits for request files
seifibrahim32 Feb 3, 2025
1ca0c3a
Reformated the committed files
seifibrahim32 Feb 3, 2025
7a5c30d
Changed values
seifibrahim32 Feb 3, 2025
85daade
Passing CI stages
seifibrahim32 Feb 3, 2025
7ece5aa
Changed the parameter to String type
seifibrahim32 Feb 4, 2025
4bca978
Reverted utils.dart
seifibrahim32 Feb 5, 2025
dad889c
Changed CacheMode from utils to browser_client file
seifibrahim32 Feb 5, 2025
2889cb2
Enhanced BrowserClient class
seifibrahim32 Feb 5, 2025
3d17957
Formatted for CI passover
seifibrahim32 Feb 5, 2025
a1f47db
Export CacheMode
seifibrahim32 Feb 6, 2025
cab59fc
Added some cache tests
seifibrahim32 Feb 11, 2025
5831578
Reverted utils.dart
seifibrahim32 Feb 11, 2025
2fd11c1
Reverted stub server
seifibrahim32 Feb 11, 2025
53dc82f
Add tests cache including fixing CI passover
seifibrahim32 Feb 12, 2025
1adce3e
Updated the branch fork
seifibrahim32 Feb 16, 2025
7d50328
Update browser_client with formatted style
seifibrahim32 Feb 16, 2025
0149cd8
Added mode WIP
seifibrahim32 Feb 19, 2025
b51a95c
Added mode WIP v.1
seifibrahim32 Feb 19, 2025
fd511c4
Added mode WIP v.2
seifibrahim32 Feb 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkgs/http/lib/browser_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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, RequestMode;
52 changes: 51 additions & 1 deletion pkgs/http/lib/src/browser_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import 'dart:async';
import 'dart:js_interop';

import 'package:web/web.dart'
show
AbortController,
Expand All @@ -30,6 +29,38 @@ 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);
}

/// Request mode used by the [BrowserClient].
///
/// Sets the request mode value of the browser Fetch API.
/// [`Request.mode`](https://developer.mozilla.org/en-US/docs/Web/API/Request/mode) property.
enum RequestMode {
sameOrigin('same-origin'),
noCors('no-cors'),
cors('cors'),
navigate('navigate');

final String requestType;

const RequestMode(this.requestType);
}

@JS('fetch')
external JSPromise<Response> _fetch(
RequestInfo input, [
Expand All @@ -51,6 +82,23 @@ external JSPromise<Response> _fetch(
class BrowserClient extends BaseClient {
final _abortController = AbortController();

final CacheMode _cacheMode;

final RequestMode _requestMode;
/// 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
/// final client = BrowserClient(cacheMode: CacheMode.reload);
/// ```
BrowserClient({RequestMode requestMode = RequestMode.cors,
CacheMode cacheMode = CacheMode.defaultType})
: _cacheMode = cacheMode,
_requestMode = requestMode;

/// Whether to send credentials such as cookies or authorization headers for
/// cross-site requests.
///
Expand All @@ -74,6 +122,8 @@ class BrowserClient extends BaseClient {
RequestInit(
method: request.method,
body: bodyBytes.isNotEmpty ? bodyBytes.toJS : null,
cache: _cacheMode.cacheType,
mode: _requestMode.requestType,
credentials: withCredentials ? 'include' : 'same-origin',
headers: {
if (request.contentLength case final contentLength?)
Expand Down
83 changes: 83 additions & 0 deletions pkgs/http/test/html/cache_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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 '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 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.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 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, 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 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, 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);

expectLater(client.send(request), throwsA(isA<ClientException>()));
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');
}