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 2 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
53 changes: 53 additions & 0 deletions Sources/XCTVapor/TestDoubles/Spies/SpyClient.swift
@@ -0,0 +1,53 @@
public final class SpyClient: Client {
Kithin marked this conversation as resolved.
Show resolved Hide resolved
private(set) var requestUrlComponentsUsed: URLComponents = .init()
Kithin marked this conversation as resolved.
Show resolved Hide resolved
private(set) var requestHttpMethodUsed: HTTPMethod?
private(set) var requestHeadersUsed: HTTPHeaders?
private(set) var requestBodyUsed: Data?

private var stubResponse: ClientResponse!
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.requestUrlComponentsUsed.scheme = request.url.scheme
Kithin marked this conversation as resolved.
Show resolved Hide resolved
self.requestUrlComponentsUsed.host = request.url.host
self.requestUrlComponentsUsed.path = request.url.path
self.requestUrlComponentsUsed.query = request.url.query

self.requestHttpMethodUsed = request.method
self.requestHeadersUsed = request.headers

if var body = request.body {
self.requestBodyUsed = body.readData(length: body.readableBytes)
}

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)
}
}
}

32 changes: 32 additions & 0 deletions Sources/XCTVapor/XCTVaporTestCase.swift
@@ -0,0 +1,32 @@
import XCTest
Kithin marked this conversation as resolved.
Show resolved Hide resolved
import Development

open class XCTVaporTestCase: XCTestCase {
open var app: Application!
open var client: SpyClient!

open override func setUpWithError() throws {
try super.setUpWithError()

app = Application(.testing)

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

app.clients.use { [unowned self] _ in
return self.client
}

try configure(app)
}

open override func tearDownWithError() throws {
client = nil

app.shutdown()
app = nil

try super.tearDownWithError()
}
}