Skip to content

Commit 26771d4

Browse files
committed
feat(dart_frog)!: serve index.html by default
1 parent a7a5bf2 commit 26771d4

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

packages/dart_frog/lib/src/create_static_file_handler.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import 'package:shelf_static/shelf_static.dart';
55
/// Defaults to the `public` directory.
66
Handler createStaticFileHandler({
77
String path = 'public',
8-
String? defaultDocument,
8+
String? defaultDocument = 'index.html',
99
}) {
1010
return fromShelfHandler(
1111
createStaticHandler(path, defaultDocument: defaultDocument),

packages/dart_frog/test/src/create_static_file_handler_test.dart

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,69 @@ void main() {
5656
tempDir.delete(recursive: true).ignore();
5757
});
5858

59-
test('serves default document', () async {
59+
test('serves index.html by default', () async {
6060
const port = 3003;
6161
final tempDir = Directory.systemTemp.createTempSync();
6262

63-
const messageContents = 'hello world';
63+
const contents = '<h1>Hello World</h1>';
64+
File(
65+
path.join(tempDir.path, 'index.html'),
66+
).writeAsStringSync(contents);
67+
68+
final server = await serve(
69+
createStaticFileHandler(path: tempDir.path),
70+
'localhost',
71+
port,
72+
);
73+
74+
final response = await http.get(Uri.parse('http://localhost:$port/'));
75+
expect(response.statusCode, equals(HttpStatus.ok));
76+
expect(response.body, equals(contents));
77+
78+
await server.close();
79+
tempDir.delete(recursive: true).ignore();
80+
});
81+
82+
test(
83+
'does not serve index.html when default document '
84+
'is explicitly set to null', () async {
85+
const port = 3003;
86+
final tempDir = Directory.systemTemp.createTempSync();
87+
88+
const contents = '<h1>Hello World</h1>';
89+
File(
90+
path.join(tempDir.path, 'index.html'),
91+
).writeAsStringSync(contents);
92+
93+
final server = await serve(
94+
createStaticFileHandler(path: tempDir.path, defaultDocument: null),
95+
'localhost',
96+
port,
97+
);
98+
99+
final indexHtmlResponse = await http.get(
100+
Uri.parse('http://localhost:$port/index.html'),
101+
);
102+
expect(indexHtmlResponse.statusCode, equals(HttpStatus.ok));
103+
expect(indexHtmlResponse.body, equals(contents));
104+
105+
final rootResponse = await http.get(
106+
Uri.parse('http://localhost:$port/'),
107+
);
108+
expect(rootResponse.statusCode, equals(HttpStatus.notFound));
109+
110+
await server.close();
111+
tempDir.delete(recursive: true).ignore();
112+
});
113+
114+
test('serves custom default document', () async {
115+
const port = 3003;
116+
final tempDir = Directory.systemTemp.createTempSync();
117+
118+
const contents = 'hello world';
64119
File(
65120
path.join(tempDir.path, 'message.txt'),
66-
).writeAsStringSync(messageContents);
121+
).writeAsStringSync(contents);
67122

68123
final server = await serve(
69124
createStaticFileHandler(
@@ -74,11 +129,11 @@ void main() {
74129
port,
75130
);
76131

77-
final messageResponse = await http.get(
132+
final response = await http.get(
78133
Uri.parse('http://localhost:$port/'),
79134
);
80-
expect(messageResponse.statusCode, equals(HttpStatus.ok));
81-
expect(messageResponse.body, equals(messageContents));
135+
expect(response.statusCode, equals(HttpStatus.ok));
136+
expect(response.body, equals(contents));
82137

83138
await server.close();
84139
tempDir.delete(recursive: true).ignore();

0 commit comments

Comments
 (0)