15
15
***************************************************************************/
16
16
17
17
#import < XCTest/XCTest.h>
18
+ #import < OHHTTPStubs/OHHTTPStubs.h>
18
19
#import " OPTLYTestHelper.h"
19
20
21
+ #import < OptimizelySDKCore/OPTLYProjectConfig.h>
20
22
#import " OPTLYManager.h"
21
23
#import " OPTLYClient.h"
22
24
23
25
// static datafile name
24
26
static NSString *const kDefaultDatafileFileName = @" datafile_6372300739" ;
25
27
static NSString *const kProjectId = @" 6372300739" ;
28
+ static NSString *const kAlternateDatafilename = @" validator_whitelisting_test_datafile" ;
26
29
static NSData *kDefaultDatafile ;
30
+ static NSData *kAlternateDatafile ;
27
31
28
32
@interface OPTLYManagerTest : XCTestCase
29
33
@@ -34,6 +38,126 @@ @implementation OPTLYManagerTest
34
38
+ (void )setUp {
35
39
[super setUp ];
36
40
kDefaultDatafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject: kDefaultDatafileFileName ];
41
+ kAlternateDatafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject: kAlternateDatafilename ];
37
42
}
38
43
44
+ - (void )testInitializeClientWithoutDatafileReturnsDummy {
45
+ // initialize manager without datafile
46
+ OPTLYManager *manager = [OPTLYManager initWithBuilderBlock: ^(OPTLYManagerBuilder * _Nullable builder) {
47
+ builder.projectId = kProjectId ;
48
+ }];
49
+
50
+ // make sure the manager is initialized correctly
51
+ XCTAssertNotNil (manager);
52
+ XCTAssertNotNil (manager.projectId );
53
+ XCTAssertNil (manager.datafile );
54
+
55
+ // try to initialize client
56
+ OPTLYClient *client = [manager initializeClient ];
57
+
58
+ // make sure we get a dummy client back
59
+ XCTAssertNotNil (client);
60
+ XCTAssertNil (client.optimizely );
61
+ XCTAssertNotNil (client.logger );
62
+ }
63
+
64
+ - (void )testInitializeClientWithDefaults {
65
+ // initialize manager
66
+ OPTLYManager *manager = [OPTLYManager initWithBuilderBlock: ^(OPTLYManagerBuilder * _Nullable builder) {
67
+ builder.datafile = kDefaultDatafile ;
68
+ builder.projectId = kProjectId ;
69
+ }];
70
+
71
+ // make sure manager is initialized correctly
72
+ XCTAssertNotNil (manager);
73
+ XCTAssertNotNil (manager.datafile );
74
+ XCTAssertEqual (manager.datafile , kDefaultDatafile );
75
+
76
+ // initialize client
77
+ OPTLYClient *client = [manager initializeClient ];
78
+
79
+ // test client initialization
80
+ XCTAssertNotNil (client);
81
+ XCTAssertNotNil (client.optimizely );
82
+ XCTAssertNotNil (client.logger );
83
+
84
+ [self checkConfigIsUsingDefaultDatafile: client.optimizely.config];
85
+ }
86
+
87
+ - (void )testInitializeClientWithCustomDatafile {
88
+ // initialize manager
89
+ OPTLYManager *manager = [OPTLYManager initWithBuilderBlock: ^(OPTLYManagerBuilder * _Nullable builder) {
90
+ builder.datafile = kDefaultDatafile ;
91
+ builder.projectId = kProjectId ;
92
+ }];
93
+
94
+ // make sure manager is initialized correctly
95
+ XCTAssertNotNil (manager);
96
+ XCTAssertNotNil (manager.datafile );
97
+ XCTAssertEqual (manager.datafile , kDefaultDatafile );
98
+
99
+ // initialize client
100
+ OPTLYClient *client = [manager initializeClientWithDatafile: kAlternateDatafile ];
101
+
102
+ // test client initialization
103
+ XCTAssertNotNil (client);
104
+ XCTAssertNotNil (client.optimizely );
105
+ XCTAssertNotNil (client.logger );
106
+
107
+ [self checkConfigIsUsingAlternativeDatafile: client.optimizely.config];
108
+ }
109
+
110
+ - (void )testInitializeClientAsync {
111
+ // initialize manager
112
+ OPTLYManager *manager = [OPTLYManager initWithBuilderBlock: ^(OPTLYManagerBuilder * _Nullable builder) {
113
+ builder.datafile = kDefaultDatafile ;
114
+ builder.projectId = kProjectId ;
115
+ }];
116
+
117
+ // make sure manager is initialized correctly
118
+ XCTAssertNotNil (manager);
119
+ XCTAssertNotNil (manager.datafile );
120
+ XCTAssertEqual (manager.datafile , kDefaultDatafile );
121
+
122
+ // stub network call
123
+ [OHHTTPStubs stubRequestsPassingTest: ^BOOL (NSURLRequest *request) {
124
+ return [request.URL.host isEqualToString: @" cdn.optimizely.com" ];
125
+ } withStubResponse: ^OHHTTPStubsResponse*(NSURLRequest *request) {
126
+ // Stub it with our "wsresponse.json" stub file (which is in same bundle as self)
127
+ return [OHHTTPStubsResponse responseWithData: kAlternateDatafile
128
+ statusCode: 200
129
+ headers: @{@" Content-Type" :@" application/json" }];
130
+ }];
131
+
132
+ // setup async expectation
133
+ __weak XCTestExpectation *expectation = [self expectationWithDescription: @" testInitializeClientAsync" ];
134
+ // initialize client
135
+ [manager initializeClientWithCallback: ^(NSError * _Nullable error, OPTLYClient * _Nullable client) {
136
+
137
+ // check client in callback
138
+ XCTAssertNotNil (client);
139
+ XCTAssertNotNil (client.optimizely , @" Client needs to have an optimizely instance" );
140
+ XCTAssertNotNil (client.logger );
141
+ [self checkConfigIsUsingAlternativeDatafile: client.optimizely.config];
142
+ [expectation fulfill ];
143
+ }];
144
+
145
+ // wait for async start to finish
146
+ [self waitForExpectationsWithTimeout: 2 handler: nil ];
147
+ }
148
+
149
+ - (void )checkConfigIsUsingDefaultDatafile : (OPTLYProjectConfig *)config {
150
+ XCTAssertEqualObjects (config.revision , @" 58" );
151
+ XCTAssertEqualObjects (config.projectId , @" 6372300739" );
152
+ XCTAssertEqualObjects (config.accountId , @" 6365361536" );
153
+ }
154
+
155
+ - (void )checkConfigIsUsingAlternativeDatafile : (OPTLYProjectConfig *)config {
156
+ XCTAssertEqualObjects (config.revision , @" 6" );
157
+ XCTAssertEqualObjects (config.projectId , @" 7519590183" );
158
+ XCTAssertEqualObjects (config.accountId , @" 3244610124" );
159
+ }
160
+
161
+
162
+
39
163
@end
0 commit comments