|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | +import 'package:shelf_flutter_asset/shelf_flutter_asset.dart'; |
| 5 | + |
| 6 | +import 'test_util.dart'; |
| 7 | + |
| 8 | +void main() { |
| 9 | + setUpAll(() async { |
| 10 | + setUpAssets(); |
| 11 | + }); |
| 12 | + |
| 13 | + group('Conditional GET Requests', () { |
| 14 | + test('includes Last-Modified header in response', () async { |
| 15 | + final handler = createAssetHandler(); |
| 16 | + final request = makeRequest( |
| 17 | + path: '/index.html', |
| 18 | + ); |
| 19 | + final response = await handler(request); |
| 20 | + |
| 21 | + expect(response.statusCode, equals(200)); |
| 22 | + expect(response.headers[HttpHeaders.lastModifiedHeader], isNotNull); |
| 23 | + expect(response.headers[HttpHeaders.lastModifiedHeader], isNotEmpty); |
| 24 | + }); |
| 25 | + |
| 26 | + test('returns 304 Not Modified for valid If-Modified-Since header', |
| 27 | + () async { |
| 28 | + // Create a single handler instance for all requests in this test |
| 29 | + // to ensure we get the same Last-Modified timestamp |
| 30 | + final handler = createAssetHandler(); |
| 31 | + |
| 32 | + // First get the Last-Modified value |
| 33 | + final initialRequest = makeRequest(path: '/index.html'); |
| 34 | + final initialResponse = await handler(initialRequest); |
| 35 | + final lastModified = |
| 36 | + initialResponse.headers[HttpHeaders.lastModifiedHeader]!; |
| 37 | + |
| 38 | + // Then make a conditional request with that same timestamp |
| 39 | + final conditionalRequest = makeRequest( |
| 40 | + path: '/index.html', |
| 41 | + headers: {HttpHeaders.ifModifiedSinceHeader: lastModified}, |
| 42 | + ); |
| 43 | + |
| 44 | + final conditionalResponse = await handler(conditionalRequest); |
| 45 | + |
| 46 | + expect(conditionalResponse.statusCode, equals(304)); |
| 47 | + expect(await conditionalResponse.read().toList(), isEmpty); |
| 48 | + }); |
| 49 | + |
| 50 | + test('returns 200 OK for If-Modified-Since header with old date', () async { |
| 51 | + final handler = createAssetHandler(); |
| 52 | + |
| 53 | + // Use a date from the past |
| 54 | + final oldDate = HttpDate.format(DateTime(2000, 1, 1)); |
| 55 | + |
| 56 | + final request = makeRequest( |
| 57 | + path: '/index.html', |
| 58 | + headers: {HttpHeaders.ifModifiedSinceHeader: oldDate}, |
| 59 | + ); |
| 60 | + final response = await handler(request); |
| 61 | + |
| 62 | + expect(response.statusCode, equals(200)); |
| 63 | + expect(await response.read().toList(), isNotEmpty); |
| 64 | + }); |
| 65 | + |
| 66 | + test('ignores invalid If-Modified-Since header', () async { |
| 67 | + final handler = createAssetHandler(); |
| 68 | + |
| 69 | + final request = makeRequest( |
| 70 | + path: '/index.html', |
| 71 | + headers: {HttpHeaders.ifModifiedSinceHeader: 'invalid-date-format'}, |
| 72 | + ); |
| 73 | + final response = await handler(request); |
| 74 | + |
| 75 | + expect(response.statusCode, equals(200)); |
| 76 | + expect(await response.read().toList(), isNotEmpty); |
| 77 | + }); |
| 78 | + }); |
| 79 | +} |
0 commit comments