From 3aad06ab8680567ee3a1c7c67eb79888b92186fd Mon Sep 17 00:00:00 2001 From: Tony Li Date: Thu, 22 Feb 2024 10:18:26 +1300 Subject: [PATCH 1/3] Add an unit test to reproduce an XMLPRC upload issue See https://github.com/wordpress-mobile/WordPress-iOS/issues/22659 --- .../Utilities/HTTPRequestBuilderTests.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/WordPressKitTests/Utilities/HTTPRequestBuilderTests.swift b/WordPressKitTests/Utilities/HTTPRequestBuilderTests.swift index 7b008c75..e21cd2cd 100644 --- a/WordPressKitTests/Utilities/HTTPRequestBuilderTests.swift +++ b/WordPressKitTests/Utilities/HTTPRequestBuilderTests.swift @@ -403,4 +403,16 @@ class HTTPRequestBuilderTests: XCTestCase { ) } + func testXMLRPCUpload() throws { + let file = try XCTUnwrap(Bundle(for: type(of: self)).url(forResource: "me-settings-success", withExtension: "json")) + let fileContentBase64 = try Data(contentsOf: file).base64EncodedString() + let fileStream = try XCTUnwrap(InputStream(url: file)) + + let request = try HTTPRequestBuilder(url: URL(string: "https://w.org/xmlrpc.php")!) + .method(.post) + .body(xmlrpc: "wp.uploadFile", parameters: ["username", "passowrd", fileStream]) + .build(encodeBody: true) + XCTAssertTrue(request.httpBodyText?.contains(fileContentBase64) == true) + } + } From 6361cf200746ba8d85ae761fa6e45f0780204b21 Mon Sep 17 00:00:00 2001 From: Tony Li Date: Thu, 22 Feb 2024 10:18:51 +1300 Subject: [PATCH 2/3] Fix an XMLRPC uploading file issue See https://github.com/wordpress-mobile/WordPress-iOS/issues/22659 --- WordPressKit/HTTPRequestBuilder.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPressKit/HTTPRequestBuilder.swift b/WordPressKit/HTTPRequestBuilder.swift index 885c8844..e132839b 100644 --- a/WordPressKit/HTTPRequestBuilder.swift +++ b/WordPressKit/HTTPRequestBuilder.swift @@ -228,7 +228,7 @@ final class HTTPRequestBuilder { } else { let data = try encoder.dataEncoded() request.setValue("\(data.count)", forHTTPHeaderField: "Content-Length") - return try .left(encoder.dataEncoded()) + return .left(data) } } } From 02a12301d89223be383b295e5e3c0eabe685e29e Mon Sep 17 00:00:00 2001 From: Tony Li Date: Thu, 22 Feb 2024 10:22:59 +1300 Subject: [PATCH 3/3] Use a more strict check --- WordPressKitTests/Utilities/HTTPRequestBuilderTests.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/WordPressKitTests/Utilities/HTTPRequestBuilderTests.swift b/WordPressKitTests/Utilities/HTTPRequestBuilderTests.swift index e21cd2cd..1bfa3f11 100644 --- a/WordPressKitTests/Utilities/HTTPRequestBuilderTests.swift +++ b/WordPressKitTests/Utilities/HTTPRequestBuilderTests.swift @@ -407,12 +407,14 @@ class HTTPRequestBuilderTests: XCTestCase { let file = try XCTUnwrap(Bundle(for: type(of: self)).url(forResource: "me-settings-success", withExtension: "json")) let fileContentBase64 = try Data(contentsOf: file).base64EncodedString() let fileStream = try XCTUnwrap(InputStream(url: file)) - let request = try HTTPRequestBuilder(url: URL(string: "https://w.org/xmlrpc.php")!) .method(.post) .body(xmlrpc: "wp.uploadFile", parameters: ["username", "passowrd", fileStream]) .build(encodeBody: true) - XCTAssertTrue(request.httpBodyText?.contains(fileContentBase64) == true) + + let xmlrpcContent = try XCTUnwrap(request.httpBodyText) + let filePart = "" + fileContentBase64 + "" + XCTAssertTrue(xmlrpcContent.contains(filePart)) } }