From 0980fa2ddd6024fa7eef53ba78230a8e7e4eee0a Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Thu, 26 Sep 2019 13:24:05 -0700 Subject: [PATCH] Serve every html file under web (#41386) --- .../lib/src/build_runner/web_fs.dart | 11 ++++- packages/flutter_tools/lib/src/project.dart | 3 ++ .../general.shard/web/asset_server_test.dart | 47 +++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 packages/flutter_tools/test/general.shard/web/asset_server_test.dart diff --git a/packages/flutter_tools/lib/src/build_runner/web_fs.dart b/packages/flutter_tools/lib/src/build_runner/web_fs.dart index bc082e8209e..a551c04b90d 100644 --- a/packages/flutter_tools/lib/src/build_runner/web_fs.dart +++ b/packages/flutter_tools/lib/src/build_runner/web_fs.dart @@ -265,7 +265,16 @@ class AssetServer { Directory partFiles; Future handle(Request request) async { - if (request.url.path.contains('stack_trace_mapper')) { + if (request.url.path.endsWith('.html')) { + final Uri htmlUri = flutterProject.web.directory.uri.resolveUri(request.url); + final File htmlFile = fs.file(htmlUri); + if (htmlFile.existsSync()) { + return Response.ok(htmlFile.readAsBytesSync(), headers: { + 'Content-Type': 'text/html', + }); + } + return Response.notFound(''); + } else if (request.url.path.contains('stack_trace_mapper')) { final File file = fs.file(fs.path.join( artifacts.getArtifactPath(Artifact.engineDartSdkPath), 'lib', diff --git a/packages/flutter_tools/lib/src/project.dart b/packages/flutter_tools/lib/src/project.dart index 92aae9f89e2..02122028555 100644 --- a/packages/flutter_tools/lib/src/project.dart +++ b/packages/flutter_tools/lib/src/project.dart @@ -636,6 +636,9 @@ class WebProject { /// The 'lib' directory for the application. Directory get libDirectory => parent.directory.childDirectory('lib'); + /// The directory containing additional files for the application. + Directory get directory => parent.directory.childDirectory('web'); + /// The html file used to host the flutter web application. File get indexFile => parent.directory .childDirectory('web') diff --git a/packages/flutter_tools/test/general.shard/web/asset_server_test.dart b/packages/flutter_tools/test/general.shard/web/asset_server_test.dart new file mode 100644 index 00000000000..d475f0a7e0d --- /dev/null +++ b/packages/flutter_tools/test/general.shard/web/asset_server_test.dart @@ -0,0 +1,47 @@ +// Copyright 2019 The Chromium Authors. 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_tools/src/base/file_system.dart'; +import 'package:flutter_tools/src/build_runner/web_fs.dart'; +import 'package:flutter_tools/src/project.dart'; +import 'package:shelf/shelf.dart'; + +import '../../src/common.dart'; +import '../../src/testbed.dart'; + +void main() { + Testbed testbed; + AssetServer assetServer; + + setUp(() { + testbed = Testbed( + setup: () { + fs.file(fs.path.join('lib', 'main.dart')) + .createSync(recursive: true); + fs.file(fs.path.join('web', 'index.html')) + ..createSync(recursive: true) + ..writeAsStringSync('hello'); + assetServer = AssetServer(FlutterProject.current(), fs.path.join('main')); + } + ); + }); + + test('can serve an html file from the web directory', () => testbed.run(() async { + final Response response = await assetServer + .handle(Request('GET', Uri.parse('http://localhost:8080/index.html'))); + + expect(response.headers, { + 'Content-Type': 'text/html', + 'content-length': '5' + }); + expect(await response.readAsString(), 'hello'); + })); + + test('handles a missing html file from the web directory', () => testbed.run(() async { + final Response response = await assetServer + .handle(Request('GET', Uri.parse('http://localhost:8080/foobar.html'))); + + expect(response.statusCode, 404); + })); +}