Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix looking up multipart form temporary files #781

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions Tests/WordPressKitTests/Tests/Utilities/URLSessionHelperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ class URLSessionHelperTests: XCTestCase {
}

// Capture a list of files in temp dirs, before calling the upload function.
let tempFilesBeforeUpload = existingTempFiles()
let tempFilesBeforeUpload = try existingMultipartFormTempFiles()

// Perform upload HTTP request
let builder = try HTTPRequestBuilder(url: URL(string: "https://wordpress.org/upload")!)
Expand All @@ -278,7 +278,7 @@ class URLSessionHelperTests: XCTestCase {
let _ = await session.perform(request: builder, errorType: TestError.self)

// Capture a list of files in the temp dirs, after calling the upload function.
let tempFilesAfterUpload = existingTempFiles()
let tempFilesAfterUpload = try existingMultipartFormTempFiles()

// There should be no new files after the HTTP request returns. This assertion relies on an implementation detail
// where the multipart form content is put into a file in temp dirs.
Expand All @@ -299,7 +299,7 @@ class URLSessionHelperTests: XCTestCase {
}

// Capture a list of files in temp dirs, before calling the upload function.
let tempFilesBeforeUpload = existingTempFiles()
let tempFilesBeforeUpload = try existingMultipartFormTempFiles()

// Perform upload HTTP request
let builder = try HTTPRequestBuilder(url: URL(string: "https://wordpress.org/upload")!)
Expand All @@ -308,28 +308,21 @@ class URLSessionHelperTests: XCTestCase {
let _ = await session.perform(request: builder, errorType: TestError.self)

// Capture a list of files in the temp dirs, after calling the upload function.
let tempFilesAfterUpload = existingTempFiles()
let tempFilesAfterUpload = try existingMultipartFormTempFiles()

// There should be no new files after the HTTP request returns. This assertion relies on an implementation detail
// where the multipart form content is put into a file in temp dirs.
let newFiles = tempFilesAfterUpload.subtracting(tempFilesBeforeUpload)
XCTAssertEqual(newFiles.count, 0)
}

private func existingTempFiles() -> Set<String> {
// This functions finds temp files that are used for uploading multipart form.
// The implementation relies on an internal implementation detail of building multipart form content.
private func existingMultipartFormTempFiles() throws -> Set<String> {
let fm = FileManager.default
let enumerators = [
fm.enumerator(atPath: NSTemporaryDirectory()),
fm.enumerator(atPath: fm.temporaryDirectory.path)
].compactMap { $0 }

var result: Set<String> = []
for enumerator in enumerators {
while let file = enumerator.nextObject() as? String {
result.insert(file)
}
}
return result
let files = try fm.contentsOfDirectory(atPath: fm.temporaryDirectory.path)
.filter { UUID(uuidString: $0) != nil }
return Set(files)
}

private func createLargeFile(megaBytes: Int) throws -> URL {
Expand Down