FIRHTTPMetricTests.m 20 KB

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