-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement TestableNetworkRequest using @testable import for access to NetworkRequest Override Equatable and Hashable implementations for use in testing comparisons * Refactor NetworkRequestHelper data properties to use new TestableNetworkRequest as keys Refactor usages to use TestableNetworkRequest methods Rename setResponseFor to addResponseFor to more accurately reflect what the method does * Refactor Mock and RealNetworkService based on changes to NetworkServiceHelper * Refactor UpstreamIntegrationTests based on changes to RealNetworkService * Add convenience init to TestableNetworkRequest and update usages * Update implementation to use lowercased string comparison and hash for alignment * Reorder operations to avoid race condition in MockNetworkService By doing the countdown AFTER notifying the completion handler, awaits on the expected network request can properly gate the rest of the test case logic (for example, if the test case resets the mock network service, there isn't a race condition between the reset and the get mock response, due to prematurely ungated await) * Remove unused NetworkService isCustomEquals method * Update docs to reflect actual method name * Align the logical implementations of isEqual and hash * Refactor networkResponses data struct to be 1:1 request to response Refactor related logic for getting and setting, and usage sites * Add method and class docs for TestableNetworkRequest * Remove defaultMockResponse property, and place it inline with only place it is used * Update method doc for getResponseFor
- Loading branch information
1 parent
c3ce8a6
commit 285aeb9
Showing
6 changed files
with
142 additions
and
67 deletions.
There are no files selected for viewing
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
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
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
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
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,73 @@ | ||
// | ||
// Copyright 2023 Adobe. All rights reserved. | ||
// This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. You may obtain a copy | ||
// of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software distributed under | ||
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
// OF ANY KIND, either express or implied. See the License for the specific language | ||
// governing permissions and limitations under the License. | ||
// | ||
|
||
|
||
import Foundation | ||
@testable import AEPServices | ||
|
||
/// `TestableNetworkRequest` is a specialized subclass of `NetworkRequest` for use in testing scenarios. | ||
/// It provides custom, overriding logic for the `Equatable` and `Hashable` protocols, and is meant for direct use as keys | ||
/// in collections that rely on the previously mentioned protocols for uniqueness (dictionaries, sets, etc.). | ||
class TestableNetworkRequest: NetworkRequest { | ||
/// Construct from existing `NetworkRequest` instance | ||
convenience init(from networkRequest: NetworkRequest) { | ||
self.init(url: networkRequest.url, | ||
httpMethod: networkRequest.httpMethod, | ||
connectPayloadData: networkRequest.connectPayload, | ||
httpHeaders: networkRequest.httpHeaders, | ||
connectTimeout: networkRequest.connectTimeout, | ||
readTimeout: networkRequest.readTimeout) | ||
} | ||
|
||
// Note that the Equatable and Hashable conformance logic needs to align exactly for it to work as expected | ||
// in the case of dictionary keys. Lowercased is used because across current test cases it has the same | ||
// properties as case insensitive compare, and is straightforward to implement for isEqual and hash. However, | ||
// if there are new cases where lowercased does not satisfy the property of case insensitive compare, this logic | ||
// will need to be updated accordingly to handle that case. | ||
|
||
// MARK: - Equatable (ObjC) conformance | ||
/// Determines equality by comparing the URL's scheme, host, path, and HTTP method, while excluding query parameters | ||
/// (and any other NetworkRequest properties). | ||
/// | ||
/// Note that host and scheme use `String.lowercased()` to perform case insensitive comparison. | ||
/// | ||
/// - Parameter object: The object to be compared with the current instance. | ||
/// - Returns: A boolean value indicating whether the given object is equal to the current instance. | ||
override func isEqual(_ object: Any?) -> Bool { | ||
guard let other = object as? NetworkRequest else { | ||
return false | ||
} | ||
|
||
return url.host?.lowercased() == other.url.host?.lowercased() | ||
&& url.scheme?.lowercased() == other.url.scheme?.lowercased() | ||
&& url.path == other.url.path | ||
&& httpMethod.rawValue == other.httpMethod.rawValue | ||
} | ||
|
||
// MARK: - Hashable (ObjC) conformance | ||
/// Determines the hash value by combining the URL's scheme, host, path, and HTTP method, while excluding query parameters | ||
/// (and any other NetworkRequest properties). | ||
/// | ||
/// Note that host and scheme use `String.lowercased()` to perform case insensitive combination. | ||
public override var hash: Int { | ||
var hasher = Hasher() | ||
if let scheme = url.scheme { | ||
hasher.combine(scheme.lowercased()) | ||
} | ||
if let host = url.host { | ||
hasher.combine(host.lowercased()) | ||
} | ||
hasher.combine(url.path) | ||
hasher.combine(httpMethod.rawValue) | ||
return hasher.finalize() | ||
} | ||
} |
Oops, something went wrong.