FIRHTTPMetricTests.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <XCTest/XCTest.h>
  15. #import "FirebasePerformance/Sources/Common/FPRConstants.h"
  16. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations+Private.h"
  17. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
  18. #import "FirebasePerformance/Sources/Configurations/FPRRemoteConfigFlags+Private.h"
  19. #import "FirebasePerformance/Sources/Configurations/FPRRemoteConfigFlags.h"
  20. #import "FirebasePerformance/Sources/FPRClient.h"
  21. #import "FirebasePerformance/Sources/Public/FirebasePerformance/FIRHTTPMetric.h"
  22. #import "FirebasePerformance/Sources/Public/FirebasePerformance/FIRPerformance.h"
  23. #import "FirebasePerformance/Sources/Instrumentation/FIRHTTPMetric+Private.h"
  24. #import "FirebasePerformance/Tests/Unit/Configurations/FPRFakeRemoteConfig.h"
  25. #import "FirebasePerformance/Tests/Unit/FPRTestCase.h"
  26. #import <OCMock/OCMock.h>
  27. @interface FIRHTTPMetricTests : FPRTestCase
  28. @property(nonatomic, strong) NSURL *sampleURL;
  29. @end
  30. @implementation FIRHTTPMetricTests
  31. - (void)tearDown {
  32. [super tearDown];
  33. FIRPerformance *performance = [FIRPerformance sharedInstance];
  34. [performance setDataCollectionEnabled:NO];
  35. [performance setInstrumentationEnabled:NO];
  36. }
  37. - (void)setUp {
  38. [super setUp];
  39. self.sampleURL = [NSURL URLWithString:@"https://a1b2c3d4.com"];
  40. FIRPerformance *performance = [FIRPerformance sharedInstance];
  41. [performance setDataCollectionEnabled:YES];
  42. [performance setInstrumentationEnabled:NO];
  43. }
  44. #pragma mark - HTTP Metric creation tests.
  45. /** Validates instance creation. */
  46. - (void)testInstanceCreation {
  47. XCTAssertNotNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodGET]);
  48. XCTAssertNotNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodPUT]);
  49. XCTAssertNotNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodPOST]);
  50. XCTAssertNotNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  51. HTTPMethod:FIRHTTPMethodCONNECT]);
  52. XCTAssertNotNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  53. HTTPMethod:FIRHTTPMethodOPTIONS]);
  54. XCTAssertNotNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodHEAD]);
  55. XCTAssertNotNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  56. HTTPMethod:FIRHTTPMethodDELETE]);
  57. XCTAssertNotNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodPATCH]);
  58. XCTAssertNotNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodTRACE]);
  59. }
  60. /** Validates instance creation when data collection is disabled. */
  61. - (void)testInstanceCreationWhenDataCollectionDisabled {
  62. FIRPerformance *performance = [FIRPerformance sharedInstance];
  63. [performance setDataCollectionEnabled:NO];
  64. XCTAssertNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodGET]);
  65. XCTAssertNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodPUT]);
  66. XCTAssertNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodPOST]);
  67. XCTAssertNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodCONNECT]);
  68. XCTAssertNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodOPTIONS]);
  69. XCTAssertNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodHEAD]);
  70. XCTAssertNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodDELETE]);
  71. XCTAssertNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodPATCH]);
  72. XCTAssertNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodTRACE]);
  73. }
  74. /** Validates if HTTPMetric creation fails when SDK flag is disabled in remote config. */
  75. - (void)testMetricCreationWhenSDKFlagDisabled {
  76. FPRConfigurations *configurations = [FPRConfigurations sharedInstance];
  77. FPRFakeRemoteConfig *remoteConfig = [[FPRFakeRemoteConfig alloc] init];
  78. FPRRemoteConfigFlags *configFlags =
  79. [[FPRRemoteConfigFlags alloc] initWithRemoteConfig:(FIRRemoteConfig *)remoteConfig];
  80. configFlags.appStartConfigFetchDelayInSeconds = 0.0;
  81. configurations.remoteConfigFlags = configFlags;
  82. NSData *valueData = [@"false" dataUsingEncoding:NSUTF8StringEncoding];
  83. FIRRemoteConfigValue *value =
  84. [[FIRRemoteConfigValue alloc] initWithData:valueData source:FIRRemoteConfigSourceRemote];
  85. [remoteConfig.configValues setObject:value forKey:@"fpr_enabled"];
  86. // Trigger the RC config fetch
  87. remoteConfig.lastFetchTime = nil;
  88. configFlags.appStartConfigFetchDelayInSeconds = 0.0;
  89. [configFlags update];
  90. XCTAssertNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodGET]);
  91. }
  92. /** Validates if HTTPMetric creation succeeds when SDK flag is enabled in remote config. */
  93. - (void)testMetricCreationWhenSDKFlagEnabled {
  94. FPRConfigurations *configurations = [FPRConfigurations sharedInstance];
  95. FPRFakeRemoteConfig *remoteConfig = [[FPRFakeRemoteConfig alloc] init];
  96. FPRRemoteConfigFlags *configFlags =
  97. [[FPRRemoteConfigFlags alloc] initWithRemoteConfig:(FIRRemoteConfig *)remoteConfig];
  98. configurations.remoteConfigFlags = configFlags;
  99. NSUserDefaults *userDefaults = [[NSUserDefaults alloc] init];
  100. configFlags.userDefaults = userDefaults;
  101. NSString *configKey = [NSString stringWithFormat:@"%@.%@", kFPRConfigPrefix, @"fpr_enabled"];
  102. [userDefaults setObject:@(TRUE) forKey:configKey];
  103. XCTAssertNotNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodGET]);
  104. }
  105. /** Validates if HTTPMetric creation fails when SDK flag is enabled in remote config, but data
  106. * collection disabled. */
  107. - (void)testMetricCreationWhenSDKFlagEnabledWithDataCollectionDisabled {
  108. [[FIRPerformance sharedInstance] setDataCollectionEnabled:NO];
  109. FPRConfigurations *configurations = [FPRConfigurations sharedInstance];
  110. FPRFakeRemoteConfig *remoteConfig = [[FPRFakeRemoteConfig alloc] init];
  111. FPRRemoteConfigFlags *configFlags =
  112. [[FPRRemoteConfigFlags alloc] initWithRemoteConfig:(FIRRemoteConfig *)remoteConfig];
  113. configurations.remoteConfigFlags = configFlags;
  114. NSData *valueData = [@"true" dataUsingEncoding:NSUTF8StringEncoding];
  115. FIRRemoteConfigValue *value =
  116. [[FIRRemoteConfigValue alloc] initWithData:valueData source:FIRRemoteConfigSourceRemote];
  117. [remoteConfig.configValues setObject:value forKey:@"fpr_enabled"];
  118. XCTAssertNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodGET]);
  119. }
  120. /** Validates that metric creation fails for invalid inputs. */
  121. - (void)testInstanceCreationForInvalidInputs {
  122. XCTAssertNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:-1]);
  123. XCTAssertNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:100]);
  124. }
  125. /** Validate if the metric creation succeeds with right values. */
  126. - (void)testMetricCreationSucceeds {
  127. id mock = [OCMockObject partialMockForObject:[FPRClient sharedInstance]];
  128. OCMStub([mock logNetworkTrace:[OCMArg any]]).andDo(nil);
  129. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  130. HTTPMethod:FIRHTTPMethodGET];
  131. [metric start];
  132. [metric markRequestComplete];
  133. metric.responsePayloadSize = 300;
  134. metric.requestPayloadSize = 100;
  135. metric.responseCode = 200;
  136. metric.responseContentType = @"text/json";
  137. [metric markResponseStart];
  138. [metric stop];
  139. FPRNetworkTrace *networkTrace = metric.networkTrace;
  140. XCTAssertEqualObjects(networkTrace.URLRequest.URL, self.sampleURL);
  141. XCTAssertEqual(networkTrace.requestSize, 100);
  142. XCTAssertEqual(networkTrace.responseSize, 300);
  143. XCTAssertEqual(networkTrace.responseCode, 200);
  144. XCTAssertEqualObjects(networkTrace.responseContentType, @"text/json");
  145. }
  146. /** Validate if the network trace is invalid if the response code is not set. */
  147. - (void)testMetricCreationFailsWhenResponseCodeNotSet {
  148. id mock = [OCMockObject partialMockForObject:[FPRClient sharedInstance]];
  149. OCMStub([mock logNetworkTrace:[OCMArg any]]).andDo(nil);
  150. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  151. HTTPMethod:FIRHTTPMethodGET];
  152. [metric start];
  153. [metric markRequestComplete];
  154. metric.responsePayloadSize = 0;
  155. metric.responseContentType = @"text/json";
  156. [metric markResponseStart];
  157. [metric stop];
  158. FPRNetworkTrace *networkTrace = metric.networkTrace;
  159. XCTAssertFalse([networkTrace isValid]);
  160. }
  161. /** Validates that starting and stopping logs an event. */
  162. - (void)testValidHTTPMetricBeingSent {
  163. id mock = [OCMockObject partialMockForObject:[FPRClient sharedInstance]];
  164. OCMStub([mock logNetworkTrace:[OCMArg any]]).andDo(nil);
  165. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  166. HTTPMethod:FIRHTTPMethodGET];
  167. [metric start];
  168. [metric markRequestComplete];
  169. metric.responseCode = 200;
  170. metric.requestPayloadSize = 200;
  171. metric.responsePayloadSize = 0;
  172. metric.responseContentType = @"text/json";
  173. [metric markResponseStart];
  174. [metric stop];
  175. OCMVerify([mock logNetworkTrace:[OCMArg any]]);
  176. }
  177. /** Validates that calling just stop does not log an event. */
  178. - (void)testStartMustBeCalledForLoggingEvent {
  179. id mock = [OCMockObject partialMockForObject:[FPRClient sharedInstance]];
  180. OCMStub([mock logNetworkTrace:[OCMArg any]]).andDo(nil);
  181. [[mock reject] logNetworkTrace:[OCMArg any]];
  182. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  183. HTTPMethod:FIRHTTPMethodGET];
  184. metric.responseCode = 200;
  185. metric.requestPayloadSize = 200;
  186. metric.responsePayloadSize = 0;
  187. metric.responseContentType = @"text/json";
  188. [metric stop];
  189. }
  190. /** Validates that calling stop twice does not log the event again. */
  191. - (void)testSameEventNotGettingLoggedTwice {
  192. id mock = [OCMockObject partialMockForObject:[FPRClient sharedInstance]];
  193. OCMStub([mock logNetworkTrace:[OCMArg any]]).andDo(nil);
  194. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  195. HTTPMethod:FIRHTTPMethodGET];
  196. [metric start];
  197. metric.responseCode = 200;
  198. metric.requestPayloadSize = 200;
  199. metric.responsePayloadSize = 0;
  200. metric.responseContentType = @"text/json";
  201. [metric stop];
  202. OCMVerify([mock logNetworkTrace:[OCMArg any]]);
  203. [[mock reject] logNetworkTrace:[OCMArg any]];
  204. [metric stop];
  205. }
  206. #pragma mark - Custom attribute related testing
  207. /** Validates if setting a valid attribute before calling start works. */
  208. - (void)testSettingValidAttributeBeforeStart {
  209. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  210. HTTPMethod:FIRHTTPMethodGET];
  211. [metric setValue:@"bar" forAttribute:@"foo"];
  212. XCTAssertEqual([metric valueForAttribute:@"foo"], @"bar");
  213. }
  214. /** Validates if setting a valid attribute works between start/stop works. */
  215. - (void)testSettingValidAttributeBetweenStartAndStop {
  216. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  217. HTTPMethod:FIRHTTPMethodGET];
  218. [metric start];
  219. [metric setValue:@"bar" forAttribute:@"foo"];
  220. XCTAssertEqual([metric valueForAttribute:@"foo"], @"bar");
  221. [metric stop];
  222. }
  223. /** Validates if setting a valid attribute works after stop is a no-op. */
  224. - (void)testSettingValidAttributeBetweenAfterStop {
  225. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  226. HTTPMethod:FIRHTTPMethodGET];
  227. [metric start];
  228. metric.responseCode = 200;
  229. [metric stop];
  230. [metric setValue:@"bar" forAttribute:@"foo"];
  231. XCTAssertNil([metric valueForAttribute:@"foo"]);
  232. }
  233. /** Validates if attributes property access works. */
  234. - (void)testReadingAttributesFromProperty {
  235. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  236. HTTPMethod:FIRHTTPMethodGET];
  237. XCTAssertNotNil(metric.attributes);
  238. XCTAssertEqual(metric.attributes.count, 0);
  239. [metric setValue:@"bar" forAttribute:@"foo"];
  240. NSDictionary<NSString *, NSString *> *attributes = metric.attributes;
  241. XCTAssertEqual(attributes.allKeys.count, 1);
  242. }
  243. /** Validates if attributes property is immutable. */
  244. - (void)testImmutablityOfAttributesProperty {
  245. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  246. HTTPMethod:FIRHTTPMethodGET];
  247. [metric setValue:@"bar" forAttribute:@"foo"];
  248. NSMutableDictionary<NSString *, NSString *> *attributes =
  249. (NSMutableDictionary<NSString *, NSString *> *)metric.attributes;
  250. XCTAssertThrows([attributes setValue:@"bar1" forKey:@"foo"]);
  251. }
  252. /** Validates if updating attribute value works. */
  253. - (void)testUpdatingAttributeValue {
  254. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  255. HTTPMethod:FIRHTTPMethodGET];
  256. [metric setValue:@"bar" forAttribute:@"foo"];
  257. [metric setValue:@"baz" forAttribute:@"foo"];
  258. XCTAssertEqual(@"baz", [metric valueForAttribute:@"foo"]);
  259. [metric setValue:@"qux" forAttribute:@"foo"];
  260. XCTAssertEqual(@"qux", [metric valueForAttribute:@"foo"]);
  261. }
  262. /** Validates if removing attributes work before call to start. */
  263. - (void)testRemovingAttributeBeforeStart {
  264. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  265. HTTPMethod:FIRHTTPMethodGET];
  266. [metric setValue:@"bar" forAttribute:@"foo"];
  267. [metric removeAttribute:@"foo"];
  268. XCTAssertNil([metric valueForAttribute:@"foo"]);
  269. [metric removeAttribute:@"foo"];
  270. XCTAssertNil([metric valueForAttribute:@"foo"]);
  271. }
  272. /** Validates if removing attributes work between start and stop calls. */
  273. - (void)testRemovingAttributeBetweenStartStop {
  274. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  275. HTTPMethod:FIRHTTPMethodGET];
  276. [metric setValue:@"bar" forAttribute:@"foo"];
  277. [metric start];
  278. [metric removeAttribute:@"foo"];
  279. [metric stop];
  280. XCTAssertNil([metric valueForAttribute:@"foo"]);
  281. }
  282. /** Validates if removing attributes is a no-op after stop. */
  283. - (void)testRemovingAttributeBetweenAfterStop {
  284. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  285. HTTPMethod:FIRHTTPMethodGET];
  286. [metric setValue:@"bar" forAttribute:@"foo"];
  287. [metric start];
  288. metric.responseCode = 200;
  289. [metric stop];
  290. [metric removeAttribute:@"foo"];
  291. XCTAssertEqual([metric valueForAttribute:@"foo"], @"bar");
  292. }
  293. /** Validates if removing non-existing attributes works. */
  294. - (void)testRemovingNonExistingAttribute {
  295. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  296. HTTPMethod:FIRHTTPMethodGET];
  297. [metric removeAttribute:@"foo"];
  298. XCTAssertNil([metric valueForAttribute:@"foo"]);
  299. [metric removeAttribute:@"foo"];
  300. XCTAssertNil([metric valueForAttribute:@"foo"]);
  301. }
  302. /** Validates if using reserved prefix in attribute prefix will drop the attribute. */
  303. - (void)testAttributeNamePrefixSrtipped {
  304. NSArray<NSString *> *reservedPrefix = @[ @"firebase_", @"google_", @"ga_" ];
  305. [reservedPrefix enumerateObjectsUsingBlock:^(NSString *prefix, NSUInteger idx, BOOL *stop) {
  306. NSString *attributeName = [NSString stringWithFormat:@"%@name", prefix];
  307. NSString *attributeValue = @"value";
  308. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  309. HTTPMethod:FIRHTTPMethodGET];
  310. [metric setValue:attributeValue forAttribute:attributeName];
  311. XCTAssertNil([metric valueForAttribute:attributeName]);
  312. }];
  313. }
  314. /** Validates if long attribute names gets dropped. */
  315. - (void)testMaxLengthForAttributeName {
  316. NSString *testName = [@"abc" stringByPaddingToLength:kFPRMaxAttributeNameLength + 1
  317. withString:@"-"
  318. startingAtIndex:0];
  319. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  320. HTTPMethod:FIRHTTPMethodGET];
  321. [metric setValue:@"bar" forAttribute:testName];
  322. XCTAssertNil([metric valueForAttribute:testName]);
  323. }
  324. /** Validates if attribute names with illegal characters gets dropped. */
  325. - (void)testIllegalCharactersInAttributeName {
  326. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  327. HTTPMethod:FIRHTTPMethodGET];
  328. [metric setValue:@"bar" forAttribute:@"foo_"];
  329. XCTAssertEqual([metric valueForAttribute:@"foo_"], @"bar");
  330. [metric setValue:@"bar" forAttribute:@"foo_$"];
  331. XCTAssertNil([metric valueForAttribute:@"foo_$"]);
  332. [metric setValue:@"bar" forAttribute:@"FOO_$"];
  333. XCTAssertNil([metric valueForAttribute:@"FOO_$"]);
  334. [metric setValue:@"bar" forAttribute:@"FOO_"];
  335. XCTAssertEqual([metric valueForAttribute:@"FOO_"], @"bar");
  336. }
  337. /** Validates if long attribute values gets truncated. */
  338. - (void)testMaxLengthForAttributeValue {
  339. NSString *testValue = [@"abc" stringByPaddingToLength:kFPRMaxAttributeValueLength + 1
  340. withString:@"-"
  341. startingAtIndex:0];
  342. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  343. HTTPMethod:FIRHTTPMethodGET];
  344. [metric setValue:testValue forAttribute:@"foo"];
  345. XCTAssertNil([metric valueForAttribute:@"foo"]);
  346. }
  347. /** Validates if empty name or value of the attributes are getting dropped. */
  348. - (void)testAttributesWithEmptyValues {
  349. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  350. HTTPMethod:FIRHTTPMethodGET];
  351. [metric setValue:@"" forAttribute:@"foo"];
  352. XCTAssertNil([metric valueForAttribute:@"foo"]);
  353. [metric setValue:@"bar" forAttribute:@""];
  354. XCTAssertNil([metric valueForAttribute:@""]);
  355. }
  356. /** Validates if the limit the maximum number of attributes work. */
  357. - (void)testMaximumNumberOfAttributes {
  358. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  359. HTTPMethod:FIRHTTPMethodGET];
  360. for (int i = 0; i < kFPRMaxGlobalCustomAttributesCount; i++) {
  361. NSString *attributeName = [NSString stringWithFormat:@"dim%d", i];
  362. [metric setValue:@"bar" forAttribute:attributeName];
  363. XCTAssertEqual([metric valueForAttribute:attributeName], @"bar");
  364. }
  365. [metric setValue:@"bar" forAttribute:@"foo"];
  366. XCTAssertNil([metric valueForAttribute:@"foo"]);
  367. }
  368. /** Validates if removing old attributes and adding new attributes work. */
  369. - (void)testRemovingAndAddingAttributes {
  370. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  371. HTTPMethod:FIRHTTPMethodGET];
  372. for (int i = 0; i < kFPRMaxGlobalCustomAttributesCount; i++) {
  373. NSString *attributeName = [NSString stringWithFormat:@"dim%d", i];
  374. [metric setValue:@"bar" forAttribute:attributeName];
  375. XCTAssertEqual([metric valueForAttribute:attributeName], @"bar");
  376. }
  377. [metric removeAttribute:@"dim1"];
  378. [metric setValue:@"bar" forAttribute:@"foo"];
  379. XCTAssertEqual([metric valueForAttribute:@"foo"], @"bar");
  380. }
  381. @end