Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update event source to use AnyURLSession #1

Merged
merged 6 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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
brianmichel marked this conversation as resolved.
Show resolved Hide resolved
# - 5.2
# - 5.3
# - 5.4
# - 5.5
- 5.6
- 5.7
- 5.8
Expand Down
16 changes: 16 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"object": {
"pins": [
{
"package": "swift-anyurlsession",
"repositoryURL": "https://github.com/thebrowsercompany/AnyURLSession",
brianmichel marked this conversation as resolved.
Show resolved Hide resolved
"state": {
"branch": "9b8e39b",
"revision": "9b8e39b95f8290909b976619eb00dc8c9ba472bf",
"version": null
}
}
]
},
"version": 1
}
9 changes: 7 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.0
// swift-tools-version:5.5

import PackageDescription

Expand All @@ -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",
Expand Down
7 changes: 6 additions & 1 deletion Source/LDSwiftEventSource.swift
Original file line number Diff line number Diff line change
@@ -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

/**
Expand Down
23 changes: 23 additions & 0 deletions Tests/LDSwiftEventSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import XCTest

#if os(Linux) || os(Windows)
import FoundationNetworking
import AnyURLSession
#endif

final class LDSwiftEventSourceTests: XCTestCase {
Expand Down Expand Up @@ -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)
Expand Down
Loading