Skip to content

Commit 158bcb3

Browse files
authored
feat(api): Update url cache for storage and api to nil (#2647)
Updates the cache control of Storage and API category to disable caching by default. This can be overridden for API Rest request by setting the Cache-Control request header.
1 parent 1e91890 commit 158bcb3

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

Amplify/Categories/API/Request/RESTRequest.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ public class RESTRequest {
3232
headers: [String: String]? = nil,
3333
queryParameters: [String: String]? = nil,
3434
body: Data? = nil) {
35+
let inputHeaders = headers ?? [:]
36+
self.headers = inputHeaders.merging(
37+
["Cache-Control": "no-store"],
38+
uniquingKeysWith: { current, _ in current}
39+
)
3540
self.apiName = apiName
3641
self.path = path
37-
self.headers = headers
3842
self.queryParameters = queryParameters
3943
self.body = body
4044
}

AmplifyPlugins/API/Sources/AWSAPIPlugin/Support/Utils/GraphQLOperationRequestUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class GraphQLOperationRequestUtils {
2222
// Construct a graphQL specific HTTP POST request with the request payload
2323
static func constructRequest(with baseUrl: URL, requestPayload: Data) -> URLRequest {
2424
var baseRequest = URLRequest(url: baseUrl)
25-
let headers = ["content-type": "application/json"]
25+
let headers = ["content-type": "application/json", "Cache-Control": "no-store"]
2626
baseRequest.allHTTPHeaderFields = headers
2727
baseRequest.httpMethod = "POST"
2828
baseRequest.httpBody = requestPayload
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
import XCTest
8+
import Amplify
9+
@testable import AWSAPIPlugin
10+
11+
class GraphQLOperationRequestUtilsTests: XCTestCase {
12+
13+
let baseURL = URL(string: "https://someurl")!
14+
let testDocument = "testDocument"
15+
16+
func testGraphQLOperationRequestWithCache() throws {
17+
let request = GraphQLOperationRequestUtils.constructRequest(with: baseURL,
18+
requestPayload: Data())
19+
XCTAssertEqual(request.allHTTPHeaderFields?["Cache-Control"], "no-store")
20+
}
21+
}

AmplifyPlugins/Storage/Sources/AWSS3StoragePlugin/Service/Storage/AWSS3StorageService.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class AWSS3StorageService: AWSS3StorageServiceBehaviour, StorageServiceProxy {
6666
#else
6767
let sessionConfiguration = URLSessionConfiguration.background(withIdentifier: storageConfiguration.sessionIdentifier)
6868
#endif
69+
sessionConfiguration.urlCache = nil
6970
sessionConfiguration.allowsCellularAccess = storageConfiguration.allowsCellularAccess
7071
sessionConfiguration.timeoutIntervalForResource = TimeInterval(storageConfiguration.timeoutIntervalForResource)
7172
_sessionConfiguration = sessionConfiguration

AmplifyTests/CategoryTests/API/APICategoryClientRESTTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ class APICategoryClientRESTTests: XCTestCase {
4444
await waitForExpectations(timeout: 0.5)
4545
}
4646

47+
func testCacheInRequest() {
48+
let request = RESTRequest(apiName: "someapi")
49+
XCTAssertEqual(request.headers?["Cache-Control"], "no-store")
50+
}
51+
52+
func testCustomCacheInRequest() {
53+
let request = RESTRequest(apiName: "someapi", headers: ["Cache-Control": "private"])
54+
XCTAssertEqual(request.headers?["Cache-Control"], "private")
55+
}
56+
57+
func testCacheWithExistingValuesInRequest() {
58+
let request = RESTRequest(apiName: "someapi", headers: ["somekey": "somevalue"])
59+
XCTAssertEqual(request.headers?["Cache-Control"], "no-store")
60+
XCTAssertEqual(request.headers?["somekey"], "somevalue")
61+
}
62+
4763
// MARK: - Utilities
4864

4965
func makeAndAddMockPlugin() throws -> MockAPICategoryPlugin {

0 commit comments

Comments
 (0)