Skip to content

Commit

Permalink
Enable request tests on all platforms (cfug#1929)
Browse files Browse the repository at this point in the history
<!-- Write down your pull request descriptions. -->



### New Pull Request Checklist

- [x] I have read the
[Documentation](https://pub.dev/documentation/dio/latest/)
- [x] I have searched for a similar pull request in the
[project](https://github.com/cfug/dio/pulls) and found none
- [x] I have updated this branch with the latest `main` branch to avoid
conflicts (via merge from master or rebase)
- [x] I have added the required tests to prove the fix/feature I'm
adding
- [x] I have updated the documentation (if necessary)
- [x] I have run the tests without failures
- [x] I have updated the `CHANGELOG.md` in the corresponding package

### Additional context and info (if any)

<!-- Provide more context and info about the PR. -->
  • Loading branch information
kuhnroyal authored Aug 15, 2023
1 parent 246ecfa commit c7cf974
Show file tree
Hide file tree
Showing 5 changed files with 371 additions and 193 deletions.
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,
),
);
});
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

0 comments on commit c7cf974

Please sign in to comment.