-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
...ins/Auth/Tests/AWSCognitoAuthPluginUnitTests/AWSCognitoAuthPluginAppSyncSignerTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import XCTest | ||
@testable import Amplify | ||
@testable import AWSCognitoAuthPlugin | ||
|
||
class AWSCognitoAuthPluginAppSyncSignerTests: XCTestCase { | ||
|
||
/// Tests translating the URLRequest to the SDKRequest | ||
/// The translation should account for expected fields, as asserted in the test. | ||
func testCreateAppSyncSdkHttpRequestBuilder() throws { | ||
var urlRequest = URLRequest(url: URL(string: "http://graphql.com")!) | ||
urlRequest.httpMethod = "post" | ||
let dataObject = Data() | ||
urlRequest.httpBody = dataObject | ||
guard let sdkRequestBuilder = try AWSCognitoAuthPlugin.createAppSyncSdkHttpRequestBuilder(urlRequest: urlRequest) else { | ||
XCTFail("Could not create SDK request") | ||
return | ||
} | ||
|
||
let request = sdkRequestBuilder.build() | ||
XCTAssertEqual(request.host, "graphql.com") | ||
XCTAssertEqual(request.path, "") | ||
XCTAssertEqual(request.queryItems, []) | ||
XCTAssertEqual(request.method, .post) | ||
XCTAssertEqual(request.endpoint.port, 443) | ||
XCTAssertEqual(request.endpoint.protocolType, .https) | ||
XCTAssertEqual(request.endpoint.headers?.headers, [.init(name: "host", value: "graphql.com")]) | ||
guard case let .data(data) = request.body else { | ||
XCTFail("Unexpected body") | ||
return | ||
} | ||
XCTAssertEqual(data, dataObject) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...s/Auth/Tests/AuthHostApp/AuthIntegrationTests/AppSyncSignerTests/AppSyncSignerTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import XCTest | ||
@testable import Amplify | ||
import AWSCognitoAuthPlugin | ||
|
||
class AppSyncSignerTests: AWSAuthBaseTest { | ||
|
||
/// Test signing an AppSync request with a live credentials provider | ||
/// | ||
/// - Given: Base test configures Amplify and adds AWSCognitoAuthPlugin | ||
/// - When: | ||
/// - I invoke AWSCognitoAuthPlugin.signAppSyncRequest(request, region) | ||
/// - Then: | ||
/// - I should get a signed request. | ||
/// | ||
func testSignAppSyncRequest() async throws { | ||
let request = URLRequest(url: URL(string: "http://graphql.com")!) | ||
let signedRequest = try await AWSCognitoAuthPlugin.signAppSyncRequest(request, region: "us-east-1") | ||
|
||
guard let headers = signedRequest.allHTTPHeaderFields else { | ||
XCTFail("Missing headers") | ||
return | ||
} | ||
XCTAssertEqual(headers.count, 4) | ||
let containsExpectedHeaders = headers.keys.contains(where: { key in | ||
key == "Authorization" || key == "Host" || key == "X-Amz-Security-Token" || key == "X-Amz-Date" | ||
}) | ||
XCTAssertTrue(containsExpectedHeaders) | ||
} | ||
} |