Skip to content

Commit 1fb3803

Browse files
Add test package (#687)
* chore: update default client config properties' defaults, update protocol response tests rendering, fix generated client config type in client * feat: add smithy-swift.smithy-swift-codegen-test-utils package * feat: add smithy-swift.smithy-swift-codegen-test package * chore: fix swiftlint * chore: address PR comments
1 parent c53bf27 commit 1fb3803

File tree

186 files changed

+4439
-35
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

186 files changed

+4439
-35
lines changed

.swiftlint.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
excluded:
22
- .build
33
- Sources/SmithyTestUtil/*
4+
- Sources/WeatherSDK/*
45
- Tests/ClientRuntimeTests/*
56
- Tests/SmithyTestUtilTests/*
67
- Tests/SmithyXMLTests/*
78
- Tests/SmithyTimestampsTests/*
9+
- Tests/WeatherSDKTests/*
10+
- smithy-swift-codegen-test/build/*
811

912
analyzer_rules:
1013
- unused_import

Package.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,18 @@ let package = Package(
8181
),
8282
].compactMap { $0 }
8383
)
84+
85+
func addTestServiceTargets() {
86+
package.targets += [
87+
.target(
88+
name: "WeatherSDK",
89+
dependencies: ["SmithyTestUtil", "ClientRuntime"]
90+
),
91+
.testTarget(
92+
name: "WeatherSDKTests",
93+
dependencies: ["WeatherSDK"]
94+
)
95+
]
96+
}
97+
98+
addTestServiceTargets()

Sources/ClientRuntime/Config/DefaultSDKRuntimeConfiguration.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ public extension DefaultSDKRuntimeConfiguration {
8989
///
9090
/// - Parameter httpClientConfiguration: The configuration for the HTTP client.
9191
/// - Returns: The `CRTClientEngine` client on Mac & Linux platforms, returns `URLSessionHttpClient` on non-Mac Apple platforms.
92-
static func makeClient(httpClientConfiguration: HttpClientConfiguration) -> HTTPClient {
92+
static func makeClient(
93+
httpClientConfiguration: HttpClientConfiguration = defaultHttpClientConfiguration
94+
) -> HTTPClient {
9395
#if os(iOS) || os(tvOS) || os(watchOS) || os(visionOS) || os(macOS)
9496
return URLSessionHTTPClient(httpClientConfiguration: httpClientConfiguration)
9597
#else
@@ -119,4 +121,27 @@ public extension DefaultSDKRuntimeConfiguration {
119121
///
120122
/// Defaults to `.request`.
121123
static var defaultClientLogMode: ClientLogMode { .request }
124+
125+
static var defaultAuthSchemeResolver: AuthSchemeResolver { DefaultAuthSchemeResolver() }
126+
}
127+
128+
public class DefaultAuthSchemeResolverParameters: AuthSchemeResolverParameters {
129+
public var operation: String
130+
public init(operation: String) {
131+
self.operation = operation
132+
}
133+
}
134+
135+
public class DefaultAuthSchemeResolver: AuthSchemeResolver {
136+
public func resolveAuthScheme(params: AuthSchemeResolverParameters) throws -> [AuthOption] {
137+
return []
138+
}
139+
140+
public func constructParameters(context: HttpContext) throws -> AuthSchemeResolverParameters {
141+
guard let opName = context.getOperation() else {
142+
throw ClientError.dataNotFound(
143+
"Operation name not configured in middleware context for auth scheme resolver params construction.")
144+
}
145+
return DefaultAuthSchemeResolverParameters(operation: opName)
146+
}
122147
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
6+
import Foundation
7+
import ClientRuntime
8+
9+
/// A general error structure for protocols with JSON response
10+
public struct JSONError {
11+
public let errorMessage: String?
12+
public let errorType: String?
13+
14+
public init(httpResponse: HttpResponse) async throws {
15+
var message: String?
16+
var errorType: String?
17+
if let data = try await httpResponse.body.readData() {
18+
let output: JSONErrorPayload = try JSONDecoder().decode(responseBody: data)
19+
message = output.message
20+
errorType = output.errorType
21+
}
22+
self.errorMessage = message
23+
self.errorType = errorType
24+
}
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
6+
public struct JSONErrorPayload: Decodable {
7+
let message: String?
8+
let errorType: String?
9+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Code generated by smithy-swift-codegen. DO NOT EDIT!
2+
3+
import ClientRuntime
4+
5+
public struct WeatherAuthSchemeResolverParameters: ClientRuntime.AuthSchemeResolverParameters {
6+
public let operation: String
7+
// Region is used for SigV4 auth scheme
8+
public let region: String?
9+
}
10+
11+
public protocol WeatherAuthSchemeResolver: ClientRuntime.AuthSchemeResolver {
12+
// Intentionally empty.
13+
// This is the parent protocol that all auth scheme resolver implementations of
14+
// the service Weather must conform to.
15+
}
16+
17+
public struct DefaultWeatherAuthSchemeResolver: WeatherAuthSchemeResolver {
18+
public func resolveAuthScheme(params: ClientRuntime.AuthSchemeResolverParameters) throws -> [AuthOption] {
19+
var validAuthOptions = [AuthOption]()
20+
guard let serviceParams = params as? WeatherAuthSchemeResolverParameters else {
21+
throw ClientError.authError("Service specific auth scheme parameters type must be passed to auth scheme resolver.")
22+
}
23+
switch serviceParams.operation {
24+
case "onlyHttpApiKeyAuth":
25+
validAuthOptions.append(AuthOption(schemeID: "smithy.api#httpApiKeyAuth"))
26+
case "onlyHttpApiKeyAuthOptional":
27+
validAuthOptions.append(AuthOption(schemeID: "smithy.api#httpApiKeyAuth"))
28+
validAuthOptions.append(AuthOption(schemeID: "smithy.api#noAuth"))
29+
case "onlyHttpBearerAuth":
30+
validAuthOptions.append(AuthOption(schemeID: "smithy.api#httpBearerAuth"))
31+
case "onlyHttpBearerAuthOptional":
32+
validAuthOptions.append(AuthOption(schemeID: "smithy.api#httpBearerAuth"))
33+
validAuthOptions.append(AuthOption(schemeID: "smithy.api#noAuth"))
34+
case "onlyHttpApiKeyAndBearerAuth":
35+
validAuthOptions.append(AuthOption(schemeID: "smithy.api#httpApiKeyAuth"))
36+
validAuthOptions.append(AuthOption(schemeID: "smithy.api#httpBearerAuth"))
37+
case "onlyHttpApiKeyAndBearerAuthReversed":
38+
validAuthOptions.append(AuthOption(schemeID: "smithy.api#httpBearerAuth"))
39+
validAuthOptions.append(AuthOption(schemeID: "smithy.api#httpApiKeyAuth"))
40+
case "onlySigv4Auth":
41+
var sigV4Option = AuthOption(schemeID: "aws.auth#sigv4")
42+
sigV4Option.signingProperties.set(key: AttributeKeys.signingName, value: "weather")
43+
guard let region = serviceParams.region else {
44+
throw ClientError.authError("Missing region in auth scheme parameters for SigV4 auth scheme.")
45+
}
46+
sigV4Option.signingProperties.set(key: AttributeKeys.signingRegion, value: region)
47+
validAuthOptions.append(sigV4Option)
48+
case "onlySigv4AuthOptional":
49+
var sigV4Option = AuthOption(schemeID: "aws.auth#sigv4")
50+
sigV4Option.signingProperties.set(key: AttributeKeys.signingName, value: "weather")
51+
guard let region = serviceParams.region else {
52+
throw ClientError.authError("Missing region in auth scheme parameters for SigV4 auth scheme.")
53+
}
54+
sigV4Option.signingProperties.set(key: AttributeKeys.signingRegion, value: region)
55+
validAuthOptions.append(sigV4Option)
56+
validAuthOptions.append(AuthOption(schemeID: "smithy.api#noAuth"))
57+
case "onlyFakeAuth":
58+
validAuthOptions.append(AuthOption(schemeID: "common#fakeAuth"))
59+
case "onlyFakeAuthOptional":
60+
validAuthOptions.append(AuthOption(schemeID: "common#fakeAuth"))
61+
validAuthOptions.append(AuthOption(schemeID: "smithy.api#noAuth"))
62+
default:
63+
var sigV4Option = AuthOption(schemeID: "aws.auth#sigv4")
64+
sigV4Option.signingProperties.set(key: AttributeKeys.signingName, value: "weather")
65+
guard let region = serviceParams.region else {
66+
throw ClientError.authError("Missing region in auth scheme parameters for SigV4 auth scheme.")
67+
}
68+
sigV4Option.signingProperties.set(key: AttributeKeys.signingRegion, value: region)
69+
validAuthOptions.append(sigV4Option)
70+
}
71+
return validAuthOptions
72+
}
73+
74+
public func constructParameters(context: HttpContext) throws -> ClientRuntime.AuthSchemeResolverParameters {
75+
guard let opName = context.getOperation() else {
76+
throw ClientError.dataNotFound("Operation name not configured in middleware context for auth scheme resolver params construction.")
77+
}
78+
let opRegion = context.getRegion()
79+
return WeatherAuthSchemeResolverParameters(operation: opName, region: opRegion)
80+
}
81+
}

Sources/WeatherSDK/Plugins.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Code generated by smithy-swift-codegen. DO NOT EDIT!
2+
3+
import ClientRuntime
4+
5+

Sources/WeatherSDK/Waiters.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Code generated by smithy-swift-codegen. DO NOT EDIT!
2+
3+
import ClientRuntime
4+
5+
extension WeatherClient {
6+
}

0 commit comments

Comments
 (0)