Skip to content

Commit 46d49e3

Browse files
authored
Integrated cupertino_http with package_http_profile (#1079)
1 parent ad2e827 commit 46d49e3

9 files changed

Lines changed: 477 additions & 27 deletions

File tree

‎.github/workflows/cupertino.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
matrix:
3232
# Test on the minimum supported flutter version and the latest
3333
# version.
34-
flutter-version: ["3.19.0", "any"]
34+
flutter-version: ["3.22.0", "any"]
3535
steps:
3636
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
3737
- uses: subosito/flutter-action@2783a3f08e1baf891508463f8c6653c258246225

‎pkgs/cupertino_http/CHANGELOG.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
## 1.4.1-wip
1+
## 1.5.0
22

3+
* Add integration to the
4+
[DevTools Network View](https://docs.flutter.dev/tools/devtools/network).
35
* Upgrade to `package:ffigen` 11.0.0.
46
* Bring `WebSocket` behavior in line with the documentation by throwing
57
`WebSocketConnectionClosed` rather than `StateError` when attempting to send

‎pkgs/cupertino_http/example/integration_test/client_conformance_test.dart‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,39 @@
55
import 'package:cupertino_http/cupertino_http.dart';
66
import 'package:flutter_test/flutter_test.dart';
77
import 'package:http_client_conformance_tests/http_client_conformance_tests.dart';
8+
import 'package:http_profile/http_profile.dart';
89
import 'package:integration_test/integration_test.dart';
910

1011
void main() {
1112
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
1213

1314
group('defaultSessionConfiguration', () {
14-
testAll(
15-
CupertinoClient.defaultSessionConfiguration,
16-
canReceiveSetCookieHeaders: true,
17-
canSendCookieHeaders: true,
18-
);
15+
group('profile enabled', () {
16+
final profile = HttpClientRequestProfile.profilingEnabled;
17+
HttpClientRequestProfile.profilingEnabled = true;
18+
try {
19+
testAll(
20+
CupertinoClient.defaultSessionConfiguration,
21+
canReceiveSetCookieHeaders: true,
22+
canSendCookieHeaders: true,
23+
);
24+
} finally {
25+
HttpClientRequestProfile.profilingEnabled = profile;
26+
}
27+
});
28+
group('profile disabled', () {
29+
final profile = HttpClientRequestProfile.profilingEnabled;
30+
HttpClientRequestProfile.profilingEnabled = false;
31+
try {
32+
testAll(
33+
CupertinoClient.defaultSessionConfiguration,
34+
canReceiveSetCookieHeaders: true,
35+
canSendCookieHeaders: true,
36+
);
37+
} finally {
38+
HttpClientRequestProfile.profilingEnabled = profile;
39+
}
40+
});
1941
});
2042
group('fromSessionConfiguration', () {
2143
final config = URLSessionConfiguration.ephemeralSessionConfiguration();
Lines changed: 335 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,335 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:async';
6+
import 'dart:io';
7+
8+
import 'package:cupertino_http/src/cupertino_client.dart';
9+
import 'package:flutter_test/flutter_test.dart';
10+
import 'package:http/http.dart';
11+
import 'package:http_profile/http_profile.dart';
12+
import 'package:integration_test/integration_test.dart';
13+
14+
void main() {
15+
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
16+
17+
group('profile', () {
18+
final profilingEnabled = HttpClientRequestProfile.profilingEnabled;
19+
20+
setUpAll(() {
21+
HttpClientRequestProfile.profilingEnabled = true;
22+
});
23+
24+
tearDownAll(() {
25+
HttpClientRequestProfile.profilingEnabled = profilingEnabled;
26+
});
27+
28+
group('non-streamed POST', () {
29+
late HttpServer successServer;
30+
late Uri successServerUri;
31+
late HttpClientRequestProfile profile;
32+
33+
setUpAll(() async {
34+
successServer = (await HttpServer.bind('localhost', 0))
35+
..listen((request) async {
36+
await request.drain<void>();
37+
request.response.headers.set('Content-Type', 'text/plain');
38+
request.response.headers.set('Content-Length', '11');
39+
request.response.write('Hello World');
40+
await request.response.close();
41+
});
42+
successServerUri = Uri.http('localhost:${successServer.port}');
43+
final client = CupertinoClientWithProfile.defaultSessionConfiguration();
44+
await client.post(successServerUri,
45+
headers: {'Content-Type': 'text/plain'}, body: 'Hi');
46+
profile = client.profile!;
47+
});
48+
tearDownAll(() {
49+
successServer.close();
50+
});
51+
52+
test('profile attributes', () {
53+
expect(profile.events, isEmpty);
54+
expect(profile.requestMethod, 'POST');
55+
expect(profile.requestUri, successServerUri.toString());
56+
expect(profile.connectionInfo,
57+
containsPair('package', 'package:cupertino_http'));
58+
});
59+
60+
test('request attributes', () {
61+
expect(profile.requestData.bodyBytes, 'Hi'.codeUnits);
62+
expect(profile.requestData.contentLength, 2);
63+
expect(profile.requestData.endTime, isNotNull);
64+
expect(profile.requestData.error, isNull);
65+
expect(
66+
profile.requestData.headers, containsPair('Content-Length', ['2']));
67+
expect(profile.requestData.headers,
68+
containsPair('Content-Type', ['text/plain; charset=utf-8']));
69+
expect(profile.requestData.persistentConnection, isNull);
70+
expect(profile.requestData.proxyDetails, isNull);
71+
expect(profile.requestData.startTime, isNotNull);
72+
});
73+
74+
test('response attributes', () {
75+
expect(profile.responseData.bodyBytes, 'Hello World'.codeUnits);
76+
expect(profile.responseData.compressionState, isNull);
77+
expect(profile.responseData.contentLength, 11);
78+
expect(profile.responseData.endTime, isNotNull);
79+
expect(profile.responseData.error, isNull);
80+
expect(profile.responseData.headers,
81+
containsPair('content-type', ['text/plain']));
82+
expect(profile.responseData.headers,
83+
containsPair('content-length', ['11']));
84+
expect(profile.responseData.isRedirect, false);
85+
expect(profile.responseData.persistentConnection, isNull);
86+
expect(profile.responseData.reasonPhrase, 'OK');
87+
expect(profile.responseData.redirects, isEmpty);
88+
expect(profile.responseData.startTime, isNotNull);
89+
expect(profile.responseData.statusCode, 200);
90+
});
91+
});
92+
93+
group('streaming POST request', () {
94+
late HttpServer successServer;
95+
late Uri successServerUri;
96+
late HttpClientRequestProfile profile;
97+
98+
setUpAll(() async {
99+
successServer = (await HttpServer.bind('localhost', 0))
100+
..listen((request) async {
101+
await request.drain<void>();
102+
request.response.headers.set('Content-Type', 'text/plain');
103+
request.response.headers.set('Content-Length', '11');
104+
request.response.write('Hello World');
105+
await request.response.close();
106+
});
107+
successServerUri = Uri.http('localhost:${successServer.port}');
108+
final client = CupertinoClientWithProfile.defaultSessionConfiguration();
109+
final request = StreamedRequest('POST', successServerUri);
110+
final stream = () async* {
111+
for (var i = 0; i < 1000; ++i) {
112+
await Future<void>.delayed(const Duration());
113+
// The request has started but not finished.
114+
expect(client.profile!.requestData.startTime, isNotNull);
115+
expect(client.profile!.requestData.endTime, isNull);
116+
expect(client.profile!.responseData.startTime, isNull);
117+
expect(client.profile!.responseData.endTime, isNull);
118+
yield 'Hello'.codeUnits;
119+
}
120+
}();
121+
unawaited(
122+
request.sink.addStream(stream).then((_) => request.sink.close()));
123+
124+
await client.send(request);
125+
profile = client.profile!;
126+
});
127+
tearDownAll(() {
128+
successServer.close();
129+
});
130+
131+
test('request attributes', () async {
132+
expect(profile.requestData.bodyBytes, ('Hello' * 1000).codeUnits);
133+
expect(profile.requestData.contentLength, isNull);
134+
expect(profile.requestData.endTime, isNotNull);
135+
expect(profile.requestData.startTime, isNotNull);
136+
expect(profile.requestData.headers, isNot(contains('Content-Length')));
137+
});
138+
});
139+
140+
group('failed POST request', () {
141+
late HttpClientRequestProfile profile;
142+
143+
setUpAll(() async {
144+
final client = CupertinoClientWithProfile.defaultSessionConfiguration();
145+
try {
146+
await client.post(Uri.http('thisisnotahost'),
147+
headers: {'Content-Type': 'text/plain'}, body: 'Hi');
148+
fail('expected exception');
149+
} on ClientException {
150+
// Expected exception.
151+
}
152+
profile = client.profile!;
153+
});
154+
155+
test('profile attributes', () {
156+
expect(profile.events, isEmpty);
157+
expect(profile.requestMethod, 'POST');
158+
expect(profile.requestUri, 'http://thisisnotahost');
159+
expect(profile.connectionInfo,
160+
containsPair('package', 'package:cupertino_http'));
161+
});
162+
163+
test('request attributes', () {
164+
expect(profile.requestData.bodyBytes, 'Hi'.codeUnits);
165+
expect(profile.requestData.contentLength, 2);
166+
expect(profile.requestData.endTime, isNotNull);
167+
expect(profile.requestData.error, startsWith('ClientException:'));
168+
expect(
169+
profile.requestData.headers, containsPair('Content-Length', ['2']));
170+
expect(profile.requestData.headers,
171+
containsPair('Content-Type', ['text/plain; charset=utf-8']));
172+
expect(profile.requestData.persistentConnection, isNull);
173+
expect(profile.requestData.proxyDetails, isNull);
174+
expect(profile.requestData.startTime, isNotNull);
175+
});
176+
177+
test('response attributes', () {
178+
expect(profile.responseData.bodyBytes, isEmpty);
179+
expect(profile.responseData.compressionState, isNull);
180+
expect(profile.responseData.contentLength, isNull);
181+
expect(profile.responseData.endTime, isNull);
182+
expect(profile.responseData.error, isNull);
183+
expect(profile.responseData.headers, isNull);
184+
expect(profile.responseData.isRedirect, isNull);
185+
expect(profile.responseData.persistentConnection, isNull);
186+
expect(profile.responseData.reasonPhrase, isNull);
187+
expect(profile.responseData.redirects, isEmpty);
188+
expect(profile.responseData.startTime, isNull);
189+
expect(profile.responseData.statusCode, isNull);
190+
});
191+
});
192+
193+
group('failed POST response', () {
194+
late HttpServer successServer;
195+
late Uri successServerUri;
196+
late HttpClientRequestProfile profile;
197+
198+
setUpAll(() async {
199+
successServer = (await HttpServer.bind('localhost', 0))
200+
..listen((request) async {
201+
await request.drain<void>();
202+
request.response.headers.set('Content-Type', 'text/plain');
203+
request.response.headers.set('Content-Length', '11');
204+
final socket = await request.response.detachSocket();
205+
await socket.close();
206+
});
207+
successServerUri = Uri.http('localhost:${successServer.port}');
208+
final client = CupertinoClientWithProfile.defaultSessionConfiguration();
209+
210+
try {
211+
await client.post(successServerUri,
212+
headers: {'Content-Type': 'text/plain'}, body: 'Hi');
213+
fail('expected exception');
214+
} on ClientException {
215+
// Expected exception.
216+
}
217+
profile = client.profile!;
218+
});
219+
tearDownAll(() {
220+
successServer.close();
221+
});
222+
223+
test('profile attributes', () {
224+
expect(profile.events, isEmpty);
225+
expect(profile.requestMethod, 'POST');
226+
expect(profile.requestUri, successServerUri.toString());
227+
expect(profile.connectionInfo,
228+
containsPair('package', 'package:cupertino_http'));
229+
});
230+
231+
test('request attributes', () {
232+
expect(profile.requestData.bodyBytes, 'Hi'.codeUnits);
233+
expect(profile.requestData.contentLength, 2);
234+
expect(profile.requestData.endTime, isNotNull);
235+
expect(profile.requestData.error, isNull);
236+
expect(
237+
profile.requestData.headers, containsPair('Content-Length', ['2']));
238+
expect(profile.requestData.headers,
239+
containsPair('Content-Type', ['text/plain; charset=utf-8']));
240+
expect(profile.requestData.persistentConnection, isNull);
241+
expect(profile.requestData.proxyDetails, isNull);
242+
expect(profile.requestData.startTime, isNotNull);
243+
});
244+
245+
test('response attributes', () {
246+
expect(profile.responseData.bodyBytes, isEmpty);
247+
expect(profile.responseData.compressionState, isNull);
248+
expect(profile.responseData.contentLength, 11);
249+
expect(profile.responseData.endTime, isNotNull);
250+
expect(profile.responseData.error, startsWith('ClientException:'));
251+
expect(profile.responseData.headers,
252+
containsPair('content-type', ['text/plain']));
253+
expect(profile.responseData.headers,
254+
containsPair('content-length', ['11']));
255+
expect(profile.responseData.isRedirect, false);
256+
expect(profile.responseData.persistentConnection, isNull);
257+
expect(profile.responseData.reasonPhrase, 'OK');
258+
expect(profile.responseData.redirects, isEmpty);
259+
expect(profile.responseData.startTime, isNotNull);
260+
expect(profile.responseData.statusCode, 200);
261+
});
262+
});
263+
264+
group('redirects', () {
265+
late HttpServer successServer;
266+
late Uri successServerUri;
267+
late HttpClientRequestProfile profile;
268+
269+
setUpAll(() async {
270+
successServer = (await HttpServer.bind('localhost', 0))
271+
..listen((request) async {
272+
if (request.requestedUri.pathSegments.isEmpty) {
273+
unawaited(request.response.close());
274+
} else {
275+
final n = int.parse(request.requestedUri.pathSegments.last);
276+
final nextPath = n - 1 == 0 ? '' : '${n - 1}';
277+
unawaited(request.response
278+
.redirect(successServerUri.replace(path: '/$nextPath')));
279+
}
280+
});
281+
successServerUri = Uri.http('localhost:${successServer.port}');
282+
});
283+
tearDownAll(() {
284+
successServer.close();
285+
});
286+
287+
test('no redirects', () async {
288+
final client = CupertinoClientWithProfile.defaultSessionConfiguration();
289+
await client.get(successServerUri);
290+
profile = client.profile!;
291+
292+
expect(profile.responseData.redirects, isEmpty);
293+
});
294+
295+
test('follow redirects', () async {
296+
final client = CupertinoClientWithProfile.defaultSessionConfiguration();
297+
await client.send(Request('GET', successServerUri.replace(path: '/3'))
298+
..followRedirects = true
299+
..maxRedirects = 4);
300+
profile = client.profile!;
301+
302+
expect(profile.requestData.followRedirects, true);
303+
expect(profile.requestData.maxRedirects, 4);
304+
expect(profile.responseData.isRedirect, false);
305+
306+
expect(profile.responseData.redirects, [
307+
HttpProfileRedirectData(
308+
statusCode: 302,
309+
method: 'GET',
310+
location: successServerUri.replace(path: '/2').toString()),
311+
HttpProfileRedirectData(
312+
statusCode: 302,
313+
method: 'GET',
314+
location: successServerUri.replace(path: '/1').toString()),
315+
HttpProfileRedirectData(
316+
statusCode: 302,
317+
method: 'GET',
318+
location: successServerUri.replace(path: '/').toString(),
319+
)
320+
]);
321+
});
322+
323+
test('no follow redirects', () async {
324+
final client = CupertinoClientWithProfile.defaultSessionConfiguration();
325+
await client.send(Request('GET', successServerUri.replace(path: '/3'))
326+
..followRedirects = false);
327+
profile = client.profile!;
328+
329+
expect(profile.requestData.followRedirects, false);
330+
expect(profile.responseData.isRedirect, true);
331+
expect(profile.responseData.redirects, isEmpty);
332+
});
333+
});
334+
});
335+
}

0 commit comments

Comments
 (0)