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

RUMM-1234 DDNSURLSessionDelegate build errors fixed #461

Merged
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
4 changes: 4 additions & 0 deletions Datadog/Datadog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@
9E68FB56244707FD0013A8AA /* ObjcExceptionHandler.h in Headers */ = {isa = PBXBuildFile; fileRef = 9E68FB54244707FD0013A8AA /* ObjcExceptionHandler.h */; settings = {ATTRIBUTES = (Public, ); }; };
9E989A4225F640D100235FC3 /* AppStateListenerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E989A4125F640D100235FC3 /* AppStateListenerTests.swift */; };
9ED6A6B425F2901800CB2E29 /* AppStateListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9ED6A6B325F2901800CB2E29 /* AppStateListener.swift */; };
9EE5AD8226205B82001E699E /* DDNSURLSessionDelegateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EE5AD8126205B82001E699E /* DDNSURLSessionDelegateTests.swift */; };
9EEA4871258B76A100EBDA9D /* Global+objc.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EEA4870258B76A100EBDA9D /* Global+objc.swift */; };
9EF963E82537556300235F98 /* DDURLSessionDelegateAsSuperclassTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EF963E72537556300235F98 /* DDURLSessionDelegateAsSuperclassTests.swift */; };
9EFD112C24B32D29003A1A2B /* FirstPartyURLsFilter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EFD112B24B32D29003A1A2B /* FirstPartyURLsFilter.swift */; };
Expand Down Expand Up @@ -971,6 +972,7 @@
9E989A4125F640D100235FC3 /* AppStateListenerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppStateListenerTests.swift; sourceTree = "<group>"; };
9E9EB37624468CE90002C80B /* Datadog.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = Datadog.modulemap; sourceTree = "<group>"; };
9ED6A6B325F2901800CB2E29 /* AppStateListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppStateListener.swift; sourceTree = "<group>"; };
9EE5AD8126205B82001E699E /* DDNSURLSessionDelegateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DDNSURLSessionDelegateTests.swift; sourceTree = "<group>"; };
9EEA4870258B76A100EBDA9D /* Global+objc.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Global+objc.swift"; sourceTree = "<group>"; };
9EF49F1624476FBD004F2CA0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
9EF49F17244770AD004F2CA0 /* DatadogIntegrationTests.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = DatadogIntegrationTests.xcconfig; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1386,6 +1388,7 @@
615A4A8824A34FD700233986 /* DDTracerTests.swift */,
615A4A8624A3452800233986 /* DDTracerConfigurationTests.swift */,
616B668D259CC28E00968EE8 /* DDRUMMonitorTests.swift */,
9EE5AD8126205B82001E699E /* DDNSURLSessionDelegateTests.swift */,
);
path = DatadogObjc;
sourceTree = "<group>";
Expand Down Expand Up @@ -3443,6 +3446,7 @@
61E917CF2464270500E6C631 /* EncodableValueTests.swift in Sources */,
61133C542423990D00786299 /* NetworkConnectionInfoProviderTests.swift in Sources */,
616B668E259CC28E00968EE8 /* DDRUMMonitorTests.swift in Sources */,
9EE5AD8226205B82001E699E /* DDNSURLSessionDelegateTests.swift in Sources */,
6182374325D3DFD5006A375B /* CrashReportingWithRUMIntegrationTests.swift in Sources */,
61B558CF2469561C001460D3 /* LoggerBuilderTests.swift in Sources */,
61133C4A2423990D00786299 /* DDConfigurationTests.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,27 @@

import Foundation

/// An interface for forwarding `URLSessionDelegate` calls to `DDURLSessionDelegate`.
/// The implementation must ensure that required methods are called on the `ddURLSessionDelegate`.
@objc
public protocol __URLSessionDelegateProviding: URLSessionDelegate {
/// Datadog delegate object.
/// The class implementing `DDURLSessionDelegateProviding` must ensure that following method calls are forwarded to `ddURLSessionDelegate`:
// - `func urlSession(_:task:didFinishCollecting:)`
// - `func urlSession(_:task:didCompleteWithError:)`
var ddURLSessionDelegate: DDURLSessionDelegate { get }
}

/// The `URLSession` delegate object which enables network requests instrumentation. **It must be
/// used together with** `Datadog.Configuration.trackURLSession(firstPartyHosts:)`.
///
/// All requests made with the `URLSession` instrumented with this delegate will be intercepted by the SDK.
@objc
open class DDURLSessionDelegate: NSObject, URLSessionTaskDelegate {
open class DDURLSessionDelegate: NSObject, URLSessionTaskDelegate, __URLSessionDelegateProviding {
public var ddURLSessionDelegate: DDURLSessionDelegate {
return self
}

var interceptor: URLSessionInterceptorType?
let firstPartyURLsFilter: FirstPartyURLsFilter?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ internal class URLSessionSwizzler {
typealias Signature = @convention(block) (URLSession, URLRequest, CompletionHandler?) -> URLSessionDataTask
swizzle(method) { previousImplementation -> Signature in
return { session, urlRequest, completionHandler -> URLSessionDataTask in
guard let interceptor = (session.delegate as? DDURLSessionDelegate)?.interceptor else {
guard let interceptor = (session.delegate as? __URLSessionDelegateProviding)?.ddURLSessionDelegate.interceptor else {
return previousImplementation(session, Self.selector, urlRequest, completionHandler)
}
let task: URLSessionDataTask
Expand Down Expand Up @@ -129,7 +129,7 @@ internal class URLSessionSwizzler {
typealias Signature = @convention(block) (URLSession, URL, CompletionHandler?) -> URLSessionDataTask
swizzle(method) { previousImplementation -> Signature in
return { session, url, completionHandler -> URLSessionDataTask in
guard let interceptor = (session.delegate as? DDURLSessionDelegate)?.interceptor else {
guard let interceptor = (session.delegate as? __URLSessionDelegateProviding)?.ddURLSessionDelegate.interceptor else {
return previousImplementation(session, Self.selector, url, completionHandler)
}
let task: URLSessionDataTask
Expand Down Expand Up @@ -183,7 +183,7 @@ internal class URLSessionSwizzler {
typealias Signature = @convention(block) (URLSession, URLRequest) -> URLSessionDataTask
swizzle(method) { previousImplementation -> Signature in
return { session, urlRequest -> URLSessionDataTask in
guard let interceptor = (session.delegate as? DDURLSessionDelegate)?.interceptor else {
guard let interceptor = (session.delegate as? __URLSessionDelegateProviding)?.ddURLSessionDelegate.interceptor else {
return previousImplementation(session, Self.selector, urlRequest)
}
let newRequest = interceptor.modify(request: urlRequest, session: session)
Expand Down Expand Up @@ -227,7 +227,7 @@ internal class URLSessionSwizzler {
typealias Signature = @convention(block) (URLSession, URL) -> URLSessionDataTask
swizzle(method) { previousImplementation -> Signature in
return { session, url -> URLSessionDataTask in
guard let interceptor = (session.delegate as? DDURLSessionDelegate)?.interceptor else {
guard let interceptor = (session.delegate as? __URLSessionDelegateProviding)?.ddURLSessionDelegate.interceptor else {
return previousImplementation(session, Self.selector, url)
}
let task = previousImplementation(session, Self.selector, url)
Expand Down
25 changes: 24 additions & 1 deletion Sources/DatadogObjc/DDURLSessionDelegate+objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,27 @@ import Foundation
import Datadog

@objc
public class DDNSURLSessionDelegate: DDURLSessionDelegate {}
open class DDNSURLSessionDelegate: NSObject, URLSessionTaskDelegate, __URLSessionDelegateProviding {
var swiftDelegate: DDURLSessionDelegate
public var ddURLSessionDelegate: DDURLSessionDelegate {
return swiftDelegate
}

@objc
override public init() {
swiftDelegate = DDURLSessionDelegate()
}

@objc
public init(additionalFirstPartyHosts: Set<String>) {
swiftDelegate = DDURLSessionDelegate(additionalFirstPartyHosts: additionalFirstPartyHosts)
}

open func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
swiftDelegate.urlSession(session, task: task, didCompleteWithError: error)
}

open func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
swiftDelegate.urlSession(session, task: task, didFinishCollecting: metrics)
}
}
ncreated marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ extension URLSession {
}

extension URLSessionTask {
static func mockAny() -> URLSessionTask {
return URLSessionTaskMock(request: .mockAny(), response: .mockAny())
}

static func mockWith(request: URLRequest, response: HTTPURLResponse) -> URLSessionTask {
return URLSessionTaskMock(request: request, response: response)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@

import XCTest
import Datadog
import DatadogObjc

@objc
public class SubDDNSURLSessionDelegate: DDNSURLSessionDelegate {
override public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
super.urlSession(session, task: task, didCompleteWithError: error)
}

override public func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
super.urlSession(session, task: task, didFinishCollecting: metrics)
}
}
ncreated marked this conversation as resolved.
Show resolved Hide resolved

internal final class SubDDURLSessionDelegate: DDURLSessionDelegate {
let property: String
Expand All @@ -22,5 +34,6 @@ class DDURLSessionDelegateAsSuperclassTests: XCTestCase {
func testSubclassability() {
// Success: tests compile, failure: compilation error
_ = SubDDURLSessionDelegate()
_ = SubDDNSURLSessionDelegate()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2019-2020 Datadog, Inc.
*/

import XCTest
@testable import Datadog
@testable import DatadogObjc

private class DDURLSessionDelegateMock: DDURLSessionDelegate {
var calledDidFinishCollecting = false
var calledDidCompleteWithError = false

override func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
calledDidFinishCollecting = true
}

override func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
calledDidCompleteWithError = true
}
}

class DDNSURLSessionDelegateTests: XCTestCase {
func testInit() {
let delegate = DDNSURLSessionDelegate()
XCTAssertNil(delegate.swiftDelegate.firstPartyURLsFilter)
}

func testInitWithAdditionalFirstPartyHosts() {
let delegate = DDNSURLSessionDelegate(additionalFirstPartyHosts: ["foo.com"])
XCTAssertNotNil(delegate.swiftDelegate.firstPartyURLsFilter)
}

func testItForwardsCallsToSwiftDelegate() {
let swiftDelegate = DDURLSessionDelegateMock()
let objcDelegate = DDNSURLSessionDelegate()
objcDelegate.swiftDelegate = swiftDelegate

objcDelegate.urlSession(.shared, task: .mockAny(), didFinishCollecting: .mockAny())
objcDelegate.urlSession(.shared, task: .mockAny(), didCompleteWithError: ErrorMock())

XCTAssertTrue(swiftDelegate.calledDidFinishCollecting)
XCTAssertTrue(swiftDelegate.calledDidCompleteWithError)
}
}