Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:shelf_static/shelf_static.dart';
/// Defaults to the `public` directory.
Handler createStaticFileHandler({
String path = 'public',
String? defaultDocument,
String? defaultDocument = 'index.html',
}) {
return fromShelfHandler(
createStaticHandler(path, defaultDocument: defaultDocument),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,69 @@ void main() {
tempDir.delete(recursive: true).ignore();
});

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

const messageContents = 'hello world';
const contents = '<h1>Hello World</h1>';
File(
path.join(tempDir.path, 'index.html'),
).writeAsStringSync(contents);

final server = await serve(
createStaticFileHandler(path: tempDir.path),
'localhost',
port,
);

final response = await http.get(Uri.parse('http://localhost:$port/'));
expect(response.statusCode, equals(HttpStatus.ok));
expect(response.body, equals(contents));

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

test(
'does not serve index.html when default document '
'is explicitly set to null', () async {
const port = 3003;
final tempDir = Directory.systemTemp.createTempSync();

const contents = '<h1>Hello World</h1>';
File(
path.join(tempDir.path, 'index.html'),
).writeAsStringSync(contents);

final server = await serve(
createStaticFileHandler(path: tempDir.path, defaultDocument: null),
'localhost',
port,
);

final indexHtmlResponse = await http.get(
Uri.parse('http://localhost:$port/index.html'),
);
expect(indexHtmlResponse.statusCode, equals(HttpStatus.ok));
expect(indexHtmlResponse.body, equals(contents));

final rootResponse = await http.get(
Uri.parse('http://localhost:$port/'),
);
expect(rootResponse.statusCode, equals(HttpStatus.notFound));

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

test('serves custom default document', () async {
const port = 3003;
final tempDir = Directory.systemTemp.createTempSync();

const contents = 'hello world';
File(
path.join(tempDir.path, 'message.txt'),
).writeAsStringSync(messageContents);
).writeAsStringSync(contents);

final server = await serve(
createStaticFileHandler(
Expand All @@ -74,11 +129,11 @@ void main() {
port,
);

final messageResponse = await http.get(
final response = await http.get(
Uri.parse('http://localhost:$port/'),
);
expect(messageResponse.statusCode, equals(HttpStatus.ok));
expect(messageResponse.body, equals(messageContents));
expect(response.statusCode, equals(HttpStatus.ok));
expect(response.body, equals(contents));

await server.close();
tempDir.delete(recursive: true).ignore();
Expand Down