Skip to content
Merged
Show file tree
Hide file tree
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
50 changes: 50 additions & 0 deletions native/swift/Tests/wordpress-api/Endpoints/Users.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import Foundation
import XCTest

@testable import WordPressAPI

final class UsersTest: XCTestCase {

func testRetrieveUser() async throws {
let response = """
{
"id": 1,
"name": "User Name",
"url": "",
"description": "",
"link": "https://profiles.wordpress.org/user/",
"slug": "poliuk",
"avatar_urls": {
"24": "https://secure.gravatar.com/avatar/uuid?s=24&d=mm&r=g",
"48": "https://secure.gravatar.com/avatar/uuid?s=48&d=mm&r=g",
"96": "https://secure.gravatar.com/avatar/uuid?s=96&d=mm&r=g"
},
"meta": [],
"_links": {
"self": [
{
"href": "https://wordpress.org/wp-json/wp/v2/users/1"
}
],
"collection": [
{
"href": "https://wordpress.org/wp-json/wp/v2/users"
}
]
}
}
"""
let stubs = HTTPStubs()
stubs.stub(path: "/wp-json/wp/v2/users/1", with: .json(response))

let api = try WordPressAPI(
urlSession: .shared,
baseUrl: URL(string: "https://wordpress.org")!,
authenticationStategy: .none,
executor: stubs
)
let user = try await api.users.retrieveWithViewContext(userId: 1)
XCTAssertEqual(user.name, "User Name")
}

}
33 changes: 33 additions & 0 deletions native/swift/Tests/wordpress-api/HTTPErrorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Foundation
import XCTest

@testable import WordPressAPI
import WordPressAPIInternal

class HTTPErrorTests: XCTestCase {

#if !os(Linux)
// Skip on Linux, because `XCTExpectFailure` is unavailable on Linux
func testTimeout() async throws {
let stubs = HTTPStubs()
stubs.missingStub = .failure(URLError(.timedOut))

let api = try WordPressAPI(
urlSession: .shared,
baseUrl: URL(string: "https://wordpress.org")!,
authenticationStategy: .none,
executor: stubs
)

do {
_ = try await api.users.retrieveWithViewContext(userId: 1)
XCTFail("Unexpected response")
} catch let error as URLError {
XCTAssertEqual(error.code, .timedOut)
} catch {
XCTAssertTrue(error is WordPressAPIInternal.WpApiError)
}
}
#endif

}
55 changes: 55 additions & 0 deletions native/swift/Tests/wordpress-api/Support/HTTPStubs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Foundation
import WordPressAPI

#if canImport(WordPressAPIInternal)
import WordPressAPIInternal
#endif

class HTTPStubs: SafeRequestExecutor {

var stubs: [(condition: (WpNetworkRequest) -> Bool, response: WpNetworkResponse)] = []

var missingStub: Result<WpNetworkResponse, Error>?

public func execute(_ request: WpNetworkRequest) async -> Result<WpNetworkResponse, RequestExecutionError> {
if let response = stub(for: request) {
return .success(response)
}

switch missingStub {
case let .success(response):
return .success(response)
case .failure:
// TODO: Translate error into the Rust type
return .failure(.RequestExecutionFailed(statusCode: nil, reason: ""))
default:
// TODO: Translate error into the Rust type
return .failure(.RequestExecutionFailed(statusCode: nil, reason: ""))
}
}

func stub(for request: WpNetworkRequest) -> WpNetworkResponse? {
stubs.first { stub in stub.condition(request) }?
.response
}

func stub(path: String, with response: WpNetworkResponse) {
stubs.append((
condition: { URL(string: $0.url)?.path == path },
response: response
))
}

}

extension WpNetworkResponse {

static func json(_ content: String) -> WpNetworkResponse {
WpNetworkResponse(
body: content.data(using: .utf8)!,
statusCode: 200,
headerMap: ["Content-Type": "application/json"]
)
}

}
125 changes: 0 additions & 125 deletions native/swift/Tests/wordpress-api/WordPressAPITests.swift

This file was deleted.