Skip to content

Commit

Permalink
Fix an XMLRPC uploading file issue (#734)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazytonyli authored Feb 21, 2024
2 parents 4df2b39 + 02a1230 commit f335202
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion WordPressKit/HTTPRequestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,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)
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions WordPressKitTests/Utilities/HTTPRequestBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,18 @@ 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)

let xmlrpcContent = try XCTUnwrap(request.httpBodyText)
let filePart = "<base64>" + fileContentBase64 + "</base64>"
XCTAssertTrue(xmlrpcContent.contains(filePart))
}

}

0 comments on commit f335202

Please sign in to comment.