|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_test/flutter_test.dart'; |
| 5 | +import 'package:mocktail/mocktail.dart'; |
| 6 | +import 'package:rive/rive.dart'; |
| 7 | + |
| 8 | +import 'mocks/mocks.dart'; |
| 9 | +import 'src/utils.dart'; |
| 10 | + |
| 11 | +class MockHttpClient extends Mock implements HttpClient {} |
| 12 | + |
| 13 | +class MockHttpClientRequest extends Mock implements HttpClientRequest {} |
| 14 | + |
| 15 | +class MockHttpClientResponse extends Mock implements HttpClientResponse {} |
| 16 | + |
| 17 | +class MockHttpHeaders extends Mock implements HttpHeaders {} |
| 18 | + |
| 19 | +void main() { |
| 20 | + late MockHttpClient mockHttpClient; |
| 21 | + late MockHttpClientRequest request; |
| 22 | + setUpAll(() { |
| 23 | + registerFallbackValue(ArtboardFake()); |
| 24 | + registerFallbackValue(Uri()); |
| 25 | + registerFallbackValue(Stream.value(<int>[])); |
| 26 | + // Build our app and trigger a frame. |
| 27 | + final riveBytes = loadFile('assets/rive-flutter-test-asset.riv'); |
| 28 | + final body = riveBytes.buffer.asUint8List(); |
| 29 | + mockHttpClient = MockHttpClient(); |
| 30 | + request = MockHttpClientRequest(); |
| 31 | + |
| 32 | + when(() => request.headers).thenReturn(MockHttpHeaders()); |
| 33 | + |
| 34 | + when(() => mockHttpClient.openUrl(any(), any())).thenAnswer((invocation) { |
| 35 | + final response = MockHttpClientResponse(); |
| 36 | + when(request.close).thenAnswer((_) => Future.value(response)); |
| 37 | + when(() => request.addStream(any())).thenAnswer((_) async => null); |
| 38 | + when(() => response.headers).thenReturn(MockHttpHeaders()); |
| 39 | + when(() => response.handleError(any(), test: any(named: 'test'))) |
| 40 | + .thenAnswer((_) => Stream.value(body)); |
| 41 | + when(() => response.statusCode).thenReturn(200); |
| 42 | + when(() => response.reasonPhrase).thenReturn('OK'); |
| 43 | + when(() => response.contentLength).thenReturn(body.length); |
| 44 | + when(() => response.isRedirect).thenReturn(false); |
| 45 | + when(() => response.persistentConnection).thenReturn(false); |
| 46 | + return Future.value(request); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + testWidgets('Using the network, calls the http client without headers', |
| 51 | + (WidgetTester tester) async { |
| 52 | + await HttpOverrides.runZoned(() async { |
| 53 | + await tester.pumpWidget( |
| 54 | + const MaterialApp( |
| 55 | + home: RiveAnimation.network('https://some.fake.url'), |
| 56 | + ), |
| 57 | + ); |
| 58 | + }, createHttpClient: (_) => mockHttpClient); |
| 59 | + |
| 60 | + verify(() => mockHttpClient.openUrl(any(), any())).called(1); |
| 61 | + verifyNever(() => request.headers.set(any(), any())); |
| 62 | + }); |
| 63 | + |
| 64 | + testWidgets('Using the network, calls the http client with headers', |
| 65 | + (WidgetTester tester) async { |
| 66 | + await HttpOverrides.runZoned(() async { |
| 67 | + await tester.pumpWidget( |
| 68 | + const MaterialApp( |
| 69 | + home: RiveAnimation.network('https://some.fake.url', headers: { |
| 70 | + 'first': 'header', |
| 71 | + 'second': 'header', |
| 72 | + }), |
| 73 | + ), |
| 74 | + ); |
| 75 | + }, createHttpClient: (_) => mockHttpClient); |
| 76 | + |
| 77 | + verify(() => mockHttpClient.openUrl(any(), any())).called(1); |
| 78 | + verify(() => request.headers.set(any(), any())).called(2); |
| 79 | + }); |
| 80 | +} |
0 commit comments