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

Allow XCTAssertContent to rethrow errors #2403

Merged
merged 2 commits into from Jul 15, 2020
Merged
Changes from all 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
12 changes: 9 additions & 3 deletions Sources/XCTVapor/XCTHTTPResponse.swift
Expand Up @@ -44,21 +44,27 @@ public func XCTAssertContent<D>(
_ res: XCTHTTPResponse,
file: StaticString = #file,
line: UInt = #line,
_ closure: (D) -> ()
_ closure: (D) throws -> ()
)
rethrows
where D: Decodable
{
guard let contentType = res.headers.contentType else {
XCTFail("response does not contain content type", file: file, line: line)
return
}

let content: D

do {
let decoder = try ContentConfiguration.global.requireDecoder(for: contentType)
let content = try decoder.decode(D.self, from: res.body, headers: res.headers)
closure(content)
content = try decoder.decode(D.self, from: res.body, headers: res.headers)
} catch {
XCTFail("could not decode body: \(error)", file: (file), line: line)
return
}

try closure(content)
}

public func XCTAssertContains(_ haystack: String?, _ needle: String?, file: StaticString = #file, line: UInt = #line) {
Expand Down