@react-native-firebase/perf 26.4.0 → 27.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/RNFBPerf.podspec +6 -1
  3. package/android/build.gradle +3 -0
  4. package/android/src/main/java/io/invertase/firebase/perf/RNFBPerfHandleRegistry.kt +71 -0
  5. package/android/src/test/java/io/invertase/firebase/perf/RNFBPerfHandleRegistryTest.java +30 -0
  6. package/dist/module/version.js +1 -1
  7. package/dist/typescript/lib/index.d.ts +1 -1
  8. package/dist/typescript/lib/version.d.ts +1 -1
  9. package/ios/RNFBPerf/RNFBPerfHandleRegistry.m +28 -9
  10. package/ios/RNFBPerf/RNFBPerfHandleRegistryStorage.swift +58 -0
  11. package/ios/RNFBPerf/RNFBPerfHelper.h +71 -0
  12. package/ios/RNFBPerf/RNFBPerfHelper.m +219 -0
  13. package/ios/RNFBPerf/RNFBPerfHttpMethodMapper.swift +86 -0
  14. package/ios/RNFBPerf/RNFBPerfHttpMetricStopApplier.swift +87 -0
  15. package/ios/RNFBPerf/RNFBPerfModule.mm +35 -172
  16. package/ios/RNFBPerf/RNFBPerfTraceStopApplier.swift +68 -0
  17. package/ios/RNFBPerf.xcodeproj/project.pbxproj +32 -0
  18. package/ios/RNFBPerfUnitTests/HostStubs/Firebase/FIRPerformanceHostStub.m +170 -0
  19. package/ios/RNFBPerfUnitTests/HostStubs/Firebase/Firebase.h +75 -0
  20. package/ios/RNFBPerfUnitTests/HostStubs/RNFBApp/RNFBSharedUtils.h +12 -0
  21. package/ios/RNFBPerfUnitTests/HostStubs/RNFBApp/RNFBSharedUtilsHostStub.m +18 -0
  22. package/ios/RNFBPerfUnitTests/HostStubs/React/RCTBridgeModule.h +10 -0
  23. package/ios/RNFBPerfUnitTests/RNFBPerfHandleRegistryTests.m +46 -0
  24. package/ios/RNFBPerfUnitTests/RNFBPerfHelperHostStubTests.m +200 -0
  25. package/ios/RNFBPerfUnitTests/RNFBPerfHttpMethodMapperFacadeTests.m +52 -0
  26. package/ios/RNFBPerfUnitTests/RNFBPerfHttpMethodMapperTests.swift +92 -0
  27. package/ios/RNFBPerfUnitTests/RNFBPerfHttpMetricStopApplierFacadeTests.m +105 -0
  28. package/ios/RNFBPerfUnitTests/RNFBPerfHttpMetricStopApplierTests.swift +201 -0
  29. package/ios/RNFBPerfUnitTests/RNFBPerfTraceStopApplierFacadeTests.m +75 -0
  30. package/ios/RNFBPerfUnitTests/RNFBPerfTraceStopApplierTests.swift +142 -0
  31. package/ios/RNFBPerfUnitTests/RNFBPerfUnitTests.xcodeproj/project.pbxproj +70 -0
  32. package/lib/version.ts +1 -1
  33. package/package.json +4 -4
  34. package/android/src/main/java/io/invertase/firebase/perf/RNFBPerfHandleRegistry.java +0 -72
@@ -0,0 +1,170 @@
1
+ /**
2
+ * Host-only stub for macOS XCTest. Not shipped in the production pod.
3
+ */
4
+ #import <Firebase/Firebase.h>
5
+
6
+ @implementation FIRTrace {
7
+ NSString *_name;
8
+ NSMutableDictionary<NSString *, NSNumber *> *_metrics;
9
+ NSMutableDictionary<NSString *, NSString *> *_attributes;
10
+ }
11
+
12
+ - (instancetype)initWithName:(NSString *)name {
13
+ self = [super init];
14
+ if (self) {
15
+ _name = [name copy];
16
+ _metrics = [NSMutableDictionary new];
17
+ _attributes = [NSMutableDictionary new];
18
+ }
19
+ return self;
20
+ }
21
+
22
+ - (NSString *)name {
23
+ return _name;
24
+ }
25
+
26
+ - (NSMutableDictionary<NSString *, NSNumber *> *)metrics {
27
+ return _metrics;
28
+ }
29
+
30
+ - (NSMutableDictionary<NSString *, NSString *> *)attributes {
31
+ return _attributes;
32
+ }
33
+
34
+ - (void)start {
35
+ self.startCallCount += 1;
36
+ }
37
+
38
+ - (void)stop {
39
+ self.stopCallCount += 1;
40
+ }
41
+
42
+ - (void)setIntValue:(int64_t)value forMetric:(NSString *)metricName {
43
+ _metrics[metricName] = @(value);
44
+ }
45
+
46
+ - (void)setValue:(NSString *)value forAttribute:(NSString *)attributeName {
47
+ _attributes[attributeName] = value;
48
+ }
49
+
50
+ @end
51
+
52
+ @implementation FIRHTTPMetric {
53
+ NSURL *_url;
54
+ FIRHTTPMethod _HTTPMethod;
55
+ NSMutableDictionary<NSString *, NSString *> *_attributes;
56
+ }
57
+
58
+ - (instancetype)initWithURL:(NSURL *)url HTTPMethod:(FIRHTTPMethod)httpMethod {
59
+ self = [super init];
60
+ if (self) {
61
+ _url = url;
62
+ _HTTPMethod = httpMethod;
63
+ _attributes = [NSMutableDictionary new];
64
+ _responseCode = 0;
65
+ _requestPayloadSize = 0;
66
+ _responsePayloadSize = 0;
67
+ [FIRPerformance recordCreatedHttpMetric:self];
68
+ }
69
+ return self;
70
+ }
71
+
72
+ - (NSURL *)url {
73
+ return _url;
74
+ }
75
+
76
+ - (FIRHTTPMethod)HTTPMethod {
77
+ return _HTTPMethod;
78
+ }
79
+
80
+ - (NSMutableDictionary<NSString *, NSString *> *)attributes {
81
+ return _attributes;
82
+ }
83
+
84
+ - (void)start {
85
+ self.startCallCount += 1;
86
+ }
87
+
88
+ - (void)stop {
89
+ self.stopCallCount += 1;
90
+ }
91
+
92
+ - (void)setValue:(NSString *)value forAttribute:(NSString *)attributeName {
93
+ _attributes[attributeName] = value;
94
+ }
95
+
96
+ - (void)setResponseCode:(NSInteger)responseCode {
97
+ _responseCode = responseCode;
98
+ }
99
+
100
+ - (void)setRequestPayloadSize:(NSInteger)bytes {
101
+ _requestPayloadSize = bytes;
102
+ }
103
+
104
+ - (void)setResponsePayloadSize:(NSInteger)bytes {
105
+ _responsePayloadSize = bytes;
106
+ }
107
+
108
+ - (void)setResponseContentType:(NSString *)contentType {
109
+ _responseContentType = [contentType copy];
110
+ }
111
+
112
+ @end
113
+
114
+ @implementation FIRPerformance
115
+
116
+ static FIRPerformance *sShared;
117
+ static NSMutableArray<FIRTrace *> *sCreatedTraces;
118
+ static NSMutableArray<FIRHTTPMetric *> *sCreatedHttpMetrics;
119
+
120
+ + (void)initialize {
121
+ if (self == [FIRPerformance class]) {
122
+ sCreatedTraces = [NSMutableArray new];
123
+ sCreatedHttpMetrics = [NSMutableArray new];
124
+ }
125
+ }
126
+
127
+ + (instancetype)sharedInstance {
128
+ static dispatch_once_t once;
129
+ dispatch_once(&once, ^{
130
+ sShared = [[FIRPerformance alloc] init];
131
+ });
132
+ return sShared;
133
+ }
134
+
135
+ - (FIRTrace *)traceWithName:(NSString *)name {
136
+ FIRTrace *trace = [[FIRTrace alloc] initWithName:name];
137
+ [sCreatedTraces addObject:trace];
138
+ return trace;
139
+ }
140
+
141
+ + (NSArray<FIRTrace *> *)createdTraces {
142
+ return [sCreatedTraces copy];
143
+ }
144
+
145
+ + (void)setCreatedTraces:(NSArray<FIRTrace *> *)createdTraces {
146
+ sCreatedTraces = createdTraces ? [createdTraces mutableCopy] : [NSMutableArray new];
147
+ }
148
+
149
+ + (NSArray<FIRHTTPMetric *> *)createdHttpMetrics {
150
+ return [sCreatedHttpMetrics copy];
151
+ }
152
+
153
+ + (void)setCreatedHttpMetrics:(NSArray<FIRHTTPMetric *> *)createdHttpMetrics {
154
+ sCreatedHttpMetrics =
155
+ createdHttpMetrics ? [createdHttpMetrics mutableCopy] : [NSMutableArray new];
156
+ }
157
+
158
+ + (void)recordCreatedHttpMetric:(FIRHTTPMetric *)metric {
159
+ [sCreatedHttpMetrics addObject:metric];
160
+ }
161
+
162
+ + (void)resetTestState {
163
+ FIRPerformance *shared = [self sharedInstance];
164
+ shared.dataCollectionEnabled = NO;
165
+ shared.instrumentationEnabled = NO;
166
+ [sCreatedTraces removeAllObjects];
167
+ [sCreatedHttpMetrics removeAllObjects];
168
+ }
169
+
170
+ @end
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Host-only stub for macOS XCTest. Not shipped in the production pod.
3
+ * Enough of Firebase Performance for compiling RNFBPerfHelper.m and
4
+ * asserting SDK call wiring without linking the real SDK.
5
+ */
6
+ #import <Foundation/Foundation.h>
7
+
8
+ typedef NS_ENUM(NSInteger, FIRHTTPMethod) {
9
+ FIRHTTPMethodGET = 0,
10
+ FIRHTTPMethodPUT = 1,
11
+ FIRHTTPMethodPOST = 2,
12
+ FIRHTTPMethodDELETE = 3,
13
+ FIRHTTPMethodHEAD = 4,
14
+ FIRHTTPMethodPATCH = 5,
15
+ FIRHTTPMethodOPTIONS = 6,
16
+ FIRHTTPMethodTRACE = 7,
17
+ FIRHTTPMethodCONNECT = 8,
18
+ };
19
+
20
+ @interface FIRTrace : NSObject
21
+
22
+ @property(nonatomic, readonly, copy) NSString *name;
23
+ @property(nonatomic, assign) NSUInteger startCallCount;
24
+ @property(nonatomic, assign) NSUInteger stopCallCount;
25
+ @property(nonatomic, readonly, strong) NSMutableDictionary<NSString *, NSNumber *> *metrics;
26
+ @property(nonatomic, readonly, strong) NSMutableDictionary<NSString *, NSString *> *attributes;
27
+
28
+ - (instancetype)initWithName:(NSString *)name;
29
+ - (void)start;
30
+ - (void)stop;
31
+ - (void)setIntValue:(int64_t)value forMetric:(NSString *)metricName;
32
+ - (void)setValue:(NSString *)value forAttribute:(NSString *)attributeName;
33
+
34
+ @end
35
+
36
+ @interface FIRHTTPMetric : NSObject
37
+
38
+ @property(nonatomic, readonly, copy) NSURL *url;
39
+ @property(nonatomic, readonly, assign) FIRHTTPMethod HTTPMethod;
40
+ @property(nonatomic, assign) NSUInteger startCallCount;
41
+ @property(nonatomic, assign) NSUInteger stopCallCount;
42
+ @property(nonatomic, assign) NSInteger responseCode;
43
+ @property(nonatomic, assign) NSInteger requestPayloadSize;
44
+ @property(nonatomic, assign) NSInteger responsePayloadSize;
45
+ @property(nonatomic, copy, nullable) NSString *responseContentType;
46
+ @property(nonatomic, readonly, strong) NSMutableDictionary<NSString *, NSString *> *attributes;
47
+
48
+ - (instancetype)initWithURL:(NSURL *)url HTTPMethod:(FIRHTTPMethod)httpMethod;
49
+ - (void)start;
50
+ - (void)stop;
51
+ - (void)setValue:(NSString *)value forAttribute:(NSString *)attributeName;
52
+ - (void)setResponseCode:(NSInteger)responseCode;
53
+ - (void)setRequestPayloadSize:(NSInteger)bytes;
54
+ - (void)setResponsePayloadSize:(NSInteger)bytes;
55
+ - (void)setResponseContentType:(NSString *)contentType;
56
+
57
+ @end
58
+
59
+ @interface FIRPerformance : NSObject
60
+
61
+ @property(nonatomic, assign) BOOL dataCollectionEnabled;
62
+ @property(nonatomic, assign) BOOL instrumentationEnabled;
63
+
64
+ + (instancetype)sharedInstance;
65
+
66
+ - (FIRTrace *)traceWithName:(NSString *)name;
67
+
68
+ /** Test seams */
69
+ @property(class, nonatomic, copy, nullable) NSArray<FIRTrace *> *createdTraces;
70
+ @property(class, nonatomic, copy, nullable) NSArray<FIRHTTPMetric *> *createdHttpMetrics;
71
+
72
+ + (void)resetTestState;
73
+ + (void)recordCreatedHttpMetric:(FIRHTTPMetric *)metric;
74
+
75
+ @end
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Host-only stub for macOS XCTest. Not shipped in the production pod.
3
+ */
4
+ #import <Foundation/Foundation.h>
5
+ #import <React/RCTBridgeModule.h>
6
+
7
+ @interface RNFBSharedUtils : NSObject
8
+
9
+ + (void)rejectPromiseWithUserInfo:(RCTPromiseRejectBlock)reject
10
+ userInfo:(NSMutableDictionary *)userInfo;
11
+
12
+ @end
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Host-only stub for macOS XCTest. Not shipped in the production pod.
3
+ */
4
+ #import "RNFBApp/RNFBSharedUtils.h"
5
+
6
+ @implementation RNFBSharedUtils
7
+
8
+ + (void)rejectPromiseWithUserInfo:(RCTPromiseRejectBlock)reject
9
+ userInfo:(NSMutableDictionary *)userInfo {
10
+ if (reject == nil) {
11
+ return;
12
+ }
13
+ NSString *code = userInfo[@"code"] ?: @"unknown";
14
+ NSString *message = userInfo[@"message"] ?: @"";
15
+ reject(code, message, nil);
16
+ }
17
+
18
+ @end
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Host-only stub for macOS XCTest. Not shipped in the production pod.
3
+ */
4
+ #import <Foundation/Foundation.h>
5
+
6
+ typedef void (^RCTPromiseResolveBlock)(id result);
7
+ typedef void (^RCTPromiseRejectBlock)(NSString *code, NSString *message, NSError *error);
8
+
9
+ @protocol RCTBridgeModule <NSObject>
10
+ @end
@@ -65,6 +65,13 @@
65
65
  XCTAssertEqual(first, [self.registry get:@1]);
66
66
  }
67
67
 
68
+ - (void)testPut_occupiedId_nilErrorPointer_returnsNO {
69
+ FakePerfHandle *first = [[FakePerfHandle alloc] init];
70
+ XCTAssertTrue([self.registry put:@1 value:first error:nil]);
71
+ XCTAssertFalse([self.registry put:@1 value:[[FakePerfHandle alloc] init] error:nil]);
72
+ XCTAssertEqual(first, [self.registry get:@1]);
73
+ }
74
+
68
75
  - (void)testPutOrDiscard_collision_dropsIncomingWithoutStop {
69
76
  FakePerfHandle *first = [[FakePerfHandle alloc] init];
70
77
  FakePerfHandle *duplicate = [[FakePerfHandle alloc] init];
@@ -90,6 +97,45 @@
90
97
  XCTAssertEqual(second, [self.registry get:@3]);
91
98
  }
92
99
 
100
+ - (void)testPutReplacing_whenFree_returnsNil {
101
+ FakePerfHandle *handle = [[FakePerfHandle alloc] init];
102
+ XCTAssertNil([self.registry putReplacing:@7 value:handle]);
103
+ XCTAssertEqual(handle, [self.registry get:@7]);
104
+ }
105
+
106
+ - (void)testTakeIf_missingDoesNotCallCondition {
107
+ __block BOOL conditionCalled = NO;
108
+ XCTAssertNil([self.registry takeIf:@99
109
+ when:^BOOL(id value) {
110
+ (void)value;
111
+ conditionCalled = YES;
112
+ return YES;
113
+ }]);
114
+ XCTAssertFalse(conditionCalled);
115
+ }
116
+
117
+ - (void)testTakeIf_falseLeavesValue {
118
+ FakePerfHandle *handle = [[FakePerfHandle alloc] init];
119
+ XCTAssertTrue([self.registry put:@8 value:handle error:nil]);
120
+ XCTAssertNil([self.registry takeIf:@8
121
+ when:^BOOL(id value) {
122
+ XCTAssertEqual(handle, value);
123
+ return NO;
124
+ }]);
125
+ XCTAssertEqual(handle, [self.registry get:@8]);
126
+ }
127
+
128
+ - (void)testTakeIf_trueRemovesAndReturns {
129
+ FakePerfHandle *handle = [[FakePerfHandle alloc] init];
130
+ XCTAssertTrue([self.registry put:@9 value:handle error:nil]);
131
+ XCTAssertEqual(handle, [self.registry takeIf:@9
132
+ when:^BOOL(id value) {
133
+ return value == handle;
134
+ }]);
135
+ XCTAssertNil([self.registry get:@9]);
136
+ XCTAssertEqual(handle.stopCount, 0);
137
+ }
138
+
93
139
  - (void)testPutReplacing_moduleCollisionPattern_stopsDisplacedOutsideLock {
94
140
  FakePerfHandle *first = [[FakePerfHandle alloc] init];
95
141
  FakePerfHandle *second = [[FakePerfHandle alloc] init];
@@ -0,0 +1,200 @@
1
+ /**
2
+ * Copyright (c) 2016-present Invertase Limited & Contributors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this library except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ #import <XCTest/XCTest.h>
18
+
19
+ #import <Firebase/Firebase.h>
20
+ #import "RNFBPerfHandleRegistry.h"
21
+ #import "RNFBPerfHelper.h"
22
+
23
+ @interface RNFBPerfHelperHostStubTests : XCTestCase
24
+ @property(nonatomic, strong) RNFBPerfHandleRegistry *traces;
25
+ @property(nonatomic, strong) RNFBPerfHandleRegistry *httpMetrics;
26
+ @end
27
+
28
+ @implementation RNFBPerfHelperHostStubTests
29
+
30
+ - (void)setUp {
31
+ [super setUp];
32
+ [FIRPerformance resetTestState];
33
+ self.traces = [[RNFBPerfHandleRegistry alloc] init];
34
+ self.httpMetrics = [[RNFBPerfHandleRegistry alloc] init];
35
+ }
36
+
37
+ - (void)tearDown {
38
+ [FIRPerformance resetTestState];
39
+ self.traces = nil;
40
+ self.httpMetrics = nil;
41
+ [super tearDown];
42
+ }
43
+
44
+ - (void)testConstantsDictionary_reflectsSharedInstanceFlags {
45
+ [FIRPerformance sharedInstance].dataCollectionEnabled = YES;
46
+ [FIRPerformance sharedInstance].instrumentationEnabled = YES;
47
+
48
+ NSDictionary *constants = [RNFBPerfHelper constantsDictionary];
49
+ XCTAssertEqualObjects(constants[@"isPerformanceCollectionEnabled"], @YES);
50
+ XCTAssertEqualObjects(constants[@"isInstrumentationEnabled"], @YES);
51
+ }
52
+
53
+ - (void)testSetPerformanceCollectionEnabled_setsFlagAndResolves {
54
+ __block id resolved = @"unset";
55
+ __block BOOL rejected = NO;
56
+
57
+ [RNFBPerfHelper setPerformanceCollectionEnabled:YES
58
+ resolve:^(id result) {
59
+ resolved = result;
60
+ }
61
+ reject:^(NSString *code, NSString *message, NSError *error) {
62
+ (void)code;
63
+ (void)message;
64
+ (void)error;
65
+ rejected = YES;
66
+ }];
67
+
68
+ XCTAssertFalse(rejected);
69
+ XCTAssertEqualObjects(resolved, [NSNull null]);
70
+ XCTAssertTrue([FIRPerformance sharedInstance].dataCollectionEnabled);
71
+ }
72
+
73
+ - (void)testSetInstrumentationEnabled_setsFlagAndResolves {
74
+ __block id resolved = @"unset";
75
+
76
+ [RNFBPerfHelper setInstrumentationEnabled:YES
77
+ resolve:^(id result) {
78
+ resolved = result;
79
+ }
80
+ reject:^(NSString *code, NSString *message, NSError *error) {
81
+ (void)code;
82
+ (void)message;
83
+ (void)error;
84
+ XCTFail(@"unexpected reject");
85
+ }];
86
+
87
+ XCTAssertEqualObjects(resolved, [NSNull null]);
88
+ XCTAssertTrue([FIRPerformance sharedInstance].instrumentationEnabled);
89
+ }
90
+
91
+ - (void)testStartTrace_startsAndStoresHandle {
92
+ [RNFBPerfHelper startTraceWithId:@1 identifier:@"trace-a" inRegistry:self.traces];
93
+
94
+ FIRTrace *trace = [self.traces get:@1];
95
+ XCTAssertNotNil(trace);
96
+ XCTAssertEqualObjects(trace.name, @"trace-a");
97
+ XCTAssertEqual(trace.startCallCount, 1u);
98
+ XCTAssertEqual(FIRPerformance.createdTraces.count, 1u);
99
+ }
100
+
101
+ - (void)testStartTrace_displacesPreviousAndStopsIt {
102
+ [RNFBPerfHelper startTraceWithId:@1 identifier:@"first" inRegistry:self.traces];
103
+ FIRTrace *first = [self.traces get:@1];
104
+
105
+ [RNFBPerfHelper startTraceWithId:@1 identifier:@"second" inRegistry:self.traces];
106
+ FIRTrace *second = [self.traces get:@1];
107
+
108
+ XCTAssertNotEqual(first, second);
109
+ XCTAssertEqual(first.stopCallCount, 1u);
110
+ XCTAssertEqual(second.startCallCount, 1u);
111
+ XCTAssertEqualObjects(second.name, @"second");
112
+ }
113
+
114
+ - (void)testStopTrace_appliesMetricsAttributesAndStops {
115
+ [RNFBPerfHelper startTraceWithId:@7 identifier:@"stop-me" inRegistry:self.traces];
116
+ FIRTrace *trace = [self.traces get:@7];
117
+
118
+ [RNFBPerfHelper stopTraceWithId:@7
119
+ metrics:@{@"bytes" : @42}
120
+ attributes:@{@"screen" : @"home"}
121
+ inRegistry:self.traces];
122
+
123
+ XCTAssertNil([self.traces get:@7]);
124
+ XCTAssertEqualObjects(trace.metrics[@"bytes"], @42);
125
+ XCTAssertEqualObjects(trace.attributes[@"screen"], @"home");
126
+ XCTAssertEqual(trace.stopCallCount, 1u);
127
+ }
128
+
129
+ - (void)testStopTrace_missingHandle_isNoOp {
130
+ [RNFBPerfHelper stopTraceWithId:@99 metrics:@{@"m" : @1} attributes:nil inRegistry:self.traces];
131
+ XCTAssertEqual(FIRPerformance.createdTraces.count, 0u);
132
+ }
133
+
134
+ - (void)testStartHttpMetric_mapsMethodStartsAndStores {
135
+ [RNFBPerfHelper startHttpMetricWithId:@3
136
+ url:@"https://example.com/api"
137
+ httpMethod:@"POST"
138
+ inRegistry:self.httpMetrics];
139
+
140
+ FIRHTTPMetric *metric = [self.httpMetrics get:@3];
141
+ XCTAssertNotNil(metric);
142
+ XCTAssertEqualObjects(metric.url.absoluteString, @"https://example.com/api");
143
+ XCTAssertEqual(metric.HTTPMethod, FIRHTTPMethodPOST);
144
+ XCTAssertEqual(metric.startCallCount, 1u);
145
+ }
146
+
147
+ - (void)testStartHttpMetric_displacesPreviousAndStopsIt {
148
+ [RNFBPerfHelper startHttpMetricWithId:@3
149
+ url:@"https://example.com/a"
150
+ httpMethod:@"GET"
151
+ inRegistry:self.httpMetrics];
152
+ FIRHTTPMetric *first = [self.httpMetrics get:@3];
153
+
154
+ [RNFBPerfHelper startHttpMetricWithId:@3
155
+ url:@"https://example.com/b"
156
+ httpMethod:@"PUT"
157
+ inRegistry:self.httpMetrics];
158
+ FIRHTTPMetric *second = [self.httpMetrics get:@3];
159
+
160
+ XCTAssertNotEqual(first, second);
161
+ XCTAssertEqual(first.stopCallCount, 1u);
162
+ XCTAssertEqual(second.HTTPMethod, FIRHTTPMethodPUT);
163
+ }
164
+
165
+ - (void)testStopHttpMetric_appliesFieldsAndStops {
166
+ [RNFBPerfHelper startHttpMetricWithId:@4
167
+ url:@"https://example.com"
168
+ httpMethod:@"GET"
169
+ inRegistry:self.httpMetrics];
170
+ FIRHTTPMetric *metric = [self.httpMetrics get:@4];
171
+
172
+ [RNFBPerfHelper stopHttpMetricWithId:@4
173
+ attributes:@{@"route" : @"/v1"}
174
+ httpResponseCode:@204
175
+ requestPayloadSize:@10
176
+ responsePayloadSize:@20
177
+ responseContentType:@"application/json"
178
+ inRegistry:self.httpMetrics];
179
+
180
+ XCTAssertNil([self.httpMetrics get:@4]);
181
+ XCTAssertEqualObjects(metric.attributes[@"route"], @"/v1");
182
+ XCTAssertEqual(metric.responseCode, 204);
183
+ XCTAssertEqual(metric.requestPayloadSize, 10);
184
+ XCTAssertEqual(metric.responsePayloadSize, 20);
185
+ XCTAssertEqualObjects(metric.responseContentType, @"application/json");
186
+ XCTAssertEqual(metric.stopCallCount, 1u);
187
+ }
188
+
189
+ - (void)testStopHttpMetric_missingHandle_isNoOp {
190
+ [RNFBPerfHelper stopHttpMetricWithId:@88
191
+ attributes:nil
192
+ httpResponseCode:@200
193
+ requestPayloadSize:nil
194
+ responsePayloadSize:nil
195
+ responseContentType:nil
196
+ inRegistry:self.httpMetrics];
197
+ XCTAssertEqual(FIRPerformance.createdHttpMetrics.count, 0u);
198
+ }
199
+
200
+ @end
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Copyright (c) 2016-present Invertase Limited & Contributors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this library except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ #import <XCTest/XCTest.h>
18
+
19
+ #import "RNFBHandleMapStorage-Swift.inc"
20
+
21
+ @interface RNFBPerfHttpMethodMapperFacadeTests : XCTestCase
22
+ @end
23
+
24
+ @implementation RNFBPerfHttpMethodMapperFacadeTests
25
+
26
+ - (void)testObjCSelectorMapsKnownMethods {
27
+ XCTAssertEqual([RNFBPerfHttpMethodMapper httpMethodRawValueForString:@"put"], 1);
28
+ XCTAssertEqual([RNFBPerfHttpMethodMapper httpMethodRawValueForString:@"POST"], 2);
29
+ XCTAssertEqual([RNFBPerfHttpMethodMapper httpMethodRawValueForString:@"delete"], 3);
30
+ XCTAssertEqual([RNFBPerfHttpMethodMapper httpMethodRawValueForString:@"HEAD"], 4);
31
+ XCTAssertEqual([RNFBPerfHttpMethodMapper httpMethodRawValueForString:@"patch"], 5);
32
+ XCTAssertEqual([RNFBPerfHttpMethodMapper httpMethodRawValueForString:@"OPTIONS"], 6);
33
+ XCTAssertEqual([RNFBPerfHttpMethodMapper httpMethodRawValueForString:@"trace"], 7);
34
+ XCTAssertEqual([RNFBPerfHttpMethodMapper httpMethodRawValueForString:@"CONNECT"], 8);
35
+ }
36
+
37
+ - (void)testObjCSelectorDefaultsUnknownNilEmptyGetToGet {
38
+ XCTAssertEqual([RNFBPerfHttpMethodMapper httpMethodRawValueForString:@"get"], 0);
39
+ XCTAssertEqual([RNFBPerfHttpMethodMapper httpMethodRawValueForString:@"bogus"], 0);
40
+ XCTAssertEqual([RNFBPerfHttpMethodMapper httpMethodRawValueForString:@""], 0);
41
+ XCTAssertEqual([RNFBPerfHttpMethodMapper httpMethodRawValueForString:nil], 0);
42
+ }
43
+
44
+ - (void)testFacadePublicSelectorUnchanged {
45
+ XCTAssertTrue(
46
+ [RNFBPerfHttpMethodMapper respondsToSelector:@selector(httpMethodRawValueForString:)]);
47
+ NSMethodSignature *signature =
48
+ [RNFBPerfHttpMethodMapper methodSignatureForSelector:@selector(httpMethodRawValueForString:)];
49
+ XCTAssertEqual(strcmp(signature.methodReturnType, @encode(NSInteger)), 0);
50
+ }
51
+
52
+ @end
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Copyright (c) 2016-present Invertase Limited & Contributors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this library except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import Foundation
18
+ import XCTest
19
+
20
+ final class RNFBPerfHttpMethodMapperTests: XCTestCase {
21
+ /// `FIRHTTPMethodGET`
22
+ private let getMethod = 0
23
+ /// `FIRHTTPMethodPUT`
24
+ private let putMethod = 1
25
+ /// `FIRHTTPMethodPOST`
26
+ private let postMethod = 2
27
+ /// `FIRHTTPMethodDELETE`
28
+ private let deleteMethod = 3
29
+ /// `FIRHTTPMethodHEAD`
30
+ private let headMethod = 4
31
+ /// `FIRHTTPMethodPATCH`
32
+ private let patchMethod = 5
33
+ /// `FIRHTTPMethodOPTIONS`
34
+ private let optionsMethod = 6
35
+ /// `FIRHTTPMethodTRACE`
36
+ private let traceMethod = 7
37
+ /// `FIRHTTPMethodCONNECT`
38
+ private let connectMethod = 8
39
+
40
+ func testPutMapsToPut() {
41
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "put"), putMethod)
42
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "PUT"), putMethod)
43
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "Put"), putMethod)
44
+ }
45
+
46
+ func testPostMapsToPost() {
47
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "post"), postMethod)
48
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "POST"), postMethod)
49
+ }
50
+
51
+ func testHeadMapsToHead() {
52
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "head"), headMethod)
53
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "HEAD"), headMethod)
54
+ }
55
+
56
+ func testTraceMapsToTrace() {
57
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "trace"), traceMethod)
58
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "TRACE"), traceMethod)
59
+ }
60
+
61
+ func testPatchMapsToPatch() {
62
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "patch"), patchMethod)
63
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "PATCH"), patchMethod)
64
+ }
65
+
66
+ func testDeleteMapsToDelete() {
67
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "delete"), deleteMethod)
68
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "DELETE"), deleteMethod)
69
+ }
70
+
71
+ func testOptionsMapsToOptions() {
72
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "options"), optionsMethod)
73
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "OPTIONS"), optionsMethod)
74
+ }
75
+
76
+ func testConnectMapsToConnect() {
77
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "connect"), connectMethod)
78
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "CONNECT"), connectMethod)
79
+ }
80
+
81
+ func testGetAndUnknownDefaultToGet() {
82
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "get"), getMethod)
83
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "GET"), getMethod)
84
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "bogus"), getMethod)
85
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: "unknown"), getMethod)
86
+ }
87
+
88
+ func testNilAndEmptyDefaultToGet() {
89
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: nil), getMethod)
90
+ XCTAssertEqual(RNFBPerfHttpMethodMapper.httpMethodRawValue(forString: ""), getMethod)
91
+ }
92
+ }