test(shelf_static): verify sub-second file mtime on If-Modified-Since (#560) Adds regression tests for sub-second modification times (`toSecondResolution` microsecond truncation already landed in #533 and shipped in `shelf_static` 1.2.0). Includes an end-to-end `If-Modified-Since` test (exercised on Dart 3.13+, where `FileStat.modified` preserves sub-second precision) and a direct unit test for `toSecondResolution` so all supported SDKs (`^3.9.0`) verify microsecond truncation. Fixes #532
diff --git a/pkgs/shelf_static/test/basic_file_test.dart b/pkgs/shelf_static/test/basic_file_test.dart index 059f34a..c134fa5 100644 --- a/pkgs/shelf_static/test/basic_file_test.dart +++ b/pkgs/shelf_static/test/basic_file_test.dart
@@ -175,6 +175,35 @@ expect(await response.readAsString(), 'root txt'); }); + test('same as last modified with sub-second precision', () async { + final handler = createStaticHandler(d.sandbox); + + final rootPath = p.join(d.sandbox, 'root.txt'); + + // A modification time an HTTP date cannot express: sub-second precision + // is not part of `Last-Modified`, so it must not decide the comparison. + // Note: FileStat.modified only gained sub-second precision in Dart 3.13, + // so we cannot assert that `statSync().modified` retained sub-second + // precision on older SDKs (e.g., Dart 3.9.0). + await File( + rootPath, + ).setLastModified(DateTime.utc(2024, 1, 2, 3, 4, 5, 678, 901)); + + final response1 = await makeRequest(handler, '/root.txt'); + final headers = { + HttpHeaders.ifModifiedSinceHeader: + response1.headers[HttpHeaders.lastModifiedHeader]!, + }; + + final response2 = await makeRequest( + handler, + '/root.txt', + headers: headers, + ); + expect(response2.statusCode, HttpStatus.notModified); + expect(response2.contentLength, 0); + }); + test('after file modification', () async { // This test updates a file on disk to ensure the file stamp is updated // which was previously not the case on Windows due to the files "changed"
diff --git a/pkgs/shelf_static/test/util_test.dart b/pkgs/shelf_static/test/util_test.dart new file mode 100644 index 0000000..5876132 --- /dev/null +++ b/pkgs/shelf_static/test/util_test.dart
@@ -0,0 +1,16 @@ +// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file +// for details. 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:shelf_static/src/util.dart'; +import 'package:test/test.dart'; + +void main() { + test('toSecondResolution truncates milliseconds and microseconds', () { + final dt = DateTime.utc(2024, 1, 2, 3, 4, 5, 678, 901); + expect(toSecondResolution(dt), DateTime.utc(2024, 1, 2, 3, 4, 5)); + + final wholeSecond = DateTime.utc(2024, 1, 2, 3, 4, 5); + expect(toSecondResolution(wholeSecond), same(wholeSecond)); + }); +}