diff --git a/pkgs/flutter_http_example/lib/http_client_factory.dart b/pkgs/flutter_http_example/lib/http_client_factory.dart index c712b6e269..cb36597c23 100644 --- a/pkgs/flutter_http_example/lib/http_client_factory.dart +++ b/pkgs/flutter_http_example/lib/http_client_factory.dart @@ -4,10 +4,14 @@ import 'dart:io'; +import 'package:cronet_http/cronet_http.dart'; import 'package:cupertino_http/cupertino_http.dart'; import 'package:http/http.dart'; Client httpClient() { + if (Platform.isAndroid) { + return CronetClient.defaultCronetEngine(); + } if (Platform.isIOS || Platform.isMacOS) { return CupertinoClient.defaultSessionConfiguration(); } diff --git a/pkgs/flutter_http_example/lib/main.dart b/pkgs/flutter_http_example/lib/main.dart index f9dd6f04ca..39f466e19a 100644 --- a/pkgs/flutter_http_example/lib/main.dart +++ b/pkgs/flutter_http_example/lib/main.dart @@ -13,6 +13,7 @@ import 'http_client_factory.dart' if (dart.library.html) 'http_client_factory_web.dart' as http_factory; void main() { + print("Main was called!"); runWithClient(() => runApp(const BookSearchApp()), http_factory.httpClient); } @@ -51,10 +52,11 @@ class _HomePageState extends State { Uri.https( 'www.googleapis.com', '/books/v1/volumes', - {'q': query, 'maxResults': '40', 'printType': 'books'}, + {'q': query, 'maxResults': '1', 'printType': 'books'}, ), ); + print(utf8.decode(response.bodyBytes)); final json = jsonDecode(utf8.decode(response.bodyBytes)) as Map; return Book.listFromJson(json); } diff --git a/pkgs/flutter_http_example/pubspec.yaml b/pkgs/flutter_http_example/pubspec.yaml index 6aeb106b6f..5e9d0cc348 100644 --- a/pkgs/flutter_http_example/pubspec.yaml +++ b/pkgs/flutter_http_example/pubspec.yaml @@ -12,6 +12,8 @@ environment: dependencies: cached_network_image: ^3.2.3 cupertino_http: ^1.1.0 + cronet_http: + path: ../cronet_http cupertino_icons: ^1.0.2 fetch_client: ^1.0.2 flutter: diff --git a/pkgs/flutter_http_example/test/widget_test.dart b/pkgs/flutter_http_example/test/widget_test.dart index 0322ce56fa..71418764a8 100644 --- a/pkgs/flutter_http_example/test/widget_test.dart +++ b/pkgs/flutter_http_example/test/widget_test.dart @@ -2,8 +2,34 @@ // 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:flutter/material.dart'; +import 'package:http/http.dart'; import 'package:flutter_http_example/main.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:http/testing.dart'; + +const _singleBookResponse = ''' +{ + "kind": "books#volumes", + "totalItems": 2069, + "items": [ + { + "kind": "books#volume", + "id": "gcnAEAAAQBAJ", + "etag": "8yZ12V0pNUI", + "selfLink": "https://www.googleapis.com/books/v1/volumes/gcnAEAAAQBAJ", + "volumeInfo": { + "title": "Flutter Cookbook", + "subtitle": "100+ step-by-step recipes for building cross...", + "authors": [ + "Simone Alessandria" + ], + "publisher": "Packt Publishing Ltd", + "publishedDate": "2023-05-31", + "description": "Write, test, and publish your web, desktop...", + }] +} +'''; void main() { testWidgets('Test initial load', (WidgetTester tester) async { @@ -11,4 +37,22 @@ void main() { expect(find.text('Please enter a query'), findsOneWidget); }); + + testWidgets('Test search', (WidgetTester tester) async { + print("HERE!-1"); + final mockClient = MockClient((request) async { + print("HERE!"); + if (request.url.path != '/books/v1/volumes') { + return Response('', 404); + } + return Response(_singleBookResponse, 200); + }); + + await runWithClient( + () => tester.pumpWidget(const BookSearchApp()), () => mockClient); + await tester.enterText(find.byType(TextField), 'Flutter Cookbook'); + await tester.pump(); + + expect(find.text('Please enter a query'), findsOneWidget); + }); }