-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom HTTP session abilities. (#344)
* Use associatedtypes to clean up URLSession conformance * Add ability to have custom HTTP sessions. * Added/updated tests * Fixed linux exclusion
- Loading branch information
Showing
8 changed files
with
191 additions
and
57 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
11 changes: 11 additions & 0 deletions
11
Sources/Segment/Utilities/Networking/HTTPSession+Apple.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,11 @@ | ||
import Foundation | ||
|
||
#if os(Linux) || os(Windows) | ||
import FoundationNetworking | ||
#endif | ||
|
||
extension URLSessionDataTask: DataTask {} | ||
extension URLSessionUploadTask: UploadTask {} | ||
|
||
// Give the built in `URLSession` conformance to HTTPSession so that it can easily be used | ||
extension URLSession: HTTPSession {} |
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,34 @@ | ||
import Foundation | ||
|
||
#if os(Linux) || os(Windows) | ||
import FoundationNetworking | ||
#endif | ||
|
||
public protocol DataTask { | ||
var state: URLSessionTask.State { get } | ||
func resume() | ||
} | ||
|
||
public protocol UploadTask: DataTask {} | ||
|
||
// An enumeration of default `HTTPSession` configurations to be used | ||
// This can be extended buy consumer to easily refer back to their configured session. | ||
public enum HTTPSessions { | ||
/// An implementation of `HTTPSession` backed by Apple's `URLSession`. | ||
public static func urlSession() -> any HTTPSession { | ||
let configuration = URLSessionConfiguration.ephemeral | ||
configuration.httpMaximumConnectionsPerHost = 2 | ||
let session = URLSession(configuration: configuration, delegate: nil, delegateQueue: nil) | ||
return session | ||
} | ||
} | ||
|
||
public protocol HTTPSession { | ||
associatedtype DataTaskType: DataTask | ||
associatedtype UploadTaskType: UploadTask | ||
|
||
func uploadTask(with request: URLRequest, fromFile file: URL, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> UploadTaskType | ||
func uploadTask(with request: URLRequest, from bodyData: Data?, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> UploadTaskType | ||
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> DataTaskType | ||
func finishTasksAndInvalidate() | ||
} |
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 |
---|---|---|
|
@@ -5,28 +5,76 @@ | |
// Created by Brandon Sneed on 1/21/21. | ||
// | ||
|
||
#if !os(Linux) | ||
|
||
import XCTest | ||
@testable import Segment | ||
|
||
class HTTPClientTests: XCTestCase { | ||
|
||
override func setUpWithError() throws { | ||
// Put setup code here. This method is called before the invocation of each test method in the class. | ||
RestrictedHTTPSession.reset() | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
// Put teardown code here. This method is called after the invocation of each test method in the class. | ||
} | ||
|
||
/*func testExample() throws { | ||
func testCustomHTTPSessionUpload() throws { | ||
// Use a specific writekey to this test so we do not collide with other cached items. | ||
let analytics = Analytics( | ||
configuration: Configuration(writeKey: "testCustomSesh") | ||
.flushInterval(9999) | ||
.flushAt(9999) | ||
.httpSession(RestrictedHTTPSession()) | ||
) | ||
|
||
waitUntilStarted(analytics: analytics) | ||
|
||
analytics.storage.hardReset(doYouKnowHowToUseThis: true) | ||
|
||
analytics.identify(userId: "brandon", traits: MyTraits(email: "[email protected]")) | ||
|
||
let flushDone = XCTestExpectation(description: "flush done") | ||
analytics.flush { | ||
flushDone.fulfill() | ||
} | ||
|
||
wait(for: [flushDone]) | ||
|
||
XCTAssertEqual(RestrictedHTTPSession.fileUploads, 1) | ||
} | ||
|
||
func testPerformanceExample() throws { | ||
// This is an example of a performance test case. | ||
self.measure { | ||
// Put the code you want to measure the time of here. | ||
|
||
func testDefaultHTTPSessionUpload() throws { | ||
// Use a specific writekey to this test so we do not collide with other cached items. | ||
let analytics = Analytics( | ||
configuration: Configuration(writeKey: "testDefaultSesh") | ||
.flushInterval(9999) | ||
.flushAt(9999) | ||
) | ||
|
||
// reach in and set it, would be the same as the default ultimately | ||
let segment = analytics.find(pluginType: SegmentDestination.self) | ||
XCTAssertTrue(!(segment?.httpClient?.session is RestrictedHTTPSession)) | ||
XCTAssertTrue(segment?.httpClient?.session is URLSession) | ||
segment?.httpClient?.session = RestrictedHTTPSession() | ||
|
||
waitUntilStarted(analytics: analytics) | ||
|
||
analytics.storage.hardReset(doYouKnowHowToUseThis: true) | ||
|
||
analytics.identify(userId: "brandon", traits: MyTraits(email: "[email protected]")) | ||
|
||
let flushDone = XCTestExpectation(description: "flush done") | ||
analytics.flush { | ||
flushDone.fulfill() | ||
} | ||
}*/ | ||
|
||
|
||
wait(for: [flushDone]) | ||
|
||
XCTAssertEqual(RestrictedHTTPSession.fileUploads, 1) | ||
} | ||
} | ||
|
||
#endif |
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