From b57d03264bdd93c5133b5270bed96e7f145670c3 Mon Sep 17 00:00:00 2001 From: Maxwell Weru Date: Mon, 15 Jul 2024 10:11:53 +0300 Subject: [PATCH] Fix local to `en_US_POSIX` in dates by `SharedKeyAuthenticationProvider` This ensures consistency irrespective of the user's device. Some failing tests may be fixed via this. --- .../Authentication/SharedKeyAuthenticationProvider.swift | 3 ++- Tests/TingleApiClientTests/SharedKeyProviderTests.swift | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Sources/TingleApiClient/Authentication/SharedKeyAuthenticationProvider.swift b/Sources/TingleApiClient/Authentication/SharedKeyAuthenticationProvider.swift index e23fb1f..52999fd 100644 --- a/Sources/TingleApiClient/Authentication/SharedKeyAuthenticationProvider.swift +++ b/Sources/TingleApiClient/Authentication/SharedKeyAuthenticationProvider.swift @@ -96,7 +96,8 @@ public final class SharedKeyAuthenticationProvider: AuthenticationHeaderProvider if (rfcDate?.isEmpty ?? true) { let formatter = DateFormatter() formatter.timeZone = TimeZone(identifier: "UTC") - formatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss 'GMT'" // RFC + formatter.locale = Locale(identifier: "en_US_POSIX") // consistent regardless of the user's device + formatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss 'GMT'" // RFC 1123 rfcDate = formatter.string(from: Date()) request.setValue(rfcDate, forHTTPHeaderField: dateHeaderName) } diff --git a/Tests/TingleApiClientTests/SharedKeyProviderTests.swift b/Tests/TingleApiClientTests/SharedKeyProviderTests.swift index 9b0830c..1eb52bd 100644 --- a/Tests/TingleApiClientTests/SharedKeyProviderTests.swift +++ b/Tests/TingleApiClientTests/SharedKeyProviderTests.swift @@ -17,7 +17,7 @@ class SharedKeyProviderTests: XCTestCase { let url = URL(string: "https://scoppe.people.tinglesoftware.com/api/v1.1/iprs?idNumber=12345678")! var request = URLRequest(url: url) request.httpMethod = "GET" - request.setValue("Tue, 26 Dec 2017 23:09:28 GMT", forHTTPHeaderField: "x-ms-date") + request.setValue("Tue, 26 Dec 2017 23:09:28 GMT", forHTTPHeaderField: "x-ms-date") // RFC 1123 // prepare the authentication provider let provider = SharedKeyAuthenticationProvider(base64Key: "TiR0p2ZwnUuBGBEDU5LADWBXpxXy3Y9Aq4Fb1nD+6CM=") @@ -38,7 +38,7 @@ class SharedKeyProviderTests: XCTestCase { request.httpMethod = "POST" request.httpBody = "{}".data(using: .utf8) request.setValue("application/json", forHTTPHeaderField: "Content-Type") - request.setValue("Tue, 26 Dec 2017 23:09:28 GMT", forHTTPHeaderField: "x-ms-date") + request.setValue("Tue, 26 Dec 2017 23:09:28 GMT", forHTTPHeaderField: "x-ms-date") // RFC 1123 // prepare the authentication provider let provider = SharedKeyAuthenticationProvider(base64Key: "TiR0p2ZwnUuBGBEDU5LADWBXpxXy3Y9Aq4Fb1nD+6CM=") @@ -69,7 +69,8 @@ class SharedKeyProviderTests: XCTestCase { XCTAssertNotNil(headerValue) let formatter = DateFormatter() formatter.timeZone = TimeZone(identifier: "UTC") - formatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss 'GMT'" // RFC + formatter.locale = Locale(identifier: "en_US_POSIX") // consistent regardless of the user's device + formatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss 'GMT'" // RFC 1123 let date = formatter.date(from: headerValue!) XCTAssertNotNil(date) XCTAssertTrue(date! < Date())