|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Foundation |
| 16 | +import Testing |
| 17 | + |
| 18 | +@testable import AWSLambdaEvents |
| 19 | + |
| 20 | +struct APIGatewayEncodableResponseTests { |
| 21 | + |
| 22 | + // MARK: Encoding |
| 23 | + struct BusinessResponse: Codable, Equatable { |
| 24 | + let message: String |
| 25 | + let code: Int |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + func testResponseEncodingV2() throws { |
| 30 | + |
| 31 | + // given |
| 32 | + let businessResponse = BusinessResponse(message: "Hello World", code: 200) |
| 33 | + |
| 34 | + var response: APIGatewayV2Response? = nil |
| 35 | + #expect(throws: Never.self) { |
| 36 | + try response = APIGatewayV2Response(statusCode: .ok, encodableBody: businessResponse) |
| 37 | + } |
| 38 | + try #require(response?.body != nil) |
| 39 | + |
| 40 | + // when |
| 41 | + let body = response?.body?.data(using: .utf8) |
| 42 | + try #require(body != nil) |
| 43 | + |
| 44 | + #expect(throws: Never.self) { |
| 45 | + let encodedBody = try JSONDecoder().decode(BusinessResponse.self, from: body!) |
| 46 | + |
| 47 | + // then |
| 48 | + #expect(encodedBody == businessResponse) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + func testResponseEncoding() throws { |
| 54 | + |
| 55 | + // given |
| 56 | + let businessResponse = BusinessResponse(message: "Hello World", code: 200) |
| 57 | + |
| 58 | + var response: APIGatewayResponse? = nil |
| 59 | + #expect(throws: Never.self) { |
| 60 | + try response = APIGatewayResponse(statusCode: .ok, encodableBody: businessResponse) |
| 61 | + } |
| 62 | + try #require(response?.body != nil) |
| 63 | + |
| 64 | + // when |
| 65 | + let body = response?.body?.data(using: .utf8) |
| 66 | + try #require(body != nil) |
| 67 | + |
| 68 | + #expect(throws: Never.self) { |
| 69 | + let encodedBody = try JSONDecoder().decode(BusinessResponse.self, from: body!) |
| 70 | + |
| 71 | + // then |
| 72 | + #expect(encodedBody == businessResponse) |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments