-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathRUMFeatureTests.swift
141 lines (126 loc) · 6.28 KB
/
RUMFeatureTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* 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
class RUMFeatureTests: XCTestCase {
override func setUp() {
super.setUp()
XCTAssertNil(Datadog.instance)
XCTAssertNil(RUMFeature.instance)
temporaryFeatureDirectories.create()
}
override func tearDown() {
XCTAssertNil(Datadog.instance)
XCTAssertNil(RUMFeature.instance)
temporaryFeatureDirectories.delete()
super.tearDown()
}
// MARK: - HTTP Message
func testItUsesExpectedHTTPMessage() throws {
let randomApplicationName: String = .mockRandom(among: .alphanumerics)
let randomApplicationVersion: String = .mockRandom(among: .decimalDigits)
let randomServiceName: String = .mockRandom(among: .alphanumerics)
let randomEnvironmentName: String = .mockRandom(among: .alphanumerics)
let randomSource: String = .mockRandom(among: .alphanumerics)
let randomOrigin: String = .mockRandom(among: .alphanumerics)
let randomSDKVersion: String = .mockRandom(among: .alphanumerics)
let randomUploadURL: URL = .mockRandom()
let randomClientToken: String = .mockRandom()
let randomDeviceModel: String = .mockRandom()
let randomDeviceOSName: String = .mockRandom()
let randomDeviceOSVersion: String = .mockRandom()
let server = ServerMock(delivery: .success(response: .mockResponseWith(statusCode: 200)))
// Given
RUMFeature.instance = .mockWith(
directories: temporaryFeatureDirectories,
configuration: .mockWith(
common: .mockWith(
applicationName: randomApplicationName,
applicationVersion: randomApplicationVersion,
serviceName: randomServiceName,
environment: randomEnvironmentName,
source: randomSource,
origin: randomOrigin,
sdkVersion: randomSDKVersion
),
uploadURL: randomUploadURL,
clientToken: randomClientToken
),
dependencies: .mockWith(
mobileDevice: .mockWith(model: randomDeviceModel, osName: randomDeviceOSName, osVersion: randomDeviceOSVersion)
)
)
defer { RUMFeature.instance?.deinitialize() }
// When
let monitor = RUMMonitor.initialize()
monitor.startView(viewController: mockView) // on starting the first view we sends `application_start` action event
// Then
let request = server.waitAndReturnRequests(count: 1)[0]
let requestURL = try XCTUnwrap(request.url)
XCTAssertEqual(request.httpMethod, "POST")
XCTAssertTrue(requestURL.absoluteString.starts(with: randomUploadURL.absoluteString + "?"))
XCTAssertEqual(
requestURL.query,
"""
ddsource=\(randomSource)&ddtags=service:\(randomServiceName),version:\(randomApplicationVersion),sdk_version:\(randomSDKVersion),env:\(randomEnvironmentName)
"""
)
XCTAssertEqual(
request.allHTTPHeaderFields?["User-Agent"],
"""
\(randomApplicationName)/\(randomApplicationVersion) CFNetwork (\(randomDeviceModel); \(randomDeviceOSName)/\(randomDeviceOSVersion))
"""
)
XCTAssertEqual(request.allHTTPHeaderFields?["Content-Type"], "text/plain;charset=UTF-8")
XCTAssertEqual(request.allHTTPHeaderFields?["Content-Encoding"], "deflate")
XCTAssertEqual(request.allHTTPHeaderFields?["DD-API-KEY"], randomClientToken)
XCTAssertEqual(request.allHTTPHeaderFields?["DD-EVP-ORIGIN"], randomOrigin)
XCTAssertEqual(request.allHTTPHeaderFields?["DD-EVP-ORIGIN-VERSION"], randomSDKVersion)
XCTAssertEqual(request.allHTTPHeaderFields?["DD-REQUEST-ID"]?.matches(regex: .uuidRegex), true)
}
// MARK: - HTTP Payload
func testItUsesExpectedPayloadFormatForUploads() throws {
let server = ServerMock(delivery: .success(response: .mockResponseWith(statusCode: 200)))
RUMFeature.instance = .mockWith(
directories: temporaryFeatureDirectories,
dependencies: .mockWith(
performance: .combining(
storagePerformance: StoragePerformanceMock(
maxFileSize: .max,
maxDirectorySize: .max,
maxFileAgeForWrite: .distantFuture, // write all spans to single file,
minFileAgeForRead: StoragePerformanceMock.readAllFiles.minFileAgeForRead,
maxFileAgeForRead: StoragePerformanceMock.readAllFiles.maxFileAgeForRead,
maxObjectsInFile: 3, // write 3 spans to payload,
maxObjectSize: .max
),
uploadPerformance: UploadPerformanceMock(
initialUploadDelay: 0.5, // wait enough until spans are written,
minUploadDelay: 1,
maxUploadDelay: 1,
uploadDelayChangeRate: 0
)
)
)
)
defer { RUMFeature.instance?.deinitialize() }
let fileWriter = try XCTUnwrap(RUMFeature.instance?.storage.writer)
fileWriter.write(value: RUMDataModelMock(attribute: "1st event"))
fileWriter.write(value: RUMDataModelMock(attribute: "2nd event"))
fileWriter.write(value: RUMDataModelMock(attribute: "3rd event"))
let payload = try XCTUnwrap(server.waitAndReturnRequests(count: 1)[0].httpBody)
// Expected payload format:
// ```
// event1JSON
// event2JSON
// event3JSON
// ```
let eventMatchers = try RUMEventMatcher.fromNewlineSeparatedJSONObjectsData(payload)
XCTAssertEqual((try eventMatchers[0].model() as RUMDataModelMock).attribute, "1st event")
XCTAssertEqual((try eventMatchers[1].model() as RUMDataModelMock).attribute, "2nd event")
XCTAssertEqual((try eventMatchers[2].model() as RUMDataModelMock).attribute, "3rd event")
}
}