11import Foundation
2+ #if canImport(FoundationNetworking)
3+ import FoundationNetworking
4+ #endif
25import Testing
36import HTTPTypes
47@testable import Simplicity
@@ -10,7 +13,7 @@ struct CacheMiddlewareTests {
1013 @Test
1114 func testReturnCacheDataElseLoad_returnsCachedResponse_whenAvailable( ) async throws {
1215 // Arrange
13- let cache = URLCache ( memoryCapacity : 10_000_000 , diskCapacity : 0 )
16+ let cache = makeTestCache ( )
1417 let middleware = CacheMiddleware ( urlCache: cache)
1518 let url = baseURL. appending ( path: " /test " )
1619 let cachedData = Data ( " cached " . utf8)
@@ -43,7 +46,7 @@ struct CacheMiddlewareTests {
4346 @Test
4447 func testReturnCacheDataElseLoad_callsNetwork_whenNotCached( ) async throws {
4548 // Arrange
46- let cache = URLCache ( memoryCapacity : 10_000_000 , diskCapacity : 0 )
49+ let cache = makeTestCache ( )
4750 let middleware = CacheMiddleware ( urlCache: cache)
4851 let url = baseURL. appending ( path: " /test " )
4952 let networkData = Data ( " network " . utf8)
@@ -72,7 +75,7 @@ struct CacheMiddlewareTests {
7275 @Test
7376 func testReturnCacheDataDontLoad_throwsCacheMiss_whenNotCached( ) async throws {
7477 // Arrange
75- let cache = URLCache ( memoryCapacity : 10_000_000 , diskCapacity : 0 )
78+ let cache = makeTestCache ( )
7679 let middleware = CacheMiddleware ( urlCache: cache)
7780 let url = baseURL. appending ( path: " /test " )
7881
@@ -106,7 +109,7 @@ struct CacheMiddlewareTests {
106109 @Test
107110 func testReturnCacheDataDontLoad_returnsCached_whenAvailable( ) async throws {
108111 // Arrange
109- let cache = URLCache ( memoryCapacity : 10_000_000 , diskCapacity : 0 )
112+ let cache = makeTestCache ( )
110113 let middleware = CacheMiddleware ( urlCache: cache)
111114 let url = baseURL. appending ( path: " /test " )
112115 let cachedData = Data ( " cached " . utf8)
@@ -138,7 +141,7 @@ struct CacheMiddlewareTests {
138141 @Test
139142 func testReloadIgnoringLocalCacheData_alwaysCallsNetwork( ) async throws {
140143 // Arrange
141- let cache = URLCache ( memoryCapacity : 10_000_000 , diskCapacity : 0 )
144+ let cache = makeTestCache ( )
142145 let middleware = CacheMiddleware ( urlCache: cache)
143146 let url = baseURL. appending ( path: " /test " )
144147 let cachedData = Data ( " cached " . utf8)
@@ -171,7 +174,7 @@ struct CacheMiddlewareTests {
171174 @Test
172175 func testCachesSuccessResponsesByDefault( ) async throws {
173176 // Arrange
174- let cache = URLCache ( memoryCapacity : 10_000_000 , diskCapacity : 0 )
177+ let cache = makeTestCache ( )
175178 let middleware = CacheMiddleware ( urlCache: cache)
176179 let url = baseURL. appending ( path: " /test " )
177180 let networkData = Data ( " network " . utf8)
@@ -201,7 +204,7 @@ struct CacheMiddlewareTests {
201204 @Test
202205 func testDoesNotCacheFailureResponsesByDefault( ) async throws {
203206 // Arrange
204- let cache = URLCache ( memoryCapacity : 10_000_000 , diskCapacity : 0 )
207+ let cache = makeTestCache ( )
205208 let middleware = CacheMiddleware ( urlCache: cache)
206209 let url = baseURL. appending ( path: " /test " )
207210 let errorData = Data ( " error " . utf8)
@@ -231,7 +234,7 @@ struct CacheMiddlewareTests {
231234 @Test
232235 func testCustomShouldCacheResponse_allowsCachingFailures( ) async throws {
233236 // Arrange
234- let cache = URLCache ( memoryCapacity : 10_000_000 , diskCapacity : 0 )
237+ let cache = makeTestCache ( )
235238 let middleware = CacheMiddleware ( urlCache: cache) { _ in true } // Cache everything
236239 let url = baseURL. appending ( path: " /test " )
237240 let errorData = Data ( " error " . utf8)
@@ -261,7 +264,7 @@ struct CacheMiddlewareTests {
261264 @Test
262265 func testDifferentQueryParams_differentCacheEntries( ) async throws {
263266 // Arrange
264- let cache = URLCache ( memoryCapacity : 10_000_000 , diskCapacity : 0 )
267+ let cache = makeTestCache ( )
265268 let middleware = CacheMiddleware ( urlCache: cache)
266269 let data1 = Data ( " data1 " . utf8)
267270 let data2 = Data ( " data2 " . utf8)
@@ -300,8 +303,8 @@ struct CacheMiddlewareTests {
300303 #expect( response2. body == data2)
301304
302305 // Verify both are now cached separately
303- let url1 = baseURL . appending ( path: " /test " ) . appending ( queryItems: [ URLQueryItem ( name: " filter " , value: " a " ) ] )
304- let url2 = baseURL . appending ( path: " /test " ) . appending ( queryItems: [ URLQueryItem ( name: " filter " , value: " b " ) ] )
306+ let url1 = makeURL ( base : baseURL , path: " /test " , queryItems: [ URLQueryItem ( name: " filter " , value: " a " ) ] )
307+ let url2 = makeURL ( base : baseURL , path: " /test " , queryItems: [ URLQueryItem ( name: " filter " , value: " b " ) ] )
305308 #expect( await middleware. hasCachedResponse ( for: url1) )
306309 #expect( await middleware. hasCachedResponse ( for: url2) )
307310
@@ -329,7 +332,7 @@ struct CacheMiddlewareTests {
329332 @Test
330333 func testRemoveCached_invalidatesEntry( ) async throws {
331334 // Arrange
332- let cache = URLCache ( memoryCapacity : 10_000_000 , diskCapacity : 0 )
335+ let cache = makeTestCache ( )
333336 let middleware = CacheMiddleware ( urlCache: cache)
334337 let url = baseURL. appending ( path: " /test " )
335338 let cachedData = Data ( " cached " . utf8)
@@ -347,7 +350,7 @@ struct CacheMiddlewareTests {
347350 @Test
348351 func testClearCache_removesAllEntries( ) async throws {
349352 // Arrange
350- let cache = URLCache ( memoryCapacity : 10_000_000 , diskCapacity : 0 )
353+ let cache = makeTestCache ( )
351354 let middleware = CacheMiddleware ( urlCache: cache)
352355 let url1 = baseURL. appending ( path: " /test1 " )
353356 let url2 = baseURL. appending ( path: " /test2 " )
@@ -369,17 +372,22 @@ struct CacheMiddlewareTests {
369372
370373// MARK: - Test Helpers
371374
375+ private func makeTestCache( ) -> URLCache {
376+ #if canImport(FoundationNetworking)
377+ URLCache ( memoryCapacity: 10_000_000 , diskCapacity: 0 , diskPath: nil )
378+ #else
379+ URLCache ( memoryCapacity: 10_000_000 , diskCapacity: 0 )
380+ #endif
381+ }
382+
372383/// Constructs a `MiddlewareRequest` for testing purposes.
373384private func makeMiddlewareRequest(
374385 baseURL: URL ,
375386 path: String ,
376387 queryItems: [ URLQueryItem ] = [ ] ,
377388 cachePolicy: CachePolicy
378389) -> MiddlewareRequest {
379- var url = baseURL. appending ( path: path)
380- if !queryItems. isEmpty {
381- url = url. appending ( queryItems: queryItems)
382- }
390+ let url = makeURL ( base: baseURL, path: path, queryItems: queryItems)
383391 let httpRequest = HTTPRequest ( method: . get, url: url, headerFields: HTTPFields ( ) )
384392 return MiddlewareRequest (
385393 httpRequest: httpRequest,
@@ -389,3 +397,11 @@ private func makeMiddlewareRequest(
389397 cachePolicy: cachePolicy
390398 )
391399}
400+
401+ private func makeURL( base: URL , path: String , queryItems: [ URLQueryItem ] = [ ] ) -> URL {
402+ let url = base. appending ( path: path)
403+ guard !queryItems. isEmpty else { return url }
404+ var components = URLComponents ( url: url, resolvingAgainstBaseURL: false ) !
405+ components. queryItems = queryItems
406+ return components. url!
407+ }
0 commit comments