-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored logic in order to add unit tests for new functionality
- Loading branch information
Showing
5 changed files
with
68 additions
and
6 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
Example/Tests/NonCachingURLSessionConfigurationFactoryTests.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,39 @@ | ||
// | ||
// NonCachingURLSessionConfigurationFactoryTests.swift | ||
// Hyperconnectivity_Tests | ||
// | ||
// Created by Ross Butler on 20/03/2022. | ||
// Copyright © 2022 CocoaPods. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
@testable import Hyperconnectivity | ||
import XCTest | ||
|
||
class NonCachingURLSessionConfigurationFactoryTests: XCTestCase { | ||
/// Tests that given a `URLSessionConfiguration` object with a response cache set, the factory will return a `URLSessionConfiguration` object which does not cache. | ||
func testResponseCachingIsDisabled() { | ||
let sut = NonCachingURLSessionConfigurationFactory() | ||
let originalURLSessionConfiguration = URLSessionConfiguration.default | ||
XCTAssertNotNil(originalURLSessionConfiguration.urlCache) | ||
let newURLSessionConfiguration = sut.urlSessionConfiguration(from: originalURLSessionConfiguration) | ||
XCTAssertNil(newURLSessionConfiguration.urlCache, "URL cache should be nil.") | ||
} | ||
|
||
/// Tests that the `URLSessionConfiguration` passed as input to the factory is copied i.e. the output of the factory is a different object. | ||
func testThatFactoryProducesACopy() { | ||
let sut = NonCachingURLSessionConfigurationFactory() | ||
let originalURLSessionConfiguration = URLSessionConfiguration.default | ||
let newURLSessionConfiguration = sut.urlSessionConfiguration(from: originalURLSessionConfiguration) | ||
XCTAssertFalse(originalURLSessionConfiguration === newURLSessionConfiguration) | ||
} | ||
|
||
/// Tests that given a `URLSessionConfiguration` object passed as input to the factory, the request caching policy of the output ignores the local cache. | ||
func testRequestCachePolicyIgnoresLocalCache() { | ||
let sut = NonCachingURLSessionConfigurationFactory() | ||
let originalURLSessionConfiguration = URLSessionConfiguration.default | ||
XCTAssertEqual(originalURLSessionConfiguration.requestCachePolicy, .useProtocolCachePolicy) | ||
let newURLSessionConfiguration = sut.urlSessionConfiguration(from: originalURLSessionConfiguration) | ||
XCTAssertEqual(newURLSessionConfiguration.requestCachePolicy, .reloadIgnoringLocalCacheData) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Hyperconnectivity/Classes/Combine/NonCachingURLSessionConfigurationFactory.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,18 @@ | ||
// | ||
// NonCachingURLSessionConfigurationFactory.swift | ||
// Hyperconnectivity | ||
// | ||
// Created by Ross Butler on 20/03/2022. | ||
// | ||
|
||
import Foundation | ||
|
||
struct NonCachingURLSessionConfigurationFactory { | ||
func urlSessionConfiguration(from input: URLSessionConfiguration) -> URLSessionConfiguration { | ||
// Ensure that we never use cached results, including where using a custom `URLSessionConfiguration`. | ||
let output = input.copy() as! URLSessionConfiguration | ||
output.requestCachePolicy = .reloadIgnoringCacheData | ||
output.urlCache = nil | ||
return output | ||
} | ||
} |
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