Skip to content

Commit

Permalink
Pass a Uri to package:http APIs (#158)
Browse files Browse the repository at this point in the history
Prepare for dart-lang/http#375
  • Loading branch information
natebosch authored Dec 9, 2020
1 parent 2a96fe5 commit fa5afaa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
4 changes: 2 additions & 2 deletions test/io_server_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ void main() {

test('Handles malformed requests gracefully.', () async {
server.mount(syncHandler);
final rs =
await http.get('${server.url}/%D0%C2%BD%A8%CE%C4%BC%FE%BC%D0.zip');
final rs = await http
.get(Uri.parse('${server.url}/%D0%C2%BD%A8%CE%C4%BC%FE%BC%D0.zip'));
expect(rs.statusCode, 400);
expect(rs.body, 'Bad Request');
});
Expand Down
28 changes: 13 additions & 15 deletions test/shelf_io_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,13 @@ void main() {
});

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

await _scheduleServer((request) {
expect(request.contentLength, 0);
expect(request.method, 'GET');

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

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

var response = await http.get('http://localhost:$_serverPort$path');
uri = Uri.http('localhost:$_serverPort', '/foo/bar', {'qs': 'value'});
var response = await http.get(uri);

expect(response.statusCode, HttpStatus.ok);
expect(response.body, 'Hello from /foo/bar');
});
Expand All @@ -108,8 +109,8 @@ void main() {
return Response.ok(null);
}));

var request = http.StreamedRequest(
'POST', Uri.parse('http://localhost:$_serverPort'));
var request =
http.StreamedRequest('POST', Uri.http('localhost:$_serverPort', ''));
request.sink.add([1, 2, 3, 4]);
request.sink.close();

Expand Down Expand Up @@ -256,7 +257,7 @@ void main() {
return syncHandler(request);
}, 'localhost', 0);

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

try {
return await http.get('http://localhost:${server.port}');
return await http.get(Uri.http('localhost:${server.port}', '/'));
} finally {
await server.close();
}
Expand Down Expand Up @@ -472,8 +473,7 @@ void main() {
context: {'shelf.io.buffer_output': false});
});

var request =
http.Request('GET', Uri.parse('http://localhost:$_serverPort/'));
var request = http.Request('GET', Uri.http('localhost:$_serverPort', ''));

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

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

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

Future<http.StreamedResponse> _post(
{Map<String, String>? headers, String? body}) {
var request =
http.Request('POST', Uri.parse('http://localhost:$_serverPort/'));
var request = http.Request('POST', Uri.http('localhost:$_serverPort', ''));

if (headers != null) request.headers.addAll(headers);
if (body != null) request.body = body;
Expand Down

0 comments on commit fa5afaa

Please sign in to comment.