Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
sebaland committed Sep 6, 2024
1 parent eca7cfe commit 546464e
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 50 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build_amplify_swift_platforms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ on:
required: true
default: true
type: boolean
push:
branches-ignore:
- main
- release
# push:
# branches-ignore:
# - main
# - release

permissions:
contents: read
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/fortify_scan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ on:
required: true
type: string

push:
branches-ignore:
- main
- release
# push:
# branches-ignore:
# - main
# - release

permissions:
id-token: write
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/unit_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ on:
required: true
default: true
type: boolean
push:
branches-ignore:
- main
- release
# push:
# branches-ignore:
# - main
# - release

permissions:
contents: read
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extension UploadPartInput {
}
builder.interceptors.add(ClientRuntime.URLPathMiddleware<UploadPartInput, UploadPartOutput>(UploadPartInput.urlPathProvider(_:)))
builder.interceptors.add(ClientRuntime.URLHostMiddleware<UploadPartInput, UploadPartOutput>())
builder.deserialize(ClientRuntime.DeserializeMiddleware<UploadPartOutput>(UploadPartOutput.httpOutput(from:), PutObjectOutputError.httpError(from:)))
builder.deserialize(ClientRuntime.DeserializeMiddleware<UploadPartOutput>(UploadPartOutput.httpOutput(from:), UploadPartOutputError.httpError(from:)))
builder.interceptors.add(ClientRuntime.LoggerMiddleware<UploadPartInput, UploadPartOutput>(clientLogMode: config.clientLogMode))
builder.retryStrategy(SmithyRetries.DefaultRetryStrategy(options: config.retryStrategyOptions))
builder.retryErrorInfoProvider(AWSClientRuntime.AWSRetryErrorInfoProvider.errorInfo(for:))
Expand All @@ -62,6 +62,7 @@ extension UploadPartInput {
builder.selectAuthScheme(ClientRuntime.AuthSchemeMiddleware<UploadPartOutput>())
builder.interceptors.add(AWSClientRuntime.AWSS3ErrorWith200StatusXMLMiddleware<UploadPartInput, UploadPartOutput>())
builder.interceptors.add(AWSClientRuntime.FlexibleChecksumsRequestMiddleware<UploadPartInput, UploadPartOutput>(checksumAlgorithm: input.checksumAlgorithm?.rawValue))
builder.serialize(UploadPartPresignedMiddleware())
var metricsAttributes = Smithy.Attributes()
metricsAttributes.set(key: ClientRuntime.OrchestratorMetricsAttributesKeys.service, value: "S3")
metricsAttributes.set(key: ClientRuntime.OrchestratorMetricsAttributesKeys.method, value: "UploadPart")
Expand All @@ -76,33 +77,35 @@ extension UploadPartInput {
.build()
return try await op.presignRequest(input: input).endpoint.url
}

static func urlPathProvider(_ value: UploadPartInput) -> Swift.String? {
guard let key = value.key else {
return nil
}
return "/\(key.urlPercentEncoding(encodeForwardSlash: false))"
}

}

extension UploadPartInput {
struct UploadPartPresignedMiddleware: Smithy.RequestMessageSerializer {
typealias InputType = UploadPartInput
typealias RequestType = SmithyHTTPAPI.HTTPRequest

let id: Swift.String = "UploadPartPresignedMiddleware"

static func queryItemProvider(_ value: UploadPartInput) throws -> [URIQueryItem] {
var items = [URIQueryItem]()
items.append(URIQueryItem(name: "x-id", value: "UploadPart"))
guard let partNumber = value.partNumber else {
func apply(input: InputType, builder: SmithyHTTPAPI.HTTPRequestBuilder, attributes: Smithy.Context) throws {
builder.withQueryItem(.init(
name: "x-id",
value: "UploadPart")
)
guard let partNumber = input.partNumber else {
let message = "Creating a URL Query Item failed. partNumber is required and must not be nil."
throw ClientError.unknownError(message)
throw ClientError.invalidValue(message)
}
let partNumberQueryItem = URIQueryItem(name: "partNumber".urlPercentEncoding(), value: Swift.String(partNumber).urlPercentEncoding())
items.append(partNumberQueryItem)
guard let uploadId = value.uploadId else {
builder.withQueryItem(.init(
name: "partNumber".urlPercentEncoding(),
value: Swift.String(partNumber).urlPercentEncoding())
)

guard let uploadId = input.uploadId else {
let message = "Creating a URL Query Item failed. uploadId is required and must not be nil."
throw ClientError.unknownError(message)
throw ClientError.invalidValue(message)
}
let uploadIdQueryItem = URIQueryItem(name: "uploadId".urlPercentEncoding(), value: Swift.String(uploadId).urlPercentEncoding())
items.append(uploadIdQueryItem)
return items
builder.withQueryItem(.init(
name: "uploadId".urlPercentEncoding(),
value: Swift.String(uploadId).urlPercentEncoding())
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class StorageTransferResponse {
} else if 400 == statusCode {
// 400 is a bad request
result = false
} else if 403 == statusCode {
// 403 is due to lack of permissions
result = false
} else if (transferTask.responseText ?? "").isEmpty {
// If we didn't get any more info from the server, error is retriable
result = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ class AWSS3StoragePluginBasicIntegrationTests: AWSS3StoragePluginTestBase {
let key = UUID().uuidString
let data = Data(key.utf8)

_ = try await Amplify.Storage.uploadData(key: key, data: data, options: nil).value
_ = try await Amplify.Storage.remove(key: key)
await waitTasks(timeout: 5) {
_ = try await Amplify.Storage.uploadData(key: key, data: data, options: nil).value
_ = try await Amplify.Storage.remove(key: key)
}

// Only the remove operation results in an SDK request
XCTAssertEqual(requestRecorder.sdkRequests.map { $0.method } , [.delete])
Expand All @@ -80,8 +82,10 @@ class AWSS3StoragePluginBasicIntegrationTests: AWSS3StoragePluginTestBase {
func testUploadEmptyData() async throws {
let key = UUID().uuidString
let data = Data("".utf8)
_ = try await Amplify.Storage.uploadData(key: key, data: data, options: nil).value
_ = try await Amplify.Storage.remove(key: key)
await waitTasks(timeout: 5) {
_ = try await Amplify.Storage.uploadData(key: key, data: data, options: nil).value
_ = try await Amplify.Storage.remove(key: key)
}

XCTAssertEqual(requestRecorder.urlRequests.map { $0.httpMethod }, ["PUT"])
try assertUserAgentComponents(urlRequests: requestRecorder.urlRequests)
Expand All @@ -97,8 +101,10 @@ class AWSS3StoragePluginBasicIntegrationTests: AWSS3StoragePluginTestBase {
let fileURL = URL(fileURLWithPath: filePath)
FileManager.default.createFile(atPath: filePath, contents: Data(key.utf8), attributes: nil)

_ = try await Amplify.Storage.uploadFile(key: key, local: fileURL, options: nil).value
_ = try await Amplify.Storage.remove(key: key)
await waitTasks(timeout: 5) {
_ = try await Amplify.Storage.uploadFile(key: key, local: fileURL, options: nil).value
_ = try await Amplify.Storage.remove(key: key)
}

// Only the remove operation results in an SDK request
XCTAssertEqual(requestRecorder.sdkRequests.map { $0.method} , [.delete])
Expand All @@ -117,8 +123,10 @@ class AWSS3StoragePluginBasicIntegrationTests: AWSS3StoragePluginTestBase {
let fileURL = URL(fileURLWithPath: filePath)
FileManager.default.createFile(atPath: filePath, contents: Data("".utf8), attributes: nil)

_ = try await Amplify.Storage.uploadFile(key: key, local: fileURL, options: nil).value
_ = try await Amplify.Storage.remove(key: key)
await waitTasks(timeout: 5) {
_ = try await Amplify.Storage.uploadFile(key: key, local: fileURL, options: nil).value
_ = try await Amplify.Storage.remove(key: key)
}

XCTAssertEqual(requestRecorder.urlRequests.map { $0.httpMethod }, ["PUT"])
try assertUserAgentComponents(urlRequests: requestRecorder.urlRequests)
Expand All @@ -130,12 +138,13 @@ class AWSS3StoragePluginBasicIntegrationTests: AWSS3StoragePluginTestBase {
func testUploadLargeData() async throws {
let key = UUID().uuidString

let uploadKey = try await Amplify.Storage.uploadData(key: key,
data: AWSS3StoragePluginTestBase.largeDataObject,
options: nil).value
XCTAssertEqual(uploadKey, key)

try await Amplify.Storage.remove(key: key)
await waitTasks(timeout: 10) {
let uploadKey = try await Amplify.Storage.uploadData(key: key,
data: AWSS3StoragePluginTestBase.largeDataObject,
options: nil).value
XCTAssertEqual(uploadKey, key)
try await Amplify.Storage.remove(key: key)
}

let userAgents = requestRecorder.urlRequests.compactMap { $0.allHTTPHeaderFields?["User-Agent"] }
XCTAssertGreaterThan(userAgents.count, 1)
Expand All @@ -157,8 +166,10 @@ class AWSS3StoragePluginBasicIntegrationTests: AWSS3StoragePluginTestBase {
contents: AWSS3StoragePluginTestBase.largeDataObject,
attributes: nil)

_ = try await Amplify.Storage.uploadFile(key: key, local: fileURL, options: nil).value
_ = try await Amplify.Storage.remove(key: key)
await waitTasks(timeout: 10) {
_ = try await Amplify.Storage.uploadFile(key: key, local: fileURL, options: nil).value
_ = try await Amplify.Storage.remove(key: key)
}

let userAgents = requestRecorder.urlRequests.compactMap { $0.allHTTPHeaderFields?["User-Agent"] }
XCTAssertGreaterThan(userAgents.count, 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,20 @@ class AWSS3StoragePluginTestBase: XCTestCase {
}
}

func waitTasks(timeout: TimeInterval, closure: @escaping () async throws -> ()) async {
let expectation = expectation(description: "Operation expectation")
Task {
defer { expectation.fulfill() }
do {
try await closure()
} catch {
XCTFail("Unexpected error: \(error)")
}
}

await fulfillment(of: [expectation], timeout: timeout)
}

private func invalidateCurrentSession() {
Self.logger.debug("Invalidating URLSession")
guard let plugin = try? Amplify.Storage.getPlugin(for: "awsS3StoragePlugin") as? AWSS3StoragePlugin,
Expand Down

0 comments on commit 546464e

Please sign in to comment.