|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | | -import XCTest |
16 | | - |
17 | 15 | @testable import App |
18 | 16 |
|
19 | 17 | import Dependencies |
| 18 | +import Testing |
20 | 19 | import Vapor |
21 | 20 |
|
22 | 21 |
|
23 | | -class ErrorMiddlewareTests: AppTestCase { |
24 | | - |
25 | | - override func setUpWithError() throws { |
26 | | - try super.setUpWithError() |
| 22 | +@Suite struct ErrorMiddlewareTests { |
27 | 23 |
|
| 24 | + func setup(_ app: Application) async throws { |
28 | 25 | // set up some test routes |
29 | 26 | app.get("ok") { _ in return "ok" } |
30 | 27 | app.get("404") { req async throws -> Response in throw Abort(.notFound) } |
31 | 28 | app.get("500") { req async throws -> Response in throw Abort(.internalServerError) } |
32 | 29 | } |
33 | 30 |
|
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 | + } |
40 | 39 | } |
41 | 40 |
|
42 | | - func test_html_error() throws { |
| 41 | + @Test func html_error() async throws { |
43 | 42 | // Test to ensure errors are converted to html error pages via the ErrorMiddleware |
44 | | - try withDependencies { |
| 43 | + try await withDependencies { |
45 | 44 | $0.environment.dbId = { nil } |
46 | 45 | } 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 | + } |
51 | 52 | } |
52 | 53 | } |
53 | 54 |
|
54 | | - func test_status_code() throws { |
| 55 | + @Test func status_code() async throws { |
55 | 56 | // Ensure we're still reporting the actual status code even when serving html pages |
56 | 57 | // (Status is important for Google ranking, see |
57 | 58 | // https://github.com/SwiftPackageIndex/SwiftPackageIndex-Server/issues/323) |
58 | | - try withDependencies { |
| 59 | + try await withDependencies { |
59 | 60 | $0.environment.dbId = { nil } |
60 | 61 | } 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 | + } |
69 | 72 | } |
70 | 73 | } |
71 | 74 |
|
|
0 commit comments