Skip to content

Commit 2f723b0

Browse files
authored
Use the Uri.http constructor in docs and tests (#707)
Use `Uri.parse` when the string is completely opaque to the calling code. When the calling code is hardcoding the protocol, use the protocol specific constructor.
1 parent 346f25d commit 2f723b0

File tree

7 files changed

+10
-10
lines changed

7 files changed

+10
-10
lines changed

pkgs/http/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ you to make individual HTTP requests with minimal hassle:
1515
```dart
1616
import 'package:http/http.dart' as http;
1717
18-
var url = Uri.parse('https://example.com/whatsit/create');
18+
var url = Uri.https('example.com', 'whatsit/create');
1919
var response = await http.post(url, body: {'name': 'doodle', 'color': 'blue'});
2020
print('Response status: ${response.statusCode}');
2121
print('Response body: ${response.body}');
2222
23-
print(await http.read(Uri.parse('https://example.com/foobar.txt')));
23+
print(await http.read(Uri.https('example.com', 'foobar.txt')));
2424
```
2525

2626
If you're making multiple requests to the same server, you can keep open a
@@ -87,7 +87,7 @@ import 'package:http/retry.dart';
8787
Future<void> main() async {
8888
final client = RetryClient(http.Client());
8989
try {
90-
print(await client.read(Uri.parse('http://example.org')));
90+
print(await client.read(Uri.http('example.org', '')));
9191
} finally {
9292
client.close();
9393
}

pkgs/http/lib/src/client.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ Client? get zoneClient {
177177
///
178178
/// void myFunction() {
179179
/// // Uses the `Client` configured in `main`.
180-
/// final response = await get(Uri.parse("https://www.example.com/"));
180+
/// final response = await get(Uri.https('www.example.com', ''));
181181
/// final client = Client();
182182
/// }
183183
/// ```

pkgs/http/lib/src/multipart_request.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final _newlineRegExp = RegExp(r'\r\n|\r|\n');
2121
/// This request automatically sets the Content-Type header to
2222
/// `multipart/form-data`. This value will override any value set by the user.
2323
///
24-
/// var uri = Uri.parse('https://example.com/create');
24+
/// var uri = Uri.https('example.com', 'create');
2525
/// var request = http.MultipartRequest('POST', uri)
2626
/// ..fields['user'] = '[email protected]'
2727
/// ..files.add(await http.MultipartFile.fromPath(

pkgs/http/test/html/client_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void main() {
2828

2929
test('#send with an invalid URL', () {
3030
var client = BrowserClient();
31-
var url = Uri.parse('http://http.invalid');
31+
var url = Uri.http('http.invalid', '');
3232
var request = http.StreamedRequest('POST', url);
3333

3434
expect(

pkgs/http/test/http_retry_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,13 @@ void main() {
208208
expect(request.maxRedirects, equals(12));
209209
expect(request.method, equals('POST'));
210210
expect(request.persistentConnection, isFalse);
211-
expect(request.url, equals(Uri.parse('http://example.org')));
211+
expect(request.url, equals(Uri.http('example.org', '')));
212212
expect(request.body, equals('hello'));
213213
return Response('', 503);
214214
}, count: 2)),
215215
[Duration.zero]);
216216

217-
final request = Request('POST', Uri.parse('http://example.org'))
217+
final request = Request('POST', Uri.http('example.org', ''))
218218
..body = 'hello'
219219
..followRedirects = false
220220
..headers['foo'] = 'bar'

pkgs/http/test/io/client_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void main() {
111111

112112
test('#send with an invalid URL', () {
113113
var client = http.Client();
114-
var url = Uri.parse('http://http.invalid');
114+
var url = Uri.http('http.invalid', '');
115115
var request = http.StreamedRequest('POST', url);
116116
request.headers[HttpHeaders.contentTypeHeader] =
117117
'application/json; charset=utf-8';

pkgs/http/test/utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'package:http_parser/http_parser.dart';
99
import 'package:test/test.dart';
1010

1111
/// A dummy URL for constructing requests that won't be sent.
12-
Uri get dummyUrl => Uri.parse('http://dart.dev/');
12+
Uri get dummyUrl => Uri.http('dart.dev', '');
1313

1414
/// Removes eight spaces of leading indentation from a multiline string.
1515
///

0 commit comments

Comments
 (0)