Skip to content

Commit bb866e6

Browse files
committed
Convert ErrorMiddlewareTests
1 parent a693b59 commit bb866e6

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

Tests/AppTests/ErrorMiddlewareTests.swift

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,60 +12,63 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import XCTest
16-
1715
@testable import App
1816

1917
import Dependencies
18+
import Testing
2019
import Vapor
2120

2221

23-
class ErrorMiddlewareTests: AppTestCase {
24-
25-
override func setUpWithError() throws {
26-
try super.setUpWithError()
22+
@Suite struct ErrorMiddlewareTests {
2723

24+
func setup(_ app: Application) async throws {
2825
// set up some test routes
2926
app.get("ok") { _ in return "ok" }
3027
app.get("404") { req async throws -> Response in throw Abort(.notFound) }
3128
app.get("500") { req async throws -> Response in throw Abort(.internalServerError) }
3229
}
3330

34-
func test_custom_routes() throws {
35-
// Test to ensure the test routes we've set up in setUpWithError are in effect
36-
try app.test(.GET, "ok", afterResponse: { response in
37-
XCTAssertEqual(response.status, .ok)
38-
XCTAssertEqual(response.body.asString(), "ok")
39-
})
31+
@Test func custom_routes() async throws {
32+
try await withApp(setup) { app in
33+
// Test to ensure the test routes we've set up in setUpWithError are in effect
34+
try await app.test(.GET, "ok", afterResponse: { response async in
35+
#expect(response.status == .ok)
36+
#expect(response.body.asString() == "ok")
37+
})
38+
}
4039
}
4140

42-
func test_html_error() throws {
41+
@Test func html_error() async throws {
4342
// Test to ensure errors are converted to html error pages via the ErrorMiddleware
44-
try withDependencies {
43+
try await withDependencies {
4544
$0.environment.dbId = { nil }
4645
} operation: {
47-
try app.test(.GET, "404", afterResponse: { response in
48-
XCTAssertEqual(response.content.contentType, .html)
49-
XCTAssert(response.body.asString().contains("404 - Not Found"))
50-
})
46+
try await withApp(setup) { app in
47+
try await app.test(.GET, "404", afterResponse: { response async in
48+
#expect(response.content.contentType == .html)
49+
#expect(response.body.asString().contains("404 - Not Found"))
50+
})
51+
}
5152
}
5253
}
5354

54-
func test_status_code() throws {
55+
@Test func status_code() async throws {
5556
// Ensure we're still reporting the actual status code even when serving html pages
5657
// (Status is important for Google ranking, see
5758
// https://github.com/SwiftPackageIndex/SwiftPackageIndex-Server/issues/323)
58-
try withDependencies {
59+
try await withDependencies {
5960
$0.environment.dbId = { nil }
6061
} operation: {
61-
try app.test(.GET, "404", afterResponse: { response in
62-
XCTAssertEqual(response.status, .notFound)
63-
XCTAssertEqual(response.content.contentType, .html)
64-
})
65-
try app.test(.GET, "500", afterResponse: { response in
66-
XCTAssertEqual(response.status, .internalServerError)
67-
XCTAssertEqual(response.content.contentType, .html)
68-
})
62+
try await withApp(setup) { app in
63+
try await app.test(.GET, "404", afterResponse: { response async in
64+
#expect(response.status == .notFound)
65+
#expect(response.content.contentType == .html)
66+
})
67+
try await app.test(.GET, "500", afterResponse: { response async in
68+
#expect(response.status == .internalServerError)
69+
#expect(response.content.contentType == .html)
70+
})
71+
}
6972
}
7073
}
7174

0 commit comments

Comments
 (0)