-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a bug where cronet_http sends incorrect HTTP request methods (#1058)
1 parent
c125ed5
commit 40a46d8
Showing
8 changed files
with
168 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
pkgs/http_client_conformance_tests/lib/src/request_methods_server.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) 2023, 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:io'; | ||
|
||
import 'package:stream_channel/stream_channel.dart'; | ||
|
||
/// Starts an HTTP server that captures the request headers. | ||
/// | ||
/// Channel protocol: | ||
/// On Startup: | ||
/// - send port | ||
/// On Request Received: | ||
/// - send the received request method (e.g. GET) as a String | ||
/// When Receive Anything: | ||
/// - exit | ||
void hybridMain(StreamChannel<Object?> channel) async { | ||
late HttpServer server; | ||
|
||
server = (await HttpServer.bind('localhost', 0)) | ||
..listen((request) async { | ||
request.response.headers.set('Access-Control-Allow-Origin', '*'); | ||
if (request.method == 'OPTIONS') { | ||
// Handle a CORS preflight request: | ||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#preflighted_requests | ||
request.response.headers | ||
..set('Access-Control-Allow-Methods', '*') | ||
..set('Access-Control-Allow-Headers', '*'); | ||
} else { | ||
channel.sink.add(request.method); | ||
} | ||
unawaited(request.response.close()); | ||
}); | ||
|
||
channel.sink.add(server.port); | ||
await channel | ||
.stream.first; // Any writes indicates that the server should exit. | ||
unawaited(server.close()); | ||
} |
12 changes: 12 additions & 0 deletions
12
pkgs/http_client_conformance_tests/lib/src/request_methods_server_vm.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
pkgs/http_client_conformance_tests/lib/src/request_methods_server_web.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
88 changes: 88 additions & 0 deletions
88
pkgs/http_client_conformance_tests/lib/src/request_methods_tests.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) 2023, 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:async/async.dart'; | ||
import 'package:http/http.dart'; | ||
import 'package:stream_channel/stream_channel.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'request_methods_server_vm.dart' | ||
if (dart.library.html) 'request_methods_server_web.dart'; | ||
|
||
/// Tests that the [Client] correctly sends HTTP request methods | ||
/// (e.g. GET, HEAD). | ||
/// | ||
/// If [preservesMethodCase] is `false` then tests that assume that the | ||
/// [Client] preserves custom request method casing will be skipped. | ||
void testRequestMethods(Client client, | ||
{bool preservesMethodCase = true}) async { | ||
group('request methods', () { | ||
late final String host; | ||
late final StreamChannel<Object?> httpServerChannel; | ||
late final StreamQueue<Object?> httpServerQueue; | ||
|
||
setUpAll(() async { | ||
httpServerChannel = await startServer(); | ||
httpServerQueue = StreamQueue(httpServerChannel.stream); | ||
host = 'localhost:${await httpServerQueue.next}'; | ||
}); | ||
tearDownAll(() => httpServerChannel.sink.add(null)); | ||
|
||
test('custom method - not case preserving', () async { | ||
await client.send(Request( | ||
'CuStOm', | ||
Uri.http(host, ''), | ||
)); | ||
final method = await httpServerQueue.next as String; | ||
expect('CUSTOM', method.toUpperCase()); | ||
}); | ||
|
||
test('custom method case preserving', () async { | ||
await client.send(Request( | ||
'CuStOm', | ||
Uri.http(host, ''), | ||
)); | ||
final method = await httpServerQueue.next as String; | ||
expect('CuStOm', method); | ||
}, | ||
skip: preservesMethodCase | ||
? false | ||
: 'does not preserve HTTP request method case'); | ||
|
||
test('delete', () async { | ||
await client.delete(Uri.http(host, '')); | ||
final method = await httpServerQueue.next as String; | ||
expect('DELETE', method); | ||
}); | ||
|
||
test('get', () async { | ||
await client.get(Uri.http(host, '')); | ||
final method = await httpServerQueue.next as String; | ||
expect('GET', method); | ||
}); | ||
test('head', () async { | ||
await client.head(Uri.http(host, '')); | ||
final method = await httpServerQueue.next as String; | ||
expect('HEAD', method); | ||
}); | ||
|
||
test('patch', () async { | ||
await client.patch(Uri.http(host, '')); | ||
final method = await httpServerQueue.next as String; | ||
expect('PATCH', method); | ||
}); | ||
|
||
test('post', () async { | ||
await client.post(Uri.http(host, '')); | ||
final method = await httpServerQueue.next as String; | ||
expect('POST', method); | ||
}); | ||
|
||
test('put', () async { | ||
await client.put(Uri.http(host, '')); | ||
final method = await httpServerQueue.next as String; | ||
expect('PUT', method); | ||
}); | ||
}); | ||
} |