-
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.
[cupertino_http] Fix a bug where content-length was not set for multi…
…part messages (#1247)
- Loading branch information
1 parent
0bbd166
commit eb189e1
Showing
8 changed files
with
143 additions
and
6 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
48 changes: 48 additions & 0 deletions
48
pkgs/http_client_conformance_tests/lib/src/multipart_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,48 @@ | ||
// Copyright (c) 2024, 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:stream_channel/stream_channel.dart'; | ||
|
||
/// Starts an HTTP server that captures the request headers and body. | ||
/// | ||
/// Channel protocol: | ||
/// On Startup: | ||
/// - send port | ||
/// On Request Received: | ||
/// - send the received headers and request body | ||
/// 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 { | ||
final headers = <String, List<String>>{}; | ||
request.headers.forEach((field, value) { | ||
headers[field] = value; | ||
}); | ||
final body = | ||
await const Utf8Decoder().bind(request).fold('', (x, y) => '$x$y'); | ||
channel.sink.add((headers, body)); | ||
} | ||
unawaited(request.response.close()); | ||
}); | ||
|
||
channel.sink.add(server.port); | ||
await channel | ||
.stream.first; // Any writes indicates that the server should exit. | ||
unawaited(server.close()); | ||
} |
14 changes: 14 additions & 0 deletions
14
pkgs/http_client_conformance_tests/lib/src/multipart_server_vm.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
pkgs/http_client_conformance_tests/lib/src/multipart_server_web.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
pkgs/http_client_conformance_tests/lib/src/multipart_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,51 @@ | ||
// Copyright (c) 2024, 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 'multipart_server_vm.dart' | ||
if (dart.library.js_interop) 'multipart_server_web.dart'; | ||
|
||
/// Tests that the [Client] correctly sends [MultipartRequest]. | ||
/// | ||
/// If [supportsMultipartRequest] is `false` then tests that assume that | ||
/// multipart requests can be sent will be skipped. | ||
void testMultipartRequests(Client client, | ||
{required bool supportsMultipartRequest}) async { | ||
group('multipart requests', () { | ||
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.nextAsInt}'; | ||
}); | ||
tearDownAll(() => httpServerChannel.sink.add(null)); | ||
|
||
test('attached file', () async { | ||
final request = MultipartRequest('POST', Uri.http(host, '')); | ||
|
||
request.files.add(MultipartFile.fromString('file1', 'Hello World')); | ||
|
||
await client.send(request); | ||
final (headers, body) = | ||
await httpServerQueue.next as (Map<String, List<String>>, String); | ||
expect(headers['content-length']!.single, '${request.contentLength}'); | ||
expect(headers['content-type']!.single, | ||
startsWith('multipart/form-data; boundary=')); | ||
expect(body, contains('''content-type: text/plain; charset=utf-8\r | ||
content-disposition: form-data; name="file1"\r | ||
\r | ||
Hello World''')); | ||
}); | ||
}, | ||
skip: supportsMultipartRequest | ||
? false | ||
: 'does not support multipart requests'); | ||
} |