@@ -34,12 +34,101 @@ class GoFeatureFlagProviderTests: XCTestCase {
34
34
35
35
let expectation = self . expectation ( description: " Waiting for delay " )
36
36
37
- DispatchQueue . global ( ) . asyncAfter ( deadline: . now( ) + 1 .0) { expectation. fulfill ( ) }
38
- await fulfillment ( of: [ expectation] , timeout: 2 .0)
37
+ DispatchQueue . global ( ) . asyncAfter ( deadline: . now( ) + 2 .0) { expectation. fulfill ( ) }
38
+ await fulfillment ( of: [ expectation] , timeout: 3 .0)
39
39
40
40
XCTAssertEqual ( 1 , mockNetworkService. dataCollectorCallCounter)
41
41
XCTAssertEqual ( 6 , mockNetworkService. dataCollectorEventCounter)
42
42
}
43
+
44
+ func testExporterMetadata( ) async {
45
+ let mockNetworkService = MockNetworkingService ( mockStatus: 200 )
46
+ let provider = GoFeatureFlagProvider (
47
+ options: GoFeatureFlagProviderOptions (
48
+ endpoint: " https://localhost:1031 " ,
49
+ dataFlushInterval: 1 ,
50
+ exporterMetadata: [ " version " : ExporterMetadataValue . string ( " 1.0.0 " ) , " testInt " : ExporterMetadataValue . integer ( 123 ) , " testDouble " : ExporterMetadataValue . double ( 123.45 ) ] ,
51
+ networkService: mockNetworkService
52
+ )
53
+ )
54
+ let evaluationCtx = MutableContext ( targetingKey: " ede04e44-463d-40d1-8fc0-b1d6855578d0 " )
55
+ let api = OpenFeatureAPI ( )
56
+ await api. setProviderAndWait ( provider: provider, initialContext: evaluationCtx)
57
+ let client = api. getClient ( )
58
+
59
+ _ = client. getBooleanDetails ( key: " my-flag " , defaultValue: false )
60
+ _ = client. getBooleanDetails ( key: " my-flag " , defaultValue: false )
61
+ _ = client. getIntegerDetails ( key: " int-flag " , defaultValue: 1 )
62
+ _ = client. getDoubleDetails ( key: " double-flag " , defaultValue: 1.0 )
63
+ _ = client. getStringDetails ( key: " string-flag " , defaultValue: " default " )
64
+ _ = client. getObjectDetails ( key: " object-flag " , defaultValue: Value . null)
65
+
66
+ let expectation = self . expectation ( description: " Waiting for delay " )
67
+
68
+ DispatchQueue . global ( ) . asyncAfter ( deadline: . now( ) + 2.0 ) { expectation. fulfill ( ) }
69
+ await fulfillment ( of: [ expectation] , timeout: 3.0 )
70
+
71
+ XCTAssertEqual ( 1 , mockNetworkService. dataCollectorCallCounter)
72
+ XCTAssertEqual ( 6 , mockNetworkService. dataCollectorEventCounter)
73
+
74
+ do {
75
+ let httpBodyCollector = mockNetworkService. requests [ mockNetworkService. requests. count - 1 ] . httpBody!
76
+ let decodedStruct = try JSONDecoder ( ) . decode ( DataCollectorRequest . self, from: httpBodyCollector)
77
+ let want = [
78
+ " version " : ExporterMetadataValue . string ( " 1.0.0 " ) ,
79
+ " testDouble " : ExporterMetadataValue . double ( 123.45 ) ,
80
+ " testInt " : ExporterMetadataValue . integer ( 123 ) ,
81
+ " openfeature " : ExporterMetadataValue . bool ( true ) ,
82
+ " provider " : ExporterMetadataValue . string ( " swift " )
83
+ ] as? [ String : ExporterMetadataValue ]
84
+ XCTAssertEqual ( want, decodedStruct. meta)
85
+ } catch {
86
+ XCTFail ( " Error deserializing: \( error) " )
87
+ }
88
+ }
89
+
90
+ func testExporterMetadataNil( ) async {
91
+ let mockNetworkService = MockNetworkingService ( mockStatus: 200 )
92
+ let provider = GoFeatureFlagProvider (
93
+ options: GoFeatureFlagProviderOptions (
94
+ endpoint: " https://localhost:1031 " ,
95
+ dataFlushInterval: 1 ,
96
+ exporterMetadata: nil ,
97
+ networkService: mockNetworkService
98
+ )
99
+ )
100
+ let evaluationCtx = MutableContext ( targetingKey: " ede04e44-463d-40d1-8fc0-b1d6855578d0 " )
101
+ let api = OpenFeatureAPI ( )
102
+ await api. setProviderAndWait ( provider: provider, initialContext: evaluationCtx)
103
+ let client = api. getClient ( )
104
+
105
+ _ = client. getBooleanDetails ( key: " my-flag " , defaultValue: false )
106
+ _ = client. getBooleanDetails ( key: " my-flag " , defaultValue: false )
107
+ _ = client. getIntegerDetails ( key: " int-flag " , defaultValue: 1 )
108
+ _ = client. getDoubleDetails ( key: " double-flag " , defaultValue: 1.0 )
109
+ _ = client. getStringDetails ( key: " string-flag " , defaultValue: " default " )
110
+ _ = client. getObjectDetails ( key: " object-flag " , defaultValue: Value . null)
111
+
112
+ let expectation = self . expectation ( description: " Waiting for delay " )
113
+
114
+ DispatchQueue . global ( ) . asyncAfter ( deadline: . now( ) + 2.0 ) { expectation. fulfill ( ) }
115
+ await fulfillment ( of: [ expectation] , timeout: 3.0 )
116
+
117
+ XCTAssertEqual ( 1 , mockNetworkService. dataCollectorCallCounter)
118
+ XCTAssertEqual ( 6 , mockNetworkService. dataCollectorEventCounter)
119
+
120
+ do {
121
+ let httpBodyCollector = mockNetworkService. requests [ mockNetworkService. requests. count - 1 ] . httpBody!
122
+ let decodedStruct = try JSONDecoder ( ) . decode ( DataCollectorRequest . self, from: httpBodyCollector)
123
+ let want = [
124
+ " openfeature " : ExporterMetadataValue . bool ( true ) ,
125
+ " provider " : ExporterMetadataValue . string ( " swift " )
126
+ ] as? [ String : ExporterMetadataValue ]
127
+ XCTAssertEqual ( want, decodedStruct. meta)
128
+ } catch {
129
+ XCTFail ( " Error deserializing: \( error) " )
130
+ }
131
+ }
43
132
44
133
func testProviderMultipleHookCall( ) async {
45
134
let mockNetworkService = MockNetworkingService ( mockStatus: 200 )
0 commit comments