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 crash on unexpected plugin slugs #767

Merged
merged 4 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ _None._

### Bug Fixes

_None._
- Fix crash when querying a WordPress plugin whose slug is not url-safe. [#767]

### Internal Changes

Expand Down
2 changes: 1 addition & 1 deletion WordPressKit/PluginDirectoryServiceRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public struct PluginDirectoryGetInformationEndpoint {

func buildRequest() throws -> URLRequest {
try HTTPRequestBuilder(url: PluginDirectoryRemoteConstants.getInformationEndpoint)
.append(percentEncodedPath: "\(slug).json")
.appendURLString("\(slug).json")
.query(name: "fields", value: "icons,banners")
.build()
}
Expand Down
12 changes: 12 additions & 0 deletions WordPressKitTests/PluginDirectoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,16 @@ class PluginDirectoryTests: XCTestCase {

XCTAssertTrue(lhs == rhs)
}

func testUnconventionalPluginSlug() async throws {
let data = try MockPluginDirectoryProvider.getPluginDirectoryMockData(with: "plugin-directory-rename-xml-rpc", sender: type(of: self))
stub(condition: isHost("api.wordpress.org")) { _ in
HTTPStubsResponse(data: data, statusCode: 200, headers: ["Content-Type": "application/json"])
}

let _ = try await PluginDirectoryServiceRemote().getPluginInformation(slug: "%-is-not-allowed")
let _ = try await PluginDirectoryServiceRemote().getPluginInformation(slug: "中文")

// No assertion needed.
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A suggestion for your consideration.

Suggested change
let _ = try await PluginDirectoryServiceRemote().getPluginInformation(slug: "%-is-not-allowed")
let _ = try await PluginDirectoryServiceRemote().getPluginInformation(slug: "中文")
// No assertion needed.
}
await XCTAssertNoThrow(try await PluginDirectoryServiceRemote().getPluginInformation(slug: "%-is-not-allowed"))
await XCTAssertNoThrow(try await PluginDirectoryServiceRemote().getPluginInformation(slug: "中文"))
}
}
func XCTAssertNoThrow<T>(
_ expression: @autoclosure () async throws -> T,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath,
line: UInt = #line
) async {
let error: Error?
do {
_ = try await expression()
error = nil
} catch let caughtError {
error = caughtError
}
XCTAssertNil(
error,
message().isEmpty ? "Expected to not throw, threw \(String(describing: error))" : message(),
file: file,
line: line
)
}

The motivation for this is that a test with no assertion looks odd to me. But, having said that, XCTest will fail a test test name() throws that threw an error, so it might be much of a muchness...

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XCTest will fail a test test name() throws that threw an error, so it might be much of a muchness...

That's my thinking too. XCTest kind of makes the "no throw" assertion for us.

}