Skip to content
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

Enable request tests on all platforms #1929

Merged
merged 5 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 2 additions & 0 deletions dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ See the [Migration Guide][] for the complete breaking changes list.**

- Fix failing requests throw `DioException`s with `.unknown` instead of `.connectionError` on `SocketException`.
- Removes the accidentally added `options` argument for `Options.compose`.
- Fix wrong formatting of multi-value header in `BrowserHttpClientAdapter`.
- Add warning in debug mode when trying to send data with a `GET` request in web.

## 5.3.2

Expand Down
6 changes: 6 additions & 0 deletions dio/dart_test.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
file_reporters:
json: build/reports/test-results.json

override_platforms:
chrome:
settings:
# disable web security to allow CORS requests
arguments: --disable-web-security
32 changes: 29 additions & 3 deletions dio/lib/src/adapters/browser_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:convert';
import 'dart:html';
import 'dart:typed_data';
import 'dart:developer' as dev;

import 'package:meta/meta.dart';

Expand All @@ -10,6 +11,13 @@ import '../dio_exception.dart';
import '../headers.dart';
import '../options.dart';

// For the web platform, an inline `bool.fromEnvironment` translates to
// `core.bool.fromEnvironment` instead of correctly being replaced by the
// constant value found in the environment at build time.
//
// See https://github.com/flutter/flutter/issues/51186.
const _kReleaseMode = bool.fromEnvironment('dart.vm.product');

HttpClientAdapter createAdapter() => BrowserHttpClientAdapter();

/// The default [HttpClientAdapter] for Web platforms.
Expand Down Expand Up @@ -49,7 +57,13 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
}

options.headers.remove(Headers.contentLengthHeader);
options.headers.forEach((key, v) => xhr.setRequestHeader(key, '$v'));
options.headers.forEach((key, v) {
if (v is Iterable) {
xhr.setRequestHeader(key, v.join(', '));
} else {
xhr.setRequestHeader(key, v.toString());
}
});

final connectTimeout = options.connectTimeout;
final receiveTimeout = options.receiveTimeout;
Expand All @@ -71,7 +85,9 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
xhr.status!,
headers: xhr.responseHeaders.map((k, v) => MapEntry(k, v.split(','))),
statusMessage: xhr.statusText,
isRedirect: xhr.status == 302 || xhr.status == 301,
isRedirect: xhr.status == 302 ||
xhr.status == 301 ||
options.uri.toString() != xhr.responseUrl,
kuhnroyal marked this conversation as resolved.
Show resolved Hide resolved
),
);
});
Expand Down Expand Up @@ -198,7 +214,8 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
});

cancelFuture?.then((_) {
if (xhr.readyState < 4 && xhr.readyState > 0) {
if (xhr.readyState < HttpRequest.DONE &&
xhr.readyState > HttpRequest.UNSENT) {
connectTimeoutTimer?.cancel();
try {
xhr.abort();
Expand All @@ -215,6 +232,15 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
});

if (requestStream != null) {
if (!_kReleaseMode && options.method == 'GET') {
dev.log(
'GET request with a body data are not support on the '
'web platform. Use POST/PUT instead.',
level: 900,
name: '🔔 Dio',
stackTrace: StackTrace.current,
);
}
final completer = Completer<Uint8List>();
final sink = ByteConversionSink.withCallback(
(bytes) => completer.complete(Uint8List.fromList(bytes)),
Expand Down
Loading
Loading