diff --git a/packages/flutter_tools/lib/src/crash_reporting.dart b/packages/flutter_tools/lib/src/crash_reporting.dart index 21caa670ed..b063dd18f2 100644 --- a/packages/flutter_tools/lib/src/crash_reporting.dart +++ b/packages/flutter_tools/lib/src/crash_reporting.dart @@ -37,14 +37,17 @@ const String _kStackTraceFileField = 'DartError'; const String _kStackTraceFilename = 'stacktrace_file'; /// Sends crash reports to Google. +/// +/// There are two ways to override the behavior of this class: +/// +/// * Define a `FLUTTER_CRASH_SERVER_BASE_URL` environment variable that points +/// to a custom crash reporting server. This is useful if your development +/// environment is behind a firewall and unable to send crash reports to +/// Google, or when you wish to use your own server for collecting crash +/// reports from Flutter Tools. +/// * In tests call [initializeWith] and provide a mock implementation of +/// [http.Client]. class CrashReportSender { - static final Uri _baseUri = new Uri( - scheme: 'https', - host: _kCrashServerHost, - port: 443, - path: _kCrashEndpointPath, - ); - static CrashReportSender _instance; CrashReportSender._(this._client); @@ -60,6 +63,20 @@ class CrashReportSender { final http.Client _client; final Usage _usage = Usage.instance; + Uri get _baseUrl { + final String overrideUrl = platform.environment['FLUTTER_CRASH_SERVER_BASE_URL']; + + if (overrideUrl != null) { + return Uri.parse(overrideUrl); + } + return new Uri( + scheme: 'https', + host: _kCrashServerHost, + port: 443, + path: _kCrashEndpointPath, + ); + } + /// Sends one crash report. /// /// The report is populated from data in [error] and [stackTrace]. @@ -75,7 +92,7 @@ class CrashReportSender { printStatus('Sending crash report to Google.'); final String flutterVersion = getFlutterVersion(); - final Uri uri = _baseUri.replace( + final Uri uri = _baseUrl.replace( queryParameters: { 'product': _kProductId, 'version': flutterVersion, diff --git a/packages/flutter_tools/test/crash_reporting_test.dart b/packages/flutter_tools/test/crash_reporting_test.dart index 004b326569..445f9b4e59 100644 --- a/packages/flutter_tools/test/crash_reporting_test.dart +++ b/packages/flutter_tools/test/crash_reporting_test.dart @@ -120,6 +120,45 @@ void main() { }, overrides: { Stdio: () => const _NoStderr(), }); + + testUsingContext('can override base URL', () async { + Uri uri; + CrashReportSender.initializeWith(new MockClient((Request request) async { + uri = request.url; + return new Response('test-report-id', 200); + })); + + final int exitCode = await tools.run( + ['crash'], + [new _CrashCommand()], + reportCrashes: true, + flutterVersion: 'test-version', + ); + + expect(exitCode, 1); + + // Verify that we sent the crash report. + expect(uri, isNotNull); + expect(uri, new Uri( + scheme: 'https', + host: 'localhost', + port: 12345, + path: '/fake_server', + queryParameters: { + 'product': 'Flutter_Tools', + 'version' : 'test-version', + }, + )); + }, overrides: { + Platform: () => new FakePlatform( + operatingSystem: 'linux', + environment: { + 'FLUTTER_CRASH_SERVER_BASE_URL': 'https://localhost:12345/fake_server', + }, + script: new Uri(scheme: 'data'), + ), + Stdio: () => const _NoStderr(), + }); }); }