Skip to content

Commit b3fa847

Browse files
authored
Implement hook for data reading from remote URL (#5049)
1 parent 17bc8ed commit b3fa847

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

Package.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ var dependencies: [Package.Dependency] {
105105
[
106106
.package(
107107
url: "https://github.com/apple/swift-foundation-icu",
108-
from: "0.0.9"),
108+
branch: "main"),
109109
.package(
110110
url: "https://github.com/apple/swift-foundation",
111-
revision: "acae3d26b69113cec2db7772b4144ab9558241db")
111+
branch: "main")
112112
]
113113
}
114114
}

Sources/FoundationNetworking/URLSession/NetworkingSpecific.swift

+10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ internal func NSRequiresConcreteImplementation(_ fn: String = #function, file: S
3535
fatalError("\(fn) must be overridden", file: file, line: line)
3636
}
3737

38+
extension Data {
39+
@_dynamicReplacement(for: init(_contentsOfRemote:options:))
40+
private init(_contentsOfRemote_foundationNetworking url: URL, options: Data.ReadingOptions = []) throws {
41+
let (nsData, _) = try _NSNonfileURLContentLoader().contentsOf(url: url)
42+
self = withExtendedLifetime(nsData) {
43+
return Data(bytes: nsData.bytes, count: nsData.length)
44+
}
45+
}
46+
}
47+
3848
@usableFromInline
3949
class _NSNonfileURLContentLoader: _NSNonfileURLContentLoading, @unchecked Sendable {
4050
@usableFromInline

Tests/Foundation/TestURL.swift

+12
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,18 @@ class TestURL : XCTestCase {
775775
throw error
776776
}
777777
}
778+
779+
func test_dataFromNonFileURL() {
780+
do {
781+
// Tests the up-call to FoundationNetworking to perform the network request
782+
try Data(contentsOf: URL(string: "https://swift.org")!)
783+
} catch {
784+
if let cocoaCode = (error as? CocoaError)?.code {
785+
// Just ensure that we supported this feature, even if the request failed
786+
XCTAssertNotEqual(cocoaCode, .featureUnsupported)
787+
}
788+
}
789+
}
778790

779791
// MARK: -
780792

0 commit comments

Comments
 (0)