Skip to content

Commit

Permalink
RUMM-1605 Use Native resource type instead of XHR
Browse files Browse the repository at this point in the history
  • Loading branch information
xgouchet committed Sep 23, 2021
1 parent 6fc6d5d commit 4481075
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 19 deletions.
10 changes: 5 additions & 5 deletions Sources/Datadog/RUMMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ internal extension RUMResourceType {
/// - Parameters:
/// - request: the `URLRequest` for the resource.
init?(request: URLRequest) {
let xhrHTTPMethods: Set<String> = ["POST", "PUT", "DELETE"]
let nativeHTTPMethods: Set<String> = ["POST", "PUT", "DELETE"]

if let requestMethod = request.httpMethod?.uppercased(),
xhrHTTPMethods.contains(requestMethod) {
self = .xhr
nativeHTTPMethods.contains(requestMethod) {
self = .native
} else {
return nil
}
Expand All @@ -54,10 +54,10 @@ internal extension RUMResourceType {
case ("font", _): self = .font
case ("text", "css"): self = .css
case ("text", "javascript"): self = .js
default: self = .xhr
default: self = .native
}
} else {
self = .xhr
self = .native
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/DatadogObjc/RUMMonitor+objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public enum DDRUMResourceType: Int {
case js
case media
case other
case native

internal var swiftType: RUMResourceType {
switch self {
Expand All @@ -156,6 +157,7 @@ public enum DDRUMResourceType: Int {
case .font: return .font
case .js: return .js
case .media: return .media
case .native: return .native
default: return .other
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/DatadogTests/Datadog/Mocks/RUMDataModelMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ extension RUMResourceEvent: RandomMockable {
size: .mockRandom(),
ssl: .init(duration: .mockRandom(), start: .mockRandom()),
statusCode: .mockRandom(),
type: [.xhr, .fetch, .image].randomElement()!,
type: [.native, .image].randomElement()!,
url: .mockRandom()
),
service: .mockRandom(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,14 @@ extension URLResponse {

static func mockWith(
statusCode: Int = 200,
mimeType: String = "application/json"
mimeType: String? = "application/json"
) -> HTTPURLResponse {
let headers: [String: String] = (mimeType == nil) ? [:] : ["Content-Type": "\(mimeType!)"]
return HTTPURLResponse(
url: .mockAny(),
statusCode: statusCode,
httpVersion: nil,
headerFields: ["Content-Type": "\(mimeType)"]
headerFields: headers
)!
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ class RUMResourceScopeTests: XCTestCase {
}

func testGivenResourceStartedWithKindBasedOnRequest_whenLoadingEndsWithDifferentKind_itSendsTheKindBasedOnRequest() throws {
let kinds: [RUMResourceType] = [.image, .xhr, .beacon, .css, .document, .fetch, .font, .js, .media, .other]
let kinds: [RUMResourceType] = [.image, .xhr, .beacon, .css, .document, .fetch, .font, .js, .media, .other, .native]
let kindBasedOnRequest = kinds.randomElement()!
let kindBasedOnResponse = kinds.randomElement()!

Expand Down
26 changes: 16 additions & 10 deletions Tests/DatadogTests/Datadog/RUMMonitorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class RUMMonitorTests: XCTestCase {
}
}

func testStartingView_thenLoadingXHRResourceWithRequestWithMetrics() throws {
func testStartingView_thenLoadingNativeResourceWithRequestWithMetrics() throws {
guard #available(iOS 13, *) else {
return // `URLSessionTaskMetrics` mocking doesn't work prior to iOS 13.0
}
Expand Down Expand Up @@ -149,14 +149,14 @@ class RUMMonitorTests: XCTestCase {

let session = try XCTUnwrap(try RUMSessionMatcher.groupMatchersBySessions(rumEventMatchers).first)
let resourceEvent = session.viewVisits[0].resourceEvents[0]
XCTAssertEqual(resourceEvent.resource.type, .xhr, "POST Resources should always have the `.xhr` kind")
XCTAssertEqual(resourceEvent.resource.type, .native, "POST Resources should always have the `.native` kind")
XCTAssertEqual(resourceEvent.resource.statusCode, 200)
XCTAssertEqual(resourceEvent.resource.duration, 4_000_000_000)
XCTAssertEqual(resourceEvent.resource.dns!.start, 1_000_000_000)
XCTAssertEqual(resourceEvent.resource.dns!.duration, 2_000_000_000)
}

func testStartingView_thenLoadingXHRResourceWithRequestWithExternalMetrics() throws {
func testStartingView_thenLoadingNativeResourceWithRequestWithExternalMetrics() throws {
RUMFeature.instance = .mockByRecordingRUMEventMatchers(directories: temporaryFeatureDirectories)
defer { RUMFeature.instance?.deinitialize() }

Expand Down Expand Up @@ -200,7 +200,7 @@ class RUMMonitorTests: XCTestCase {

let session = try XCTUnwrap(try RUMSessionMatcher.groupMatchersBySessions(rumEventMatchers).first)
let resourceEvent = session.viewVisits[0].resourceEvents[0]
XCTAssertEqual(resourceEvent.resource.type, .xhr, "POST Resources should always have the `.xhr` kind")
XCTAssertEqual(resourceEvent.resource.type, .native, "POST Resources should always have the `.native` kind")
XCTAssertEqual(resourceEvent.resource.statusCode, 200)

XCTAssertEqual(resourceEvent.resource.duration, 12_000_000_000)
Expand Down Expand Up @@ -1373,15 +1373,15 @@ class RUMResourceKindTests: XCTestCase {
}
}

func testWhenInitializedWithPOSTorPUTorDELETErequest_itReturnsXHR() {
func testWhenInitialized_itDefaultsToNative() {
XCTAssertEqual(
RUMResourceType(request: .mockWith(httpMethod: "POST".randomcased())), .xhr
RUMResourceType(request: .mockWith(httpMethod: "POST".randomcased())), .native
)
XCTAssertEqual(
RUMResourceType(request: .mockWith(httpMethod: "PUT".randomcased())), .xhr
RUMResourceType(request: .mockWith(httpMethod: "PUT".randomcased())), .native
)
XCTAssertEqual(
RUMResourceType(request: .mockWith(httpMethod: "DELETE".randomcased())), .xhr
RUMResourceType(request: .mockWith(httpMethod: "DELETE".randomcased())), .native
)
}

Expand All @@ -1397,9 +1397,15 @@ class RUMResourceKindTests: XCTestCase {
)
}

func testWhenInitializingFromHTTPURLResponse_itDefaultsToXhr() {
func testWhenInitializingFromHTTPURLResponseWithUnknownType_itDefaultsToNative() {
XCTAssertEqual(
RUMResourceType(response: .mockWith(mimeType: "unknown/type")), .xhr
RUMResourceType(response: .mockWith(mimeType: "unknown/type")), .native
)
}

func testWhenInitializingFromHTTPURLResponseWithNoType_itDefaultsToNative() {
XCTAssertEqual(
RUMResourceType(response: .mockWith(mimeType: nil)), .native
)
}
}
1 change: 1 addition & 0 deletions Tests/DatadogTests/DatadogObjc/DDRUMMonitorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class DDRUMResourceKindTests: XCTestCase {
XCTAssertEqual(DDRUMResourceType.js.swiftType, .js)
XCTAssertEqual(DDRUMResourceType.media.swiftType, .media)
XCTAssertEqual(DDRUMResourceType.other.swiftType, .other)
XCTAssertEqual(DDRUMResourceType.native.swiftType, .native)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ - (void)testDDRUMUserActionTypeAPI {
- (void)testDDRUMResourceTypeAPI {
DDRUMResourceTypeImage; DDRUMResourceTypeXhr; DDRUMResourceTypeBeacon; DDRUMResourceTypeCss; DDRUMResourceTypeDocument;
DDRUMResourceTypeFetch; DDRUMResourceTypeFont; DDRUMResourceTypeJs; DDRUMResourceTypeMedia; DDRUMResourceTypeOther;
DDRUMResourceTypeNative;
}

- (void)testDDRUMMethodAPI {
Expand Down

0 comments on commit 4481075

Please sign in to comment.