FIRHTTPMetricTests.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. configurations.remoteConfigFlags = configFlags;
  81. NSData *valueData = [@"false" dataUsingEncoding:NSUTF8StringEncoding];
  82. FIRRemoteConfigValue *value =
  83. [[FIRRemoteConfigValue alloc] initWithData:valueData source:FIRRemoteConfigSourceRemote];
  84. [remoteConfig.configValues setObject:value forKey:@"fpr_enabled"];
  85. // Trigger the RC config fetch
  86. remoteConfig.lastFetchTime = nil;
  87. [configFlags update];
  88. XCTAssertNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodGET]);
  89. }
  90. /** Validates if HTTPMetric creation succeeds when SDK flag is enabled in remote config. */
  91. - (void)testMetricCreationWhenSDKFlagEnabled {
  92. FPRConfigurations *configurations = [FPRConfigurations sharedInstance];
  93. FPRFakeRemoteConfig *remoteConfig = [[FPRFakeRemoteConfig alloc] init];
  94. FPRRemoteConfigFlags *configFlags =
  95. [[FPRRemoteConfigFlags alloc] initWithRemoteConfig:(FIRRemoteConfig *)remoteConfig];
  96. configurations.remoteConfigFlags = configFlags;
  97. NSUserDefaults *userDefaults = [[NSUserDefaults alloc] init];
  98. configFlags.userDefaults = userDefaults;
  99. NSString *configKey = [NSString stringWithFormat:@"%@.%@", kFPRConfigPrefix, @"fpr_enabled"];
  100. [userDefaults setObject:@(TRUE) forKey:configKey];
  101. XCTAssertNotNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodGET]);
  102. }
  103. /** Validates if HTTPMetric creation fails when SDK flag is enabled in remote config, but data
  104. * collection disabled. */
  105. - (void)testMetricCreationWhenSDKFlagEnabledWithDataCollectionDisabled {
  106. [[FIRPerformance sharedInstance] setDataCollectionEnabled:NO];
  107. FPRConfigurations *configurations = [FPRConfigurations sharedInstance];
  108. FPRFakeRemoteConfig *remoteConfig = [[FPRFakeRemoteConfig alloc] init];
  109. FPRRemoteConfigFlags *configFlags =
  110. [[FPRRemoteConfigFlags alloc] initWithRemoteConfig:(FIRRemoteConfig *)remoteConfig];
  111. configurations.remoteConfigFlags = configFlags;
  112. NSData *valueData = [@"true" dataUsingEncoding:NSUTF8StringEncoding];
  113. FIRRemoteConfigValue *value =
  114. [[FIRRemoteConfigValue alloc] initWithData:valueData source:FIRRemoteConfigSourceRemote];
  115. [remoteConfig.configValues setObject:value forKey:@"fpr_enabled"];
  116. XCTAssertNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:FIRHTTPMethodGET]);
  117. }
  118. /** Validates that metric creation fails for invalid inputs. */
  119. - (void)testInstanceCreationForInvalidInputs {
  120. XCTAssertNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:-1]);
  121. XCTAssertNil([[FIRHTTPMetric alloc] initWithURL:self.sampleURL HTTPMethod:100]);
  122. }
  123. /** Validate if the metric creation succeeds with right values. */
  124. - (void)testMetricCreationSucceeds {
  125. id mock = [OCMockObject partialMockForObject:[FPRClient sharedInstance]];
  126. OCMStub([mock logNetworkTrace:[OCMArg any]]).andDo(nil);
  127. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  128. HTTPMethod:FIRHTTPMethodGET];
  129. [metric start];
  130. [metric markRequestComplete];
  131. metric.responsePayloadSize = 300;
  132. metric.requestPayloadSize = 100;
  133. metric.responseCode = 200;
  134. metric.responseContentType = @"text/json";
  135. [metric markResponseStart];
  136. [metric stop];
  137. FPRNetworkTrace *networkTrace = metric.networkTrace;
  138. XCTAssertEqualObjects(networkTrace.URLRequest.URL, self.sampleURL);
  139. XCTAssertEqual(networkTrace.requestSize, 100);
  140. XCTAssertEqual(networkTrace.responseSize, 300);
  141. XCTAssertEqual(networkTrace.responseCode, 200);
  142. XCTAssertEqualObjects(networkTrace.responseContentType, @"text/json");
  143. }
  144. /** Validate if the network trace is invalid if the response code is not set. */
  145. - (void)testMetricCreationFailsWhenResponseCodeNotSet {
  146. id mock = [OCMockObject partialMockForObject:[FPRClient sharedInstance]];
  147. OCMStub([mock logNetworkTrace:[OCMArg any]]).andDo(nil);
  148. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  149. HTTPMethod:FIRHTTPMethodGET];
  150. [metric start];
  151. [metric markRequestComplete];
  152. metric.responsePayloadSize = 0;
  153. metric.responseContentType = @"text/json";
  154. [metric markResponseStart];
  155. [metric stop];
  156. FPRNetworkTrace *networkTrace = metric.networkTrace;
  157. XCTAssertFalse([networkTrace isValid]);
  158. }
  159. /** Validates that starting and stopping logs an event. */
  160. - (void)testValidHTTPMetricBeingSent {
  161. id mock = [OCMockObject partialMockForObject:[FPRClient sharedInstance]];
  162. OCMStub([mock logNetworkTrace:[OCMArg any]]).andDo(nil);
  163. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  164. HTTPMethod:FIRHTTPMethodGET];
  165. [metric start];
  166. [metric markRequestComplete];
  167. metric.responseCode = 200;
  168. metric.requestPayloadSize = 200;
  169. metric.responsePayloadSize = 0;
  170. metric.responseContentType = @"text/json";
  171. [metric markResponseStart];
  172. [metric stop];
  173. OCMVerify([mock logNetworkTrace:[OCMArg any]]);
  174. }
  175. /** Validates that calling just stop does not log an event. */
  176. - (void)testStartMustBeCalledForLoggingEvent {
  177. id mock = [OCMockObject partialMockForObject:[FPRClient sharedInstance]];
  178. OCMStub([mock logNetworkTrace:[OCMArg any]]).andDo(nil);
  179. [[mock reject] logNetworkTrace:[OCMArg any]];
  180. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  181. HTTPMethod:FIRHTTPMethodGET];
  182. metric.responseCode = 200;
  183. metric.requestPayloadSize = 200;
  184. metric.responsePayloadSize = 0;
  185. metric.responseContentType = @"text/json";
  186. [metric stop];
  187. }
  188. /** Validates that calling stop twice does not log the event again. */
  189. - (void)testSameEventNotGettingLoggedTwice {
  190. id mock = [OCMockObject partialMockForObject:[FPRClient sharedInstance]];
  191. OCMStub([mock logNetworkTrace:[OCMArg any]]).andDo(nil);
  192. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  193. HTTPMethod:FIRHTTPMethodGET];
  194. [metric start];
  195. metric.responseCode = 200;
  196. metric.requestPayloadSize = 200;
  197. metric.responsePayloadSize = 0;
  198. metric.responseContentType = @"text/json";
  199. [metric stop];
  200. OCMVerify([mock logNetworkTrace:[OCMArg any]]);
  201. [[mock reject] logNetworkTrace:[OCMArg any]];
  202. [metric stop];
  203. }
  204. #pragma mark - Custom attribute related testing
  205. /** Validates if setting a valid attribute before calling start works. */
  206. - (void)testSettingValidAttributeBeforeStart {
  207. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  208. HTTPMethod:FIRHTTPMethodGET];
  209. [metric setValue:@"bar" forAttribute:@"foo"];
  210. XCTAssertEqual([metric valueForAttribute:@"foo"], @"bar");
  211. }
  212. /** Validates if setting a valid attribute works between start/stop works. */
  213. - (void)testSettingValidAttributeBetweenStartAndStop {
  214. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  215. HTTPMethod:FIRHTTPMethodGET];
  216. [metric start];
  217. [metric setValue:@"bar" forAttribute:@"foo"];
  218. XCTAssertEqual([metric valueForAttribute:@"foo"], @"bar");
  219. [metric stop];
  220. }
  221. /** Validates if setting a valid attribute works after stop is a no-op. */
  222. - (void)testSettingValidAttributeBetweenAfterStop {
  223. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  224. HTTPMethod:FIRHTTPMethodGET];
  225. [metric start];
  226. metric.responseCode = 200;
  227. [metric stop];
  228. [metric setValue:@"bar" forAttribute:@"foo"];
  229. XCTAssertNil([metric valueForAttribute:@"foo"]);
  230. }
  231. /** Validates if attributes property access works. */
  232. - (void)testReadingAttributesFromProperty {
  233. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  234. HTTPMethod:FIRHTTPMethodGET];
  235. XCTAssertNotNil(metric.attributes);
  236. XCTAssertEqual(metric.attributes.count, 0);
  237. [metric setValue:@"bar" forAttribute:@"foo"];
  238. NSDictionary<NSString *, NSString *> *attributes = metric.attributes;
  239. XCTAssertEqual(attributes.allKeys.count, 1);
  240. }
  241. /** Validates if attributes property is immutable. */
  242. - (void)testImmutablityOfAttributesProperty {
  243. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  244. HTTPMethod:FIRHTTPMethodGET];
  245. [metric setValue:@"bar" forAttribute:@"foo"];
  246. NSMutableDictionary<NSString *, NSString *> *attributes =
  247. (NSMutableDictionary<NSString *, NSString *> *)metric.attributes;
  248. XCTAssertThrows([attributes setValue:@"bar1" forKey:@"foo"]);
  249. }
  250. /** Validates if updating attribute value works. */
  251. - (void)testUpdatingAttributeValue {
  252. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  253. HTTPMethod:FIRHTTPMethodGET];
  254. [metric setValue:@"bar" forAttribute:@"foo"];
  255. [metric setValue:@"baz" forAttribute:@"foo"];
  256. XCTAssertEqual(@"baz", [metric valueForAttribute:@"foo"]);
  257. [metric setValue:@"qux" forAttribute:@"foo"];
  258. XCTAssertEqual(@"qux", [metric valueForAttribute:@"foo"]);
  259. }
  260. /** Validates if removing attributes work before call to start. */
  261. - (void)testRemovingAttributeBeforeStart {
  262. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  263. HTTPMethod:FIRHTTPMethodGET];
  264. [metric setValue:@"bar" forAttribute:@"foo"];
  265. [metric removeAttribute:@"foo"];
  266. XCTAssertNil([metric valueForAttribute:@"foo"]);
  267. [metric removeAttribute:@"foo"];
  268. XCTAssertNil([metric valueForAttribute:@"foo"]);
  269. }
  270. /** Validates if removing attributes work between start and stop calls. */
  271. - (void)testRemovingAttributeBetweenStartStop {
  272. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  273. HTTPMethod:FIRHTTPMethodGET];
  274. [metric setValue:@"bar" forAttribute:@"foo"];
  275. [metric start];
  276. [metric removeAttribute:@"foo"];
  277. [metric stop];
  278. XCTAssertNil([metric valueForAttribute:@"foo"]);
  279. }
  280. /** Validates if removing attributes is a no-op after stop. */
  281. - (void)testRemovingAttributeBetweenAfterStop {
  282. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  283. HTTPMethod:FIRHTTPMethodGET];
  284. [metric setValue:@"bar" forAttribute:@"foo"];
  285. [metric start];
  286. metric.responseCode = 200;
  287. [metric stop];
  288. [metric removeAttribute:@"foo"];
  289. XCTAssertEqual([metric valueForAttribute:@"foo"], @"bar");
  290. }
  291. /** Validates if removing non-existing attributes works. */
  292. - (void)testRemovingNonExistingAttribute {
  293. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  294. HTTPMethod:FIRHTTPMethodGET];
  295. [metric removeAttribute:@"foo"];
  296. XCTAssertNil([metric valueForAttribute:@"foo"]);
  297. [metric removeAttribute:@"foo"];
  298. XCTAssertNil([metric valueForAttribute:@"foo"]);
  299. }
  300. /** Validates if using reserved prefix in attribute prefix will drop the attribute. */
  301. - (void)testAttributeNamePrefixSrtipped {
  302. NSArray<NSString *> *reservedPrefix = @[ @"firebase_", @"google_", @"ga_" ];
  303. [reservedPrefix enumerateObjectsUsingBlock:^(NSString *prefix, NSUInteger idx, BOOL *stop) {
  304. NSString *attributeName = [NSString stringWithFormat:@"%@name", prefix];
  305. NSString *attributeValue = @"value";
  306. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  307. HTTPMethod:FIRHTTPMethodGET];
  308. [metric setValue:attributeValue forAttribute:attributeName];
  309. XCTAssertNil([metric valueForAttribute:attributeName]);
  310. }];
  311. }
  312. /** Validates if long attribute names gets dropped. */
  313. - (void)testMaxLengthForAttributeName {
  314. NSString *testName = [@"abc" stringByPaddingToLength:kFPRMaxAttributeNameLength + 1
  315. withString:@"-"
  316. startingAtIndex:0];
  317. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  318. HTTPMethod:FIRHTTPMethodGET];
  319. [metric setValue:@"bar" forAttribute:testName];
  320. XCTAssertNil([metric valueForAttribute:testName]);
  321. }
  322. /** Validates if attribute names with illegal characters gets dropped. */
  323. - (void)testIllegalCharactersInAttributeName {
  324. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  325. HTTPMethod:FIRHTTPMethodGET];
  326. [metric setValue:@"bar" forAttribute:@"foo_"];
  327. XCTAssertEqual([metric valueForAttribute:@"foo_"], @"bar");
  328. [metric setValue:@"bar" forAttribute:@"foo_$"];
  329. XCTAssertNil([metric valueForAttribute:@"foo_$"]);
  330. [metric setValue:@"bar" forAttribute:@"FOO_$"];
  331. XCTAssertNil([metric valueForAttribute:@"FOO_$"]);
  332. [metric setValue:@"bar" forAttribute:@"FOO_"];
  333. XCTAssertEqual([metric valueForAttribute:@"FOO_"], @"bar");
  334. }
  335. /** Validates if long attribute values gets truncated. */
  336. - (void)testMaxLengthForAttributeValue {
  337. NSString *testValue = [@"abc" stringByPaddingToLength:kFPRMaxAttributeValueLength + 1
  338. withString:@"-"
  339. startingAtIndex:0];
  340. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  341. HTTPMethod:FIRHTTPMethodGET];
  342. [metric setValue:testValue forAttribute:@"foo"];
  343. XCTAssertNil([metric valueForAttribute:@"foo"]);
  344. }
  345. /** Validates if empty name or value of the attributes are getting dropped. */
  346. - (void)testAttributesWithEmptyValues {
  347. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  348. HTTPMethod:FIRHTTPMethodGET];
  349. [metric setValue:@"" forAttribute:@"foo"];
  350. XCTAssertNil([metric valueForAttribute:@"foo"]);
  351. [metric setValue:@"bar" forAttribute:@""];
  352. XCTAssertNil([metric valueForAttribute:@""]);
  353. }
  354. /** Validates if the limit the maximum number of attributes work. */
  355. - (void)testMaximumNumberOfAttributes {
  356. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  357. HTTPMethod:FIRHTTPMethodGET];
  358. for (int i = 0; i < kFPRMaxGlobalCustomAttributesCount; i++) {
  359. NSString *attributeName = [NSString stringWithFormat:@"dim%d", i];
  360. [metric setValue:@"bar" forAttribute:attributeName];
  361. XCTAssertEqual([metric valueForAttribute:attributeName], @"bar");
  362. }
  363. [metric setValue:@"bar" forAttribute:@"foo"];
  364. XCTAssertNil([metric valueForAttribute:@"foo"]);
  365. }
  366. /** Validates if removing old attributes and adding new attributes work. */
  367. - (void)testRemovingAndAddingAttributes {
  368. FIRHTTPMetric *metric = [[FIRHTTPMetric alloc] initWithURL:self.sampleURL
  369. HTTPMethod:FIRHTTPMethodGET];
  370. for (int i = 0; i < kFPRMaxGlobalCustomAttributesCount; i++) {
  371. NSString *attributeName = [NSString stringWithFormat:@"dim%d", i];
  372. [metric setValue:@"bar" forAttribute:attributeName];
  373. XCTAssertEqual([metric valueForAttribute:attributeName], @"bar");
  374. }
  375. [metric removeAttribute:@"dim1"];
  376. [metric setValue:@"bar" forAttribute:@"foo"];
  377. XCTAssertEqual([metric valueForAttribute:@"foo"], @"bar");
  378. }
  379. @end