Repository: flutter/flutter
File: packages/flutter_tools/test/general.shard/web/devfs_web_test.dart (Dart)

Code before:
        ..createSync(recursive: true)
        ..writeAsStringSync('flutter.js content');

      final webAssetServer = WebAssetServer(
        FakeHttpServer(),
        PackageConfig.empty,
        InternetAddress.anyIPv4,
        <String, String>{},
        <String, String>{},
        usesDdcModuleSystem,
        canaryFeatures,
        webRenderer: WebRendererMode.canvaskit,
        useLocalCanvasKit: false,
        fileSystem: globals.fs,
        webDefines: <String, String>{}, // Empty webDefines
        logger: logger,
      );

      final Response response = await webAssetServer.handleRequest(
        Request('GET', Uri.parse('http://foobar/')),
      );

      expect(response.statusCode, HttpStatus.ok);
      // Verify the placeholder is preserved
      expect(await response.readAsString(), contains("const apiUrl = '{{MISSING_VAR}}';"));
      expect(logger.warningText, contains('Missing web-define variable: MISSING_VAR'));
    }),
  );

  test(
    'WebAssetServer serves flutter_bootstrap.js with web-define variables',
    () => testbed.run(() async {
      const bootstrapContent = '''
// Flutter bootstrap script
const config = {
  api: '{{API_URL}}',
  debug: {{DEBUG_MODE}}
};
''';

      globals.fs.currentDirectory.childDirectory('web').childFile('flutter_bootstrap.js')
        ..createSync(recursive: true)
        ..writeAsStringSync(bootstrapContent);

      globals.fs.file(
          globals.fs.path.join(
            globals.artifacts!.getHostArtifact(HostArtifact.flutterJsDirectory).path,
            'flutter.js',
          ),
        )
        ..createSync(recursive: true)

Review comment (suggestion):
Since there is code for pluralizing the vars for the logger, can we add a test that handles 2 missing ones? We do that in the web template test as well apparently. Just for completeness, though.

Code after:
        ..createSync(recursive: true)
        ..writeAsStringSync('flutter.js content');

      final webAssetServer = WebAssetServer(
        FakeHttpServer(),
        PackageConfig.empty,
        InternetAddress.anyIPv4,
        <String, String>{},
        <String, String>{},
        usesDdcModuleSystem,
        canaryFeatures,
        webRenderer: WebRendererMode.canvaskit,
        useLocalCanvasKit: false,
        fileSystem: globals.fs,
        webDefines: <String, String>{}, // Empty webDefines
        logger: logger,
      );

      final Response response = await webAssetServer.handleRequest(
        Request('GET', Uri.parse('http://foobar/')),
      );

      expect(response.statusCode, HttpStatus.ok);
      // Verify the placeholder is preserved
      expect(await response.readAsString(), contains("const apiUrl = '{{MISSING_VAR}}';"));
      expect(logger.warningText, contains('Missing web-define variable: MISSING_VAR'));
    }),
  );

  test(
    'WebAssetServer logs warning for multiple missing web-define variables in index.html',
    () => testbed.run(() async {
      const htmlContent = '''
<!DOCTYPE html>
<html>
<head>
  <title>Test</title>
  <base href="/">
</head>
<body>
  <script>
    const apiUrl = '{{MISSING_VAR_1}}';
    const apiKey = '{{MISSING_VAR_2}}';
  </script>
</body>
</html>''';

      globals.fs.currentDirectory.childDirectory('web').childFile('index.html')
        ..createSync(recursive: true)
        ..writeAsStringSync(htmlContent);