Use file modified date for 304 checks ("changed" is creation date on Windows) (#38)
Fixes #37
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 85a3c27..78a954d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,8 @@
+## 0.2.9
+
+* Use file `modified` dates instead of `changed` for `304 Not Modified` checks
+ as `changed` returns creation dates on Windows.
+
## 0.2.8
* Update SDK constraint to `>=2.0.0-dev.61 <3.0.0`.
diff --git a/lib/src/static_handler.dart b/lib/src/static_handler.dart
index b6131ea..4f278ec 100644
--- a/lib/src/static_handler.dart
+++ b/lib/src/static_handler.dart
@@ -182,7 +182,7 @@
var ifModifiedSince = request.ifModifiedSince;
if (ifModifiedSince != null) {
- var fileChangeAtSecResolution = toSecondResolution(stat.changed);
+ var fileChangeAtSecResolution = toSecondResolution(stat.modified);
if (!fileChangeAtSecResolution.isAfter(ifModifiedSince)) {
return new Response.notModified();
}
@@ -190,7 +190,7 @@
var headers = {
HttpHeaders.contentLengthHeader: stat.size.toString(),
- HttpHeaders.lastModifiedHeader: formatHttpDate(stat.changed)
+ HttpHeaders.lastModifiedHeader: formatHttpDate(stat.modified)
};
var contentType = await getContentType();
diff --git a/pubspec.yaml b/pubspec.yaml
index b105264..c37a42c 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: shelf_static
-version: 0.2.8
+version: 0.2.9
author: Dart Team <misc@dartlang.org>
description: Static file server support for Shelf
homepage: https://github.com/dart-lang/shelf_static
diff --git a/test/basic_file_test.dart b/test/basic_file_test.dart
index 2b31dd6..16f63d0 100644
--- a/test/basic_file_test.dart
+++ b/test/basic_file_test.dart
@@ -2,6 +2,7 @@
// 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 'dart:async';
import 'dart:convert';
import 'dart:io';
@@ -86,7 +87,7 @@
var handler = createStaticHandler(d.sandbox);
var rootPath = p.join(d.sandbox, 'root.txt');
- var modified = new File(rootPath).statSync().changed.toUtc();
+ var modified = new File(rootPath).statSync().modified.toUtc();
var response = await makeRequest(handler, '/root.txt');
expect(response.lastModified, atSameTimeToSecond(modified));
@@ -97,7 +98,7 @@
var handler = createStaticHandler(d.sandbox);
var rootPath = p.join(d.sandbox, 'root.txt');
- var modified = new File(rootPath).statSync().changed.toUtc();
+ var modified = new File(rootPath).statSync().modified.toUtc();
var headers = {
HttpHeaders.ifModifiedSinceHeader: formatHttpDate(modified)
@@ -112,7 +113,7 @@
var handler = createStaticHandler(d.sandbox);
var rootPath = p.join(d.sandbox, 'root.txt');
- var modified = new File(rootPath).statSync().changed.toUtc();
+ var modified = new File(rootPath).statSync().modified.toUtc();
var headers = {
HttpHeaders.ifModifiedSinceHeader:
@@ -128,7 +129,7 @@
var handler = createStaticHandler(d.sandbox);
var rootPath = p.join(d.sandbox, 'root.txt');
- var modified = new File(rootPath).statSync().changed.toUtc();
+ var modified = new File(rootPath).statSync().modified.toUtc();
var headers = {
HttpHeaders.ifModifiedSinceHeader:
@@ -139,6 +140,33 @@
expect(response.statusCode, HttpStatus.notModified);
expect(response.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"
+ // date being the creation date.
+ // https://github.com/dart-lang/shelf_static/issues/37
+
+ var handler = createStaticHandler(d.sandbox);
+ var rootPath = p.join(d.sandbox, 'root.txt');
+
+ var response1 = await makeRequest(handler, '/root.txt');
+ var originalModificationDate = response1.lastModified;
+
+ // Ensure the timestamp change is > 1s.
+ await Future<void>.delayed(Duration(seconds: 2));
+ new File(rootPath).writeAsStringSync('updated root txt');
+
+ var headers = {
+ HttpHeaders.ifModifiedSinceHeader:
+ formatHttpDate(originalModificationDate)
+ };
+
+ var response2 = await makeRequest(handler, '/root.txt', headers: headers);
+ expect(response2.statusCode, HttpStatus.ok);
+ expect(response2.lastModified.millisecondsSinceEpoch,
+ greaterThan(originalModificationDate.millisecondsSinceEpoch));
+ });
});
group('content type', () {
diff --git a/test/sample_test.dart b/test/sample_test.dart
index 1c4b85f..87d2486 100644
--- a/test/sample_test.dart
+++ b/test/sample_test.dart
@@ -62,7 +62,7 @@
var response = await _requestFile(filename);
expect(response.contentLength, fileStat.size);
- expect(response.lastModified, atSameTimeToSecond(fileStat.changed.toUtc()));
+ expect(response.lastModified, atSameTimeToSecond(fileStat.modified.toUtc()));
await _expectCompletesWithBytes(response, fileContents);
}