FIRHTTPMetricTests.m 20 KB

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