|
| 1 | +// |
| 2 | +// Copyright Amazon.com Inc. or its affiliates. |
| 3 | +// All Rights Reserved. |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: Apache-2.0 |
| 6 | +// |
| 7 | + |
| 8 | +@testable import Amplify |
| 9 | + |
| 10 | +import AWSS3StoragePlugin |
| 11 | +import ClientRuntime |
| 12 | +import CryptoKit |
| 13 | +import XCTest |
| 14 | + |
| 15 | +class AWSS3StoragePluginUploadIntegrationTests: AWSS3StoragePluginTestBase { |
| 16 | + |
| 17 | + var uploadedKeys: [String]! |
| 18 | + |
| 19 | + /// Represents expected pieces of the User-Agent header of an SDK http request. |
| 20 | + /// |
| 21 | + /// Example SDK User-Agent: |
| 22 | + /// ``` |
| 23 | + /// User-Agent: aws-sdk-swift/1.0 api/s3/1.0 os/iOS/16.4.0 lang/swift/5.8 |
| 24 | + /// ``` |
| 25 | + /// - Tag: SdkUserAgentComponent |
| 26 | + private enum SdkUserAgentComponent: String, CaseIterable { |
| 27 | + case api = "api/s3" |
| 28 | + case lang = "lang/swift" |
| 29 | + case os = "os/" |
| 30 | + case sdk = "aws-sdk-swift/" |
| 31 | + } |
| 32 | + |
| 33 | + /// Represents expected pieces of the User-Agent header of an URLRequest used for uploading or |
| 34 | + /// downloading. |
| 35 | + /// |
| 36 | + /// Example SDK User-Agent: |
| 37 | + /// ``` |
| 38 | + /// User-Agent: lib/amplify-swift |
| 39 | + /// ``` |
| 40 | + /// - Tag: SdkUserAgentComponent |
| 41 | + private enum URLUserAgentComponent: String, CaseIterable { |
| 42 | + case lib = "lib/amplify-swift" |
| 43 | + case os = "os/" |
| 44 | + } |
| 45 | + |
| 46 | + override func setUp() async throws { |
| 47 | + try await super.setUp() |
| 48 | + uploadedKeys = [] |
| 49 | + } |
| 50 | + |
| 51 | + override func tearDown() async throws { |
| 52 | + for key in uploadedKeys { |
| 53 | + _ = try await Amplify.Storage.remove(path: .fromString("public/\(key)")) |
| 54 | + } |
| 55 | + uploadedKeys = nil |
| 56 | + try await super.tearDown() |
| 57 | + } |
| 58 | + |
| 59 | + /// Given: An data object |
| 60 | + /// When: Upload the data |
| 61 | + /// Then: The operation completes successfully |
| 62 | + func testUploadData() async throws { |
| 63 | + let key = UUID().uuidString |
| 64 | + let data = Data(key.utf8) |
| 65 | + |
| 66 | + _ = try await Amplify.Storage.uploadData(path: .fromString("public/\(key)"), data: data, options: nil).value |
| 67 | + _ = try await Amplify.Storage.remove(path: .fromString("public/\(key)")) |
| 68 | + |
| 69 | + // Only the remove operation results in an SDK request |
| 70 | + XCTAssertEqual(requestRecorder.sdkRequests.map { $0.method } , [.delete]) |
| 71 | + try assertUserAgentComponents(sdkRequests: requestRecorder.sdkRequests) |
| 72 | + |
| 73 | + XCTAssertEqual(requestRecorder.urlRequests.map { $0.httpMethod }, ["PUT"]) |
| 74 | + try assertUserAgentComponents(urlRequests: requestRecorder.urlRequests) |
| 75 | + } |
| 76 | + |
| 77 | + /// Given: A empty data object |
| 78 | + /// When: Upload the data |
| 79 | + /// Then: The operation completes successfully |
| 80 | + func testUploadEmptyData() async throws { |
| 81 | + let key = UUID().uuidString |
| 82 | + let data = Data("".utf8) |
| 83 | + _ = try await Amplify.Storage.uploadData(path: .fromString("public/\(key)"), data: data, options: nil).value |
| 84 | + _ = try await Amplify.Storage.remove(path: .fromString("public/\(key)")) |
| 85 | + |
| 86 | + XCTAssertEqual(requestRecorder.urlRequests.map { $0.httpMethod }, ["PUT"]) |
| 87 | + try assertUserAgentComponents(urlRequests: requestRecorder.urlRequests) |
| 88 | + } |
| 89 | + |
| 90 | + /// Given: A file with contents |
| 91 | + /// When: Upload the file |
| 92 | + /// Then: The operation completes successfully and all URLSession and SDK requests include a user agent |
| 93 | + func testUploadFile() async throws { |
| 94 | + let key = UUID().uuidString |
| 95 | + let filePath = NSTemporaryDirectory() + key + ".tmp" |
| 96 | + |
| 97 | + let fileURL = URL(fileURLWithPath: filePath) |
| 98 | + FileManager.default.createFile(atPath: filePath, contents: Data(key.utf8), attributes: nil) |
| 99 | + |
| 100 | + _ = try await Amplify.Storage.uploadFile(path: .fromString("public/\(key)"), local: fileURL, options: nil).value |
| 101 | + _ = try await Amplify.Storage.remove(path: .fromString("public/\(key)")) |
| 102 | + |
| 103 | + // Only the remove operation results in an SDK request |
| 104 | + XCTAssertEqual(requestRecorder.sdkRequests.map { $0.method} , [.delete]) |
| 105 | + try assertUserAgentComponents(sdkRequests: requestRecorder.sdkRequests) |
| 106 | + |
| 107 | + XCTAssertEqual(requestRecorder.urlRequests.map { $0.httpMethod }, ["PUT"]) |
| 108 | + try assertUserAgentComponents(urlRequests: requestRecorder.urlRequests) |
| 109 | + } |
| 110 | + |
| 111 | + /// Given: A file with empty contents |
| 112 | + /// When: Upload the file |
| 113 | + /// Then: The operation completes successfully |
| 114 | + func testUploadFileEmptyData() async throws { |
| 115 | + let key = UUID().uuidString |
| 116 | + let filePath = NSTemporaryDirectory() + key + ".tmp" |
| 117 | + let fileURL = URL(fileURLWithPath: filePath) |
| 118 | + FileManager.default.createFile(atPath: filePath, contents: Data("".utf8), attributes: nil) |
| 119 | + |
| 120 | + _ = try await Amplify.Storage.uploadFile(path: .fromString("public/\(key)"), local: fileURL, options: nil).value |
| 121 | + _ = try await Amplify.Storage.remove(path: .fromString("public/\(key)")) |
| 122 | + |
| 123 | + XCTAssertEqual(requestRecorder.urlRequests.map { $0.httpMethod }, ["PUT"]) |
| 124 | + try assertUserAgentComponents(urlRequests: requestRecorder.urlRequests) |
| 125 | + } |
| 126 | + |
| 127 | + /// Given: A large data object |
| 128 | + /// When: Upload the data |
| 129 | + /// Then: The operation completes successfully |
| 130 | + func testUploadLargeData() async throws { |
| 131 | + let key = "public/" + UUID().uuidString |
| 132 | + |
| 133 | + let uploadKey = try await Amplify.Storage.uploadData(path: .fromString(key), |
| 134 | + data: AWSS3StoragePluginTestBase.largeDataObject, |
| 135 | + options: nil).value |
| 136 | + XCTAssertEqual(uploadKey, key) |
| 137 | + |
| 138 | + try await Amplify.Storage.remove(path: .fromString(key)) |
| 139 | + |
| 140 | + let userAgents = requestRecorder.urlRequests.compactMap { $0.allHTTPHeaderFields?["User-Agent"] } |
| 141 | + XCTAssertGreaterThan(userAgents.count, 1) |
| 142 | + for userAgent in userAgents { |
| 143 | + let expectedComponent = "MultiPart/UploadPart" |
| 144 | + XCTAssertTrue(userAgent.contains(expectedComponent), "\(userAgent) does not contain \(expectedComponent)") |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /// Given: A large file |
| 149 | + /// When: Upload the file |
| 150 | + /// Then: The operation completes successfully |
| 151 | + func testUploadLargeFile() async throws { |
| 152 | + let key = UUID().uuidString |
| 153 | + let filePath = NSTemporaryDirectory() + key + ".tmp" |
| 154 | + let fileURL = URL(fileURLWithPath: filePath) |
| 155 | + |
| 156 | + FileManager.default.createFile(atPath: filePath, |
| 157 | + contents: AWSS3StoragePluginTestBase.largeDataObject, |
| 158 | + attributes: nil) |
| 159 | + |
| 160 | + _ = try await Amplify.Storage.uploadFile(path: .fromString("public/\(key)"), local: fileURL, options: nil).value |
| 161 | + _ = try await Amplify.Storage.remove(path: .fromString("public/\(key)")) |
| 162 | + |
| 163 | + let userAgents = requestRecorder.urlRequests.compactMap { $0.allHTTPHeaderFields?["User-Agent"] } |
| 164 | + XCTAssertGreaterThan(userAgents.count, 1) |
| 165 | + for userAgent in userAgents { |
| 166 | + let expectedComponent = "MultiPart/UploadPart" |
| 167 | + XCTAssertTrue(userAgent.contains(expectedComponent), "\(userAgent) does not contain \(expectedComponent)") |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + func removeIfExists(_ fileURL: URL) { |
| 172 | + let fileExists = FileManager.default.fileExists(atPath: fileURL.path) |
| 173 | + if fileExists { |
| 174 | + do { |
| 175 | + try FileManager.default.removeItem(at: fileURL) |
| 176 | + } catch { |
| 177 | + XCTFail("Failed to delete file at \(fileURL)") |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + private func assertUserAgentComponents(sdkRequests: [SdkHttpRequest], file: StaticString = #filePath, line: UInt = #line) throws { |
| 183 | + for request in sdkRequests { |
| 184 | + let headers = request.headers.dictionary |
| 185 | + let userAgent = try XCTUnwrap(headers["User-Agent"]?.joined(separator:",")) |
| 186 | + for component in SdkUserAgentComponent.allCases { |
| 187 | + XCTAssertTrue(userAgent.contains(component.rawValue), "\(userAgent.description) does not contain \(component)", file: file, line: line) |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + private func assertUserAgentComponents(urlRequests: [URLRequest], file: StaticString = #filePath, line: UInt = #line) throws { |
| 193 | + for request in urlRequests { |
| 194 | + let headers = try XCTUnwrap(request.allHTTPHeaderFields) |
| 195 | + let userAgent = try XCTUnwrap(headers["User-Agent"]) |
| 196 | + for component in URLUserAgentComponent.allCases { |
| 197 | + XCTAssertTrue(userAgent.contains(component.rawValue), "\(userAgent.description) does not contain \(component)", file: file, line: line) |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments