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

Add mocked client + subclass of XCTestCase #2928

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions Sources/XCTVapor/TestDoubles/Spies/SpyClient.swift
@@ -0,0 +1,42 @@
import Foundation
Kithin marked this conversation as resolved.
Show resolved Hide resolved

public final class SpyClient: Client {
Kithin marked this conversation as resolved.
Show resolved Hide resolved
private(set) var requestsUsed: [ClientRequest] = []
Kithin marked this conversation as resolved.
Show resolved Hide resolved

private var stubResponse: ClientResponse = .init(status: .ok)
Kithin marked this conversation as resolved.
Show resolved Hide resolved

public var eventLoop: EventLoop

public init(
eventLoop: EventLoop
) {
self.eventLoop = eventLoop
}

// Conforming to the protocol: Client
public func delegating(to eventLoop: EventLoop) -> Client {
self.eventLoop = eventLoop

return self
}

// Conforming to the protocol: Client
public func send(_ request: ClientRequest) -> EventLoopFuture<ClientResponse> {
self.requestsUsed.append(request)
Kithin marked this conversation as resolved.
Show resolved Hide resolved

return self.eventLoop.future(self.stubResponse)
}

/// To be able to stub the response of a request made with the client.
/// - Parameters:
/// - httpStatus: Provide HTTP status, you want the response to have
/// - responseData: Provide response data, you want the response to have
public func stubResponse(httpStatus: HTTPStatus, responseData: (any Content)? = nil) throws {
Kithin marked this conversation as resolved.
Show resolved Hide resolved
self.stubResponse = .init(status: httpStatus)

if let responseData = responseData {
try self.stubResponse.content.encode(responseData, as: .json)
}
}
}

25 changes: 20 additions & 5 deletions Tests/VaporTests/ApplicationTests.swift
Expand Up @@ -104,16 +104,31 @@ final class ApplicationTests: XCTestCase {
func testBoilerplate() throws {
let app = Application(.testing)
defer { app.shutdown() }

app.get("hello") { req in
"Hello, world!"

let client = SpyClient(
eventLoop: app.eventLoopGroup.next()
)

app.clients.use { _ in
return client
}
struct Greeting: Content {
let message: String
}

let greeting: Greeting = .init(message: "Hello, world!")
try client.stubResponse(httpStatus: .ok, responseData: greeting)

app.environment.arguments = ["serve"]
try app.start()

app.get("hello") { req in
"Hello, world!"
}

let res = try app.client.get("http://localhost:8080/hello").wait()
XCTAssertEqual(res.body?.string, "Hello, world!")
let response = try app.client.get("http://localhost:8080/hello").wait()
let message = try response.content.decode(Greeting.self).message
XCTAssertEqual(message, "Hello, world!")
}

func testAutomaticPortPickingWorks() {
Expand Down