Skip to content

Commit fa5afaa

Browse files
authored
Pass a Uri to package:http APIs (#158)
Prepare for dart-lang/http#375
1 parent 2a96fe5 commit fa5afaa

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

test/io_server_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ void main() {
3232

3333
test('Handles malformed requests gracefully.', () async {
3434
server.mount(syncHandler);
35-
final rs =
36-
await http.get('${server.url}/%D0%C2%BD%A8%CE%C4%BC%FE%BC%D0.zip');
35+
final rs = await http
36+
.get(Uri.parse('${server.url}/%D0%C2%BD%A8%CE%C4%BC%FE%BC%D0.zip'));
3737
expect(rs.statusCode, 400);
3838
expect(rs.body, 'Bad Request');
3939
});

test/shelf_io_test.dart

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,13 @@ void main() {
6363
});
6464

6565
test('Request is populated correctly', () async {
66-
var path = '/foo/bar?qs=value';
66+
late Uri uri;
6767

6868
await _scheduleServer((request) {
6969
expect(request.contentLength, 0);
7070
expect(request.method, 'GET');
7171

72-
var expectedUrl = 'http://localhost:$_serverPort$path';
73-
expect(request.requestedUri, Uri.parse(expectedUrl));
72+
expect(request.requestedUri, uri);
7473

7574
expect(request.url.path, 'foo/bar');
7675
expect(request.url.pathSegments, ['foo', 'bar']);
@@ -81,7 +80,9 @@ void main() {
8180
return syncHandler(request);
8281
});
8382

84-
var response = await http.get('http://localhost:$_serverPort$path');
83+
uri = Uri.http('localhost:$_serverPort', '/foo/bar', {'qs': 'value'});
84+
var response = await http.get(uri);
85+
8586
expect(response.statusCode, HttpStatus.ok);
8687
expect(response.body, 'Hello from /foo/bar');
8788
});
@@ -108,8 +109,8 @@ void main() {
108109
return Response.ok(null);
109110
}));
110111

111-
var request = http.StreamedRequest(
112-
'POST', Uri.parse('http://localhost:$_serverPort'));
112+
var request =
113+
http.StreamedRequest('POST', Uri.http('localhost:$_serverPort', ''));
113114
request.sink.add([1, 2, 3, 4]);
114115
request.sink.close();
115116

@@ -256,7 +257,7 @@ void main() {
256257
return syncHandler(request);
257258
}, 'localhost', 0);
258259

259-
var response = await http.get('http://localhost:${server.port}');
260+
var response = await http.get(Uri.http('localhost:${server.port}', '/'));
260261
expect(response.statusCode, HttpStatus.ok);
261262
expect(response.body, 'Hello from /');
262263
await server.close();
@@ -273,7 +274,7 @@ void main() {
273274
}, 'localhost', 0);
274275

275276
try {
276-
return await http.get('http://localhost:${server.port}');
277+
return await http.get(Uri.http('localhost:${server.port}', '/'));
277278
} finally {
278279
await server.close();
279280
}
@@ -472,8 +473,7 @@ void main() {
472473
context: {'shelf.io.buffer_output': false});
473474
});
474475

475-
var request =
476-
http.Request('GET', Uri.parse('http://localhost:$_serverPort/'));
476+
var request = http.Request('GET', Uri.http('localhost:$_serverPort', ''));
477477

478478
var response = await request.send();
479479
var stream = StreamQueue(utf8.decoder.bind(response.stream));
@@ -516,7 +516,7 @@ void main() {
516516
var sslClient = HttpClient(context: securityContext);
517517

518518
Future<HttpClientRequest> _scheduleSecureGet() =>
519-
sslClient.getUrl(Uri.parse('https://localhost:${_server!.port}/'));
519+
sslClient.getUrl(Uri.https('localhost:${_server!.port}', ''));
520520

521521
test('secure sync handler returns a value to the client', () async {
522522
await _scheduleServer(syncHandler, securityContext: securityContext);
@@ -565,8 +565,7 @@ Future<http.Response> _get({
565565
// TODO: use http.Client once it supports sending and receiving multiple headers.
566566
final client = HttpClient();
567567
try {
568-
final rq =
569-
await client.getUrl(Uri.parse('http://localhost:$_serverPort/$path'));
568+
final rq = await client.getUrl(Uri.http('localhost:$_serverPort', path));
570569
headers?.forEach((key, value) {
571570
rq.headers.add(key, value);
572571
});
@@ -587,8 +586,7 @@ Future<http.Response> _get({
587586

588587
Future<http.StreamedResponse> _post(
589588
{Map<String, String>? headers, String? body}) {
590-
var request =
591-
http.Request('POST', Uri.parse('http://localhost:$_serverPort/'));
589+
var request = http.Request('POST', Uri.http('localhost:$_serverPort', ''));
592590

593591
if (headers != null) request.headers.addAll(headers);
594592
if (body != null) request.body = body;

0 commit comments

Comments
 (0)