From 171e08987331dec5af7ecf89aa284d9a9dcb207d Mon Sep 17 00:00:00 2001 From: Brian Michel Date: Thu, 18 Jan 2024 17:52:54 -0500 Subject: [PATCH] feat: Update event source to use AnyURLSession (#1) * Update event source to use AnyURLSession * Fix targeting for CI runs * Fix swift versions * Point to different anyurlsession commit * Adjust test for session creation swapping * Update comment in ci --- .github/workflows/ci.yml | 19 ++++++++++++------- Package.resolved | 16 ++++++++++++++++ Package.swift | 9 +++++++-- Source/LDSwiftEventSource.swift | 7 ++++++- Tests/LDSwiftEventSourceTests.swift | 23 +++++++++++++++++++++++ 5 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 Package.resolved diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 04ce38c..1df104e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,11 +1,11 @@ name: Run CI on: push: - branches: [ main ] + branches: [ main-bcny ] paths-ignore: - '**.md' # Do not need to run CI for markdown changes. pull_request: - branches: [ main ] + branches: [ main-bcny ] paths-ignore: - '**.md' @@ -44,11 +44,16 @@ jobs: strategy: matrix: swift-version: - - 5.1 - - 5.2 - - 5.3 - - 5.4 - - 5.5 + # This package originally goes back to Swift 5.0 + # however, we're unable to specify a dependency at a specific SHA + # until Swift version 5.5+ so while these reflect what used to be + # supported by the base package, this fork requires a different + # minimum toolchain and package description. + # - 5.1 + # - 5.2 + # - 5.3 + # - 5.4 + # - 5.5 - 5.6 - 5.7 - 5.8 diff --git a/Package.resolved b/Package.resolved new file mode 100644 index 0000000..0e78e34 --- /dev/null +++ b/Package.resolved @@ -0,0 +1,16 @@ +{ + "object": { + "pins": [ + { + "package": "swift-anyurlsession", + "repositoryURL": "https://github.com/thebrowsercompany/AnyURLSession", + "state": { + "branch": "9b8e39b", + "revision": "9b8e39b95f8290909b976619eb00dc8c9ba472bf", + "version": null + } + } + ] + }, + "version": 1 +} diff --git a/Package.swift b/Package.swift index 943a0c5..175528d 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.0 +// swift-tools-version:5.5 import PackageDescription @@ -13,10 +13,15 @@ let package = Package( products: [ .library(name: "LDSwiftEventSource", targets: ["LDSwiftEventSource"]), ], - dependencies: [], + dependencies: [ + .package(url: "https://github.com/thebrowsercompany/AnyURLSession", revision: "9b8e39b") + ], targets: [ .target( name: "LDSwiftEventSource", + dependencies: [ + .product(name: "AnyURLSession", package: "AnyURLSession") + ], path: "Source"), .testTarget( name: "LDSwiftEventSourceTests", diff --git a/Source/LDSwiftEventSource.swift b/Source/LDSwiftEventSource.swift index 86952d2..3853c0f 100644 --- a/Source/LDSwiftEventSource.swift +++ b/Source/LDSwiftEventSource.swift @@ -1,7 +1,12 @@ import Foundation #if os(Linux) || os(Windows) -import FoundationNetworking +import AnyURLSession + +import class FoundationNetworking.HTTPURLResponse +import struct FoundationNetworking.URLRequest +import class FoundationNetworking.URLResponse +import class FoundationNetworking.URLSessionConfiguration #endif /** diff --git a/Tests/LDSwiftEventSourceTests.swift b/Tests/LDSwiftEventSourceTests.swift index e66b889..fd65be7 100644 --- a/Tests/LDSwiftEventSourceTests.swift +++ b/Tests/LDSwiftEventSourceTests.swift @@ -3,6 +3,7 @@ import XCTest #if os(Linux) || os(Windows) import FoundationNetworking +import AnyURLSession #endif final class LDSwiftEventSourceTests: XCTestCase { @@ -103,6 +104,28 @@ final class LDSwiftEventSourceTests: XCTestCase { } func testCreatedSession() { + #if os(Linux) || os(Windows) + final class TestURLSessionGuts: URLSessionGuts { + var configuration: URLSessionConfiguration + + init(configuration: URLSessionConfiguration, delegate: (any AnyURLSession.URLSessionDelegate)?, delegateQueue queue: OperationQueue?) { + self.configuration = configuration + } + + func uploadTask(with request: URLRequest, fromFile file: URL, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> AnyURLSession.URLSessionUploadTask { AnyURLSession.URLSessionUploadTask() } + func dataTask(with request: URLRequest) -> AnyURLSession.URLSessionDataTask { AnyURLSession.URLSessionDataTask() } + func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> AnyURLSession.URLSessionDataTask { AnyURLSession.URLSessionDataTask() } + func invalidateAndCancel() {} + func finishTasksAndInvalidate() {} + } + + AnyURLSession.Dependencies.current.setValue( + Dependencies(gutsFactory: { (config, delegate, queue) in + TestURLSessionGuts(configuration: config, delegate: delegate, delegateQueue: queue) + }) + ) + + #endif let config = EventSource.Config(handler: mockHandler, url: URL(string: "abc")!) let session = EventSourceDelegate(config: config).createSession() XCTAssertEqual(session.configuration.timeoutIntervalForRequest, config.idleTimeout)