func testS3UploadUsingParameters()

in AWSAppSyncIntegrationTests/AWSAppSyncCognitoAuthTests.swift [56:112]


    func testS3UploadUsingParameters() throws {
        let testBundle = Bundle(for: AWSAppSyncCognitoAuthTests.self)
        let testConfiguration = AppSyncClientTestConfiguration(with: testBundle)!
        let appSyncClient = try makeS3EnabledAppSyncClient(testConfiguration: testConfiguration, testBundle: testBundle)

        // Note "public" prefix. See https://aws-amplify.github.io/docs/js/storage#using-amazon-s3
        let objectKey = "public/testS3Object-\(UUID().uuidString).jpg"
        let localURL = testBundle.url(forResource: "testS3Object", withExtension: ".jpg")!

        let region = AWSEndpoint.regionName(from: testConfiguration.bucketRegion)!

        let postCreated = expectation(description: "Post created successfully.")

        let s3ObjectInput = S3ObjectInput(
            bucket: testConfiguration.bucketName,
            key: objectKey,
            region: region,
            localUri: localURL.path,
            mimeType: "image/jpeg")

        let createPostWithFile = CreatePostWithFileUsingParametersMutation(
            author: "Test S3 Object Author",
            title: "Test S3 Object Upload",
            content: "Testing S3 object upload",
            url: "http://www.example.testing.com",
            ups: 0,
            downs: 0,
            file: s3ObjectInput)

        var mutationResult: GraphQLResult<CreatePostWithFileUsingParametersMutation.Data>? = nil
        appSyncClient.perform(mutation: createPostWithFile,
                              queue: AWSAppSyncCognitoAuthTests.mutationQueue,
                              resultHandler:  { result, error in
                                XCTAssertNil(error)
                                mutationResult = result
                                postCreated.fulfill()
                              })

        wait(for: [postCreated], timeout: AWSAppSyncCognitoAuthTests.networkOperationTimeout)

        guard let postId = mutationResult?.data?.createPostWithFileUsingParameters?.id else {
            XCTFail("Mutation result unexpectedly has nil ID")
            return
        }

        let destinationURL: URL
        do {
            destinationURL = try downloadFile(from: postId, with: appSyncClient)
        } catch {
            XCTFail("Error downloading file from \(postId): \(error.localizedDescription)")
            return
        }

        let file1Data = try Data(contentsOf: destinationURL)
        let file2Data = try Data(contentsOf: localURL)
        XCTAssertEqual(file1Data, file2Data)
    }