Skip to content

Commit

Permalink
Fix looking up multipart form temporary files (#781)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazytonyli authored Apr 8, 2024
2 parents 5bf57f8 + a7f9297 commit ff552e0
Showing 1 changed file with 10 additions and 17 deletions.
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

0 comments on commit ff552e0

Please sign in to comment.