|
15 | 15 | // You should have received a copy of the GNU Affero General Public License |
16 | 16 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
17 | 17 |
|
18 | | -import XCTVapor |
19 | 18 | @testable import ErrorMiddleware |
| 19 | +import VaporTesting |
| 20 | +import Testing |
20 | 21 |
|
21 | | -final class ErrorMiddlewareTests: XCTestCase { |
22 | | - // swiftlint: disable implicitly_unwrapped_optional |
23 | | - var app: Application! |
24 | | - // swiftlint: enable implicitly_unwrapped_optional |
25 | | - override func setUp() async throws { |
26 | | - app = try await Application.make(.testing) |
27 | | - app.post("order") { _ -> String in |
28 | | - return "done" |
| 22 | +@Suite("Error middleware tests") |
| 23 | +struct ErrorMiddlewareTests { |
| 24 | + private func withApp(_ test: (Application) async throws -> ()) async throws { |
| 25 | + let app = try await Application.make(.testing) |
| 26 | + do { |
| 27 | + app.post("order") { _ -> String in |
| 28 | + return "done" |
| 29 | + } |
| 30 | + try await test(app) |
| 31 | + } catch { |
| 32 | + throw error |
29 | 33 | } |
30 | | - } |
31 | | - |
32 | | - override func tearDown() async throws { |
33 | 34 | try await app.asyncShutdown() |
34 | 35 | } |
35 | 36 |
|
36 | | - func testErrorMiddlewareSnakeCase() throws { |
37 | | - let app = Application(.testing) |
38 | | - defer { app.shutdown() } |
39 | | - app.middleware.use(ErrorMiddleware.custom(environment: app.environment, for: 1)) |
| 37 | + @Test("Error middleware snake case") |
| 38 | + func errorMiddlewareSnakeCase() async throws { |
| 39 | + try await withApp { app in |
| 40 | + app.middleware.use(ErrorMiddleware.custom(environment: app.environment, for: 1)) |
40 | 41 |
|
41 | | - try app.testable().test(.GET, "order") { res throws in |
42 | | - let error = try res.content.decode(ErrorResponse.self, using: app.errorDecode) |
43 | | - XCTAssertEqual(error.isError, true) |
44 | | - XCTAssertEqual(error.reason, "Not Found") |
45 | | - XCTAssertEqual(error.error, "404") |
46 | | - XCTAssertEqual(error.status, "404") |
47 | | - XCTAssertEqual(error.code, "404.1.") |
48 | | - XCTAssertEqual(error.errorUri, "https://example.com/doc/errors") |
| 42 | + try await app.test(.GET, "order") { res throws in |
| 43 | + let error = try res.content.decode(ErrorResponse.self, using: app.errorDecode) |
| 44 | + #expect(error.isError == true) |
| 45 | + #expect(error.reason == "Not Found") |
| 46 | + #expect(error.error == "404") |
| 47 | + #expect(error.status == "404") |
| 48 | + #expect(error.code == "404.1.") |
| 49 | + #expect(error.errorUri == "https://example.com/doc/errors") |
| 50 | + } |
49 | 51 | } |
50 | 52 | } |
51 | 53 |
|
52 | | - func testErrorMiddlewareDefaultCase() throws { |
53 | | - let app = Application(.testing) |
54 | | - defer { app.shutdown() } |
55 | | - app.middleware.use(ErrorMiddleware.custom(environment: app.environment, for: 1, keyEncodingStrategy: .useDefaultKeys)) |
| 54 | + @Test("Error middleware default case") |
| 55 | + func errorMiddlewareDefaultCase() async throws { |
| 56 | + try await withApp { app in |
| 57 | + app.middleware.use(ErrorMiddleware.custom(environment: app.environment, for: 1, keyEncodingStrategy: .useDefaultKeys)) |
56 | 58 |
|
57 | | - try app.testable().test(.GET, "order") { res throws in |
58 | | - let error = try res.content.decode(ErrorResponse.self) |
59 | | - XCTAssertEqual(error.isError, true) |
60 | | - XCTAssertEqual(error.reason, "Not Found") |
61 | | - XCTAssertEqual(error.error, "404") |
62 | | - XCTAssertEqual(error.status, "404") |
63 | | - XCTAssertEqual(error.code, "404.1.") |
64 | | - XCTAssertEqual(error.errorUri, "https://example.com/doc/errors") |
| 59 | + try await app.test(.GET, "order") { res throws in |
| 60 | + let error = try res.content.decode(ErrorResponse.self) |
| 61 | + #expect(error.isError == true) |
| 62 | + #expect(error.reason == "Not Found") |
| 63 | + #expect(error.error == "404") |
| 64 | + #expect(error.status == "404") |
| 65 | + #expect(error.code == "404.1.") |
| 66 | + #expect(error.errorUri == "https://example.com/doc/errors") |
| 67 | + } |
65 | 68 | } |
66 | 69 | } |
67 | 70 | } |
0 commit comments