Skip to content

Commit

Permalink
refactor(neon_framework): use http_parser for http date parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolas Rimikis <[email protected]>
  • Loading branch information
Leptopoda committed Mar 10, 2024
1 parent 769b2a7 commit d538b71
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
8 changes: 2 additions & 6 deletions packages/neon_framework/lib/src/utils/request_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:built_value/serializer.dart';
import 'package:dynamite_runtime/http_client.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
import 'package:http_parser/http_parser.dart';
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:neon_framework/models.dart';
Expand Down Expand Up @@ -44,10 +44,6 @@ const kMaxTries = 3;
/// Requests that take longer than this duration will be canceled.
const kDefaultTimeout = Duration(seconds: 30);

/// Implements https://www.rfc-editor.org/rfc/rfc9110#name-date-time-formats
@visibleForTesting
final httpDateFormat = DateFormat('E, d MMM yyyy HH:mm:ss v', 'en_US');

/// A singleton class that handles requests to the Nextcloud API.
///
/// Requests need to be made through the [nextcloud](https://pub.dev/packages/nextcloud)
Expand Down Expand Up @@ -369,7 +365,7 @@ class CacheParameters {

/// Parse the cache parameters from HTTP response headers.
factory CacheParameters.parseHeaders(Map<String, dynamic> headers) {
final expiry = headers.containsKey('expires') ? httpDateFormat.parse(headers['expires']! as String) : null;
final expiry = headers.containsKey('expires') ? parseHttpDate(headers['expires']! as String) : null;
return CacheParameters(
etag: headers['etag'] as String?,
expires: _isExpired(expiry) ? null : expiry,
Expand Down
1 change: 1 addition & 0 deletions packages/neon_framework/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies:
flutter_zxing: ^1.0.0
go_router: ^13.0.0
http: ^1.0.0
http_parser: ^4.0.0
image: ^4.0.0
intersperse: ^2.0.0
intl: ^0.18.0
Expand Down
5 changes: 3 additions & 2 deletions packages/neon_framework/test/request_manager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:built_value/serializer.dart';
import 'package:dynamite_runtime/http_client.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart';
import 'package:http_parser/http_parser.dart';
import 'package:mocktail/mocktail.dart';
import 'package:neon_framework/src/bloc/result.dart';
import 'package:neon_framework/src/utils/request_manager.dart';
Expand Down Expand Up @@ -681,7 +682,7 @@ void main() {

test('cache ETag and Expires', () async {
for (final (hours, isSet) in [(1, true), (-1, false)]) {
var newExpires = DateTime.now().add(Duration(hours: hours));
var newExpires = DateTime.timestamp().add(Duration(hours: hours));
// Only precise to the second is allowed.
newExpires = newExpires.subtract(
Duration(
Expand Down Expand Up @@ -713,7 +714,7 @@ void main() {
headers: {},
rawHeaders: {
'etag': 'a',
'expires': httpDateFormat.format(newExpires),
'expires': formatHttpDate(newExpires),
},
),
unwrap: (rawResponse) => rawResponse.body,
Expand Down

0 comments on commit d538b71

Please sign in to comment.