1+ //===----------------------------------------------------------------------===//
2+ //
3+ // This source file is part of the Swift OpenAPI Lambda open source project
4+ //
5+ // Copyright Swift OpenAPI Lambda project authors
6+ // Copyright (c) 2023 Amazon.com, Inc. or its affiliates.
7+ // Licensed under Apache License v2.0
8+ //
9+ // See LICENSE.txt for license information
10+ // See CONTRIBUTORS.txt for the list of Swift OpenAPI Lambda project authors
11+ //
12+ // SPDX-License-Identifier: Apache-2.0
13+ //
14+ //===----------------------------------------------------------------------===//
15+ import AWSLambdaEvents
16+ import Foundation
17+ import HTTPTypes
18+ import Testing
19+
20+ @testable import OpenAPILambda
21+
22+ struct APIGatewayV2ConversionTests {
23+
24+ static let apiGatewayEventJSON = """
25+ {
26+ " rawQueryString " : " " ,
27+ " headers " : {
28+ " host " : " b2k1t8fon7.execute-api.us-east-1.amazonaws.com " ,
29+ " accept " : " */* " ,
30+ " user-agent " : " curl/8.1.2 " ,
31+ " authorization " : " Bearer 123 "
32+ },
33+ " requestContext " : {
34+ " apiId " : " b2k1t8fon7 " ,
35+ " http " : {
36+ " sourceIp " : " 191.95.148.219 " ,
37+ " userAgent " : " curl/8.1.2 " ,
38+ " method " : " GET " ,
39+ " path " : " /stocks/AAPL " ,
40+ " protocol " : " HTTP/1.1 "
41+ },
42+ " timeEpoch " : 1701957940365,
43+ " domainPrefix " : " b2k1t8fon7 " ,
44+ " accountId " : " 486652066693 " ,
45+ " time " : " 07/Dec/2023:14:05:40 +0000 " ,
46+ " stage " : " $default " ,
47+ " domainName " : " b2k1t8fon7.execute-api.us-east-1.amazonaws.com " ,
48+ " requestId " : " Pk2gOia2IAMEPOw= "
49+ },
50+ " isBase64Encoded " : false,
51+ " version " : " 2.0 " ,
52+ " routeKey " : " $default " ,
53+ " rawPath " : " /stocks/AAPL "
54+ }
55+ """
56+
57+ static let apiGatewayEventWithQueryJSON = """
58+ {
59+ " rawQueryString " : " limit=10&offset=0 " ,
60+ " headers " : {
61+ " host " : " b2k1t8fon7.execute-api.us-east-1.amazonaws.com "
62+ },
63+ " requestContext " : {
64+ " apiId " : " b2k1t8fon7 " ,
65+ " http " : {
66+ " sourceIp " : " 191.95.148.219 " ,
67+ " userAgent " : " curl/8.1.2 " ,
68+ " method " : " GET " ,
69+ " path " : " /stocks/AAPL " ,
70+ " protocol " : " HTTP/1.1 "
71+ },
72+ " timeEpoch " : 1701957940365,
73+ " domainPrefix " : " b2k1t8fon7 " ,
74+ " accountId " : " 486652066693 " ,
75+ " time " : " 07/Dec/2023:14:05:40 +0000 " ,
76+ " stage " : " $default " ,
77+ " domainName " : " b2k1t8fon7.execute-api.us-east-1.amazonaws.com " ,
78+ " requestId " : " Pk2gOia2IAMEPOw= "
79+ },
80+ " isBase64Encoded " : false,
81+ " version " : " 2.0 " ,
82+ " routeKey " : " $default " ,
83+ " rawPath " : " /stocks/AAPL "
84+ }
85+ """
86+
87+ @Test ( " API Gateway v2 request to HTTPRequest conversion " )
88+ func testAPIGatewayV2RequestToHTTPRequest( ) throws {
89+ let data = APIGatewayV2ConversionTests . apiGatewayEventJSON. data ( using: . utf8) !
90+ let apiGatewayRequest = try JSONDecoder ( ) . decode ( APIGatewayV2Request . self, from: data)
91+
92+ let httpRequest = try apiGatewayRequest. httpRequest ( )
93+
94+ #expect( httpRequest. method == HTTPRequest . Method. get)
95+ #expect( httpRequest. path == " /stocks/AAPL " )
96+ #expect( httpRequest. scheme == " https " )
97+ #expect( httpRequest. headerFields [ HTTPField . Name ( " host " ) !] == " b2k1t8fon7.execute-api.us-east-1.amazonaws.com " )
98+ #expect( httpRequest. headerFields [ HTTPField . Name. accept] == " */* " )
99+ #expect( httpRequest. headerFields [ HTTPField . Name. userAgent] == " curl/8.1.2 " )
100+ #expect( httpRequest. headerFields [ HTTPField . Name. authorization] == " Bearer 123 " )
101+ }
102+
103+ @Test ( " API Gateway v2 request with query string " )
104+ func testAPIGatewayV2RequestWithQueryString( ) throws {
105+ let data = APIGatewayV2ConversionTests . apiGatewayEventWithQueryJSON. data ( using: . utf8) !
106+ let apiGatewayRequest = try JSONDecoder ( ) . decode ( APIGatewayV2Request . self, from: data)
107+
108+ let httpRequest = try apiGatewayRequest. httpRequest ( )
109+
110+ #expect( httpRequest. path == " /stocks/AAPL?limit=10&offset=0 " )
111+ }
112+
113+ @Test ( " HTTPResponse to API Gateway v2 response conversion " )
114+ func testHTTPResponseToAPIGatewayV2Response( ) throws {
115+ var httpResponse = HTTPResponse ( status: . ok)
116+ httpResponse. headerFields [ HTTPField . Name. contentType] = " application/json "
117+ httpResponse. headerFields [ HTTPField . Name. contentLength] = " 42 "
118+
119+ let apiGatewayResponse = APIGatewayV2Response ( from: httpResponse)
120+
121+ #expect( apiGatewayResponse. statusCode == . ok)
122+ #expect( apiGatewayResponse. headers ? [ HTTPField . Name. contentType. rawName] == " application/json " )
123+ #expect( apiGatewayResponse. headers ? [ HTTPField . Name. contentLength. rawName] == " 42 " )
124+ #expect( apiGatewayResponse. isBase64Encoded == false )
125+ #expect( apiGatewayResponse. cookies == nil )
126+ }
127+ }
0 commit comments