Skip to content

Commit

Permalink
Finished fixing forwarding based on responds
Browse files Browse the repository at this point in the history
  • Loading branch information
ArielDemarco committed Nov 29, 2024
1 parent 40b1af2 commit 1b7b90c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,6 @@ class URLSessionDelegateProxy: NSObject {
toSelector selector: Selector,
session: URLSession
) -> DelegateRespondsResult<T> {
// guard that we are not the session.delegate to prevent infinite recursion
guard (session.delegate as? URLSessionDelegateProxy) != self else {
return .doesNotRespond
}

// avoid forwarding the delegate if it was already swizzled by somebody else
// during our swizzling to prevent potential infinite recursion.
guard self.swizzledDelegate == nil else {
return .doesNotRespond
}

// check if the originalDelegate responds to the selector
if let originalDelegate = originalDelegate,
Expand All @@ -86,6 +76,17 @@ class URLSessionDelegateProxy: NSObject {
}
}

// guard that we are not the session.delegate to prevent infinite recursion
guard (session.delegate as? URLSessionDelegateProxy) != self else {
return .doesNotRespond
}

// avoid forwarding the delegate if it was already swizzled by somebody else
// during our swizzling to prevent potential infinite recursion.
guard self.swizzledDelegate == nil else {
return .doesNotRespond
}

// if session delegate also responds to selector, we must call it
if let sessionDelegate = session.delegate,
sessionDelegate.responds(to: selector) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ final class URLSessionDelegateProxyAsTaskDelegateTests: XCTestCase {
], timeout: Self.timeoutQuick)

XCTAssertTrue(try XCTUnwrap(otherSwizzler?.proxy?.didInvokeRespondsTo))
XCTAssertFalse(try XCTUnwrap(otherSwizzler?.proxy?.didInvokeForwardingTarget))
XCTAssertFalse(try XCTUnwrap(otherSwizzler?.proxy?.didForwardToTargetSuccessfully))
XCTAssertFalse(try XCTUnwrap(otherSwizzler?.proxy?.didForwardRespondsToSuccessfullyBool))
XCTAssertTrue(try XCTUnwrap(otherSwizzler?.proxy?.didInvokeForwardingTarget))
XCTAssertTrue(try XCTUnwrap(otherSwizzler?.proxy?.didForwardToTargetSuccessfully))
}

@available(iOS 15.0, watchOS 8.0, *)
Expand Down Expand Up @@ -157,9 +156,8 @@ final class URLSessionDelegateProxyAsTaskDelegateTests: XCTestCase {
], timeout: Self.timeoutQuick)

XCTAssertTrue(try XCTUnwrap(otherSwizzler?.proxy?.didInvokeRespondsTo))
XCTAssertFalse(try XCTUnwrap(otherSwizzler?.proxy?.didInvokeForwardingTarget))
XCTAssertFalse(try XCTUnwrap(otherSwizzler?.proxy?.didForwardToTargetSuccessfully))
XCTAssertFalse(try XCTUnwrap(otherSwizzler?.proxy?.didForwardRespondsToSuccessfullyBool))
XCTAssertTrue(try XCTUnwrap(otherSwizzler?.proxy?.didInvokeForwardingTarget))
XCTAssertTrue(try XCTUnwrap(otherSwizzler?.proxy?.didForwardToTargetSuccessfully))
}

@available(iOS 15.0, watchOS 8.0, *)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ class DummyURLSessionInitWithDelegateSwizzler: Swizzlable {
}

class DummyURLProxy: NSObject, URLSessionDelegate {
weak var originalDelegate: URLSessionDelegate?
var originalDelegate: URLSessionDelegate?
var didForwardToTargetSuccessfully: Bool = false
var didInvokeForwardingTarget: Bool = false
var didInvokeRespondsTo: Bool = false
var didForwardRespondsToSuccessfullyBool = false

init(originalDelegate: URLSessionDelegate?) {
self.originalDelegate = originalDelegate
Expand Down

0 comments on commit 1b7b90c

Please sign in to comment.