Skip to content

Commit

Permalink
Refactored logic in order to add unit tests for new functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
rwbutler committed Mar 20, 2022
1 parent 7043af7 commit c0dbcfb
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Example/Hyperconnectivity.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
0E0DB5B062AA610A7FB6B2F1 /* Pods_Hyperconnectivity_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5906C1669FAA2FD5D578F683 /* Pods_Hyperconnectivity_Tests.framework */; };
134B21BE2466DD5500A8F332 /* ConnectivityStateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 134B21BC2466DC7200A8F332 /* ConnectivityStateTests.swift */; };
134B21C1246877F300A8F332 /* ConnectionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 134B21BF2468779E00A8F332 /* ConnectionTests.swift */; };
134E807027E7AB2700C2A94A /* NonCachingURLSessionConfigurationFactoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 134E806F27E7AB2700C2A94A /* NonCachingURLSessionConfigurationFactoryTests.swift */; };
13962F0A246F3F7300CCE8B9 /* ConnectivitySubscriptionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13962F09246F3F7300CCE8B9 /* ConnectivitySubscriptionTests.swift */; };
13962F12247191B000CCE8B9 /* MockPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13962F11247191B000CCE8B9 /* MockPath.swift */; };
13962F142471929500CCE8B9 /* MockConnectivity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13962F132471929500CCE8B9 /* MockConnectivity.swift */; };
Expand Down Expand Up @@ -46,6 +47,7 @@
/* Begin PBXFileReference section */
134B21BC2466DC7200A8F332 /* ConnectivityStateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectivityStateTests.swift; sourceTree = "<group>"; };
134B21BF2468779E00A8F332 /* ConnectionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionTests.swift; sourceTree = "<group>"; };
134E806F27E7AB2700C2A94A /* NonCachingURLSessionConfigurationFactoryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NonCachingURLSessionConfigurationFactoryTests.swift; sourceTree = "<group>"; };
13962F09246F3F7300CCE8B9 /* ConnectivitySubscriptionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectivitySubscriptionTests.swift; sourceTree = "<group>"; };
13962F11247191B000CCE8B9 /* MockPath.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockPath.swift; sourceTree = "<group>"; };
13962F132471929500CCE8B9 /* MockConnectivity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockConnectivity.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -183,6 +185,7 @@
13962F222471AA1600CCE8B9 /* ConnectivityPublisherTests.swift */,
13C3D88E2471C36800E0A879 /* ReachabilityPublisherTests.swift */,
607FACE91AFB9204008FA782 /* Supporting Files */,
134E806F27E7AB2700C2A94A /* NonCachingURLSessionConfigurationFactoryTests.swift */,
);
path = Tests;
sourceTree = "<group>";
Expand Down Expand Up @@ -428,6 +431,7 @@
13962F12247191B000CCE8B9 /* MockPath.swift in Sources */,
13962F232471AA1600CCE8B9 /* ConnectivityPublisherTests.swift in Sources */,
13962F142471929500CCE8B9 /* MockConnectivity.swift in Sources */,
134E807027E7AB2700C2A94A /* NonCachingURLSessionConfigurationFactoryTests.swift in Sources */,
13962F0A246F3F7300CCE8B9 /* ConnectivitySubscriptionTests.swift in Sources */,
13962F1A247196F800CCE8B9 /* ResponseContainsStringValidatorTests.swift in Sources */,
13962F18247196AE00CCE8B9 /* ResponseStringEqualityValidatorTests.swift in Sources */,
Expand Down
4 changes: 4 additions & 0 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions Example/Tests/NonCachingURLSessionConfigurationFactoryTests.swift
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)
}
}
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
}
}
9 changes: 3 additions & 6 deletions Hyperconnectivity/Classes/Core/Hyperconnectivity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,15 @@ public class Hyperconnectivity {

private extension Hyperconnectivity {
private func checkConnectivity(of path: NWPath, using configuration: Configuration) {
// Ensure that we never use cached results, including where using a custom `URLSessionConfiguration`.
let urlSessionConfiguration = configuration.urlSessionConfiguration.copy() as! URLSessionConfiguration
urlSessionConfiguration.requestCachePolicy = .reloadIgnoringCacheData
urlSessionConfiguration.urlCache = nil

let factory = NonCachingURLSessionConfigurationFactory()
let urlSessionConfiguration = factory.urlSessionConfiguration(from: configuration.urlSessionConfiguration)
let publishers = configuration.connectivityURLs.map { url in
URLSession(configuration: urlSessionConfiguration).dataTaskPublisher(for: url)
}
let totalChecks = UInt(configuration.connectivityURLs.count)
let result = ConnectivityResult(path: path, successThreshold: configuration.successThreshold, totalChecks: totalChecks)
let combinedPublisher = Publishers.MergeMany(publishers)
cancellable = combinedPublisher.sink(receiveCompletion:{ [weak self] _ in
cancellable = combinedPublisher.sink(receiveCompletion:{ [weak self] _ in
self?.connectivityChanged(result)
}, receiveValue: { [weak self] response in
result.connectivityCheck(successful: configuration.isResponseValid(response))
Expand Down

0 comments on commit c0dbcfb

Please sign in to comment.