forked from launchdarkly/ios-client-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLDURLCacheTest.m
287 lines (224 loc) · 11.9 KB
/
LDURLCacheTest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//
// LDURLCacheTest.m
// DarklyTests
//
// Created by Mark Pokorny on 11/16/18. +JMJ
// Copyright © 2018 LaunchDarkly. All rights reserved.
//
#import "DarklyXCTestCase.h"
#import "LDURLCache.h"
#import "OCMock.h"
#import "LDUserModel+Testable.h"
#import "LDRequestManager.h"
static NSString *const testMobileKey = @"testMobileKey";
@interface NSURLRequest (LDURLCacheTest)
@end
@implementation NSURLRequest (LDURLCacheTest)
-(BOOL)hasPropertiesMatchingRequest:(NSURLRequest*)otherRequest {
return [self.URL.scheme isEqualToString:otherRequest.URL.scheme]
&& [self.URL.host isEqualToString:otherRequest.URL.host]
&& [self.URL.path isEqualToString:otherRequest.URL.path]
&& [self.HTTPMethod isEqualToString:otherRequest.HTTPMethod]
&& ((self.HTTPBody != nil && [self.HTTPBody isEqualToData:otherRequest.HTTPBody]) || (self.HTTPBody == nil && otherRequest.HTTPBody == nil))
&& self.timeoutInterval == otherRequest.timeoutInterval
&& self.cachePolicy == otherRequest.cachePolicy
&& [self.allHTTPHeaderFields isEqualToDictionary:otherRequest.allHTTPHeaderFields];
}
@end
@interface LDRequestManager (LDURLCacheTest)
-(NSURLRequest*)flagRequestUsingReportMethodForUser:(LDUserModel*)user;
-(NSURLRequest*)flagRequestUsingGetMethodForUser:(LDUserModel*)user;
@end
@interface LDURLCache (LDURLCacheTest)
@property (nonatomic, strong) NSURLCache *baseUrlCache;
@end
@implementation LDURLCache (LDURLCacheTest)
@dynamic baseUrlCache;
@end
@interface LDURLCacheTest : DarklyXCTestCase
@property (strong, nonatomic) id nsUrlCacheMock;
@property (nonatomic, strong) LDConfig *config;
@property (nonatomic, strong) LDUserModel *user;
@property (nonatomic, strong) LDRequestManager *requestManager;
@property (nonatomic, strong) NSCachedURLResponse *cachedResponseStub;
@property (strong, nonatomic) LDURLCache *urlCache;
@end
@implementation LDURLCacheTest
-(void)setUp {
[super setUp];
self.config = [[LDConfig alloc] initWithMobileKey:testMobileKey];
self.config.streaming = NO;
self.config.useReport = YES;
self.user = [LDUserModel stubWithKey:[[NSUUID UUID] UUIDString]];
self.requestManager = [LDRequestManager requestManagerForMobileKey:testMobileKey config:self.config delegate:nil callbackQueue:nil];
self.cachedResponseStub = [[NSCachedURLResponse alloc] init];
self.nsUrlCacheMock = [OCMockObject niceMockForClass:[NSURLCache class]];
(void)[[[[self.nsUrlCacheMock stub] andReturn:self.nsUrlCacheMock] ignoringNonObjectArgs] initWithMemoryCapacity:0 diskCapacity:0 diskPath:[OCMArg any]];
self.urlCache = (LDURLCache*)[LDURLCache urlCacheForConfig:self.config usingCache:self.nsUrlCacheMock];
}
-(void)tearDown {
[super tearDown];
}
-(void)testUrlCacheForConfigUsingCache_streaming_get {
self.config.streaming = YES;
self.config.useReport = NO;
id urlCache = [LDURLCache urlCacheForConfig:self.config usingCache:self.nsUrlCacheMock];
XCTAssertEqualObjects(urlCache, self.nsUrlCacheMock);
}
-(void)testUrlCacheForConfigUsingCache_streaming_report {
self.config.streaming = YES;
self.config.useReport = YES;
id urlCache = [LDURLCache urlCacheForConfig:self.config usingCache:self.nsUrlCacheMock];
XCTAssertEqualObjects(urlCache, self.nsUrlCacheMock);
}
-(void)testUrlCacheForConfigUsingCache_polling_get {
self.config.streaming = NO;
self.config.useReport = NO;
id urlCache = [LDURLCache urlCacheForConfig:self.config usingCache:self.nsUrlCacheMock];
XCTAssertEqualObjects(urlCache, self.nsUrlCacheMock);
}
-(void)testUrlCacheForConfigUsingCache_polling_report {
self.config.streaming = NO;
self.config.useReport = YES;
id urlCache = [LDURLCache urlCacheForConfig:self.config usingCache:self.nsUrlCacheMock];
XCTAssertNotEqualObjects(urlCache, self.nsUrlCacheMock);
XCTAssertEqual([urlCache class], [LDURLCache class]);
}
-(void)testUrlCacheForConfigUsingCache_missingBaseCache {
self.config.streaming = NO;
self.config.useReport = YES;
NSURLCache *missingCache;
id urlCache = [LDURLCache urlCacheForConfig:self.config usingCache:missingCache];
XCTAssertNil(urlCache);
}
-(void)testShouldUseLDURLCacheForConfig_streaming_get {
self.config.streaming = YES;
self.config.useReport = NO;
XCTAssertFalse([LDURLCache shouldUseLDURLCacheForConfig:self.config]);
}
-(void)testShouldUseLDURLCacheForConfig_streaming_report {
self.config.streaming = YES;
self.config.useReport = YES;
XCTAssertFalse([LDURLCache shouldUseLDURLCacheForConfig:self.config]);
}
-(void)testShouldUseLDURLCacheForConfig_polling_get {
self.config.streaming = NO;
self.config.useReport = NO;
XCTAssertFalse([LDURLCache shouldUseLDURLCacheForConfig:self.config]);
}
-(void)testShouldUseLDURLCacheForConfig_polling_report {
self.config.streaming = NO;
self.config.useReport = YES;
XCTAssertTrue([LDURLCache shouldUseLDURLCacheForConfig:self.config]);
}
-(void)testStoreCachedResponseForDataTask {
NSURLRequest *reportRequest = [self.requestManager flagRequestUsingReportMethodForUser:self.user];
NSURLSessionDataTask *reportDataTask = [[NSURLSession sharedSession] dataTaskWithRequest:reportRequest
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//Empty because its required, but this should never be executed
}];
NSURLRequest *getRequest = [self.requestManager flagRequestUsingGetMethodForUser:self.user];
[[self.nsUrlCacheMock expect] storeCachedResponse:self.cachedResponseStub forRequest:[OCMArg checkWithBlock:^BOOL(id obj) {
if(![obj isKindOfClass:[NSURLRequest class]]) {
return NO;
}
NSURLRequest *request = obj;
return [request hasPropertiesMatchingRequest:getRequest];
}]];
[self.urlCache storeCachedResponse:self.cachedResponseStub forDataTask:reportDataTask];
[self.nsUrlCacheMock verify];
}
-(void)testStoreCachedResponseForDataTask_getDataTask {
NSURLRequest *getRequest = [self.requestManager flagRequestUsingGetMethodForUser:self.user];
NSURLSessionDataTask *getDataTask = [[NSURLSession sharedSession] dataTaskWithRequest:getRequest
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//Empty because its required, but this should never be executed
}];
[[self.nsUrlCacheMock expect] storeCachedResponse:self.cachedResponseStub forDataTask:getDataTask];
[self.urlCache storeCachedResponse:self.cachedResponseStub forDataTask:getDataTask];
[self.nsUrlCacheMock verify];
}
-(void)testStoreCachedResponseForRequest {
NSURLRequest *reportRequest = [self.requestManager flagRequestUsingReportMethodForUser:self.user];
NSURLRequest *getRequest = [self.requestManager flagRequestUsingGetMethodForUser:self.user];
[[self.nsUrlCacheMock expect] storeCachedResponse:self.cachedResponseStub forRequest:[OCMArg checkWithBlock:^BOOL(id obj) {
if(![obj isKindOfClass:[NSURLRequest class]]) {
return NO;
}
NSURLRequest *request = obj;
return [request hasPropertiesMatchingRequest:getRequest];
}]];
[self.urlCache storeCachedResponse:self.cachedResponseStub forRequest:reportRequest];
[self.nsUrlCacheMock verify];
}
-(void)testStoreCachedResponseForRequest_getRequest {
NSURLRequest *getRequest = [self.requestManager flagRequestUsingGetMethodForUser:self.user];
[[self.nsUrlCacheMock expect] storeCachedResponse:self.cachedResponseStub forRequest:getRequest]; //pass the original request through, it wasn't a REPORT
[self.urlCache storeCachedResponse:self.cachedResponseStub forRequest:getRequest];
[self.nsUrlCacheMock verify];
}
-(void)testCachedResponseForRequest {
NSURLRequest *reportRequest = [self.requestManager flagRequestUsingReportMethodForUser:self.user];
NSURLRequest *getRequest = [self.requestManager flagRequestUsingGetMethodForUser:self.user];
[[[self.nsUrlCacheMock expect] andReturn:self.cachedResponseStub] cachedResponseForRequest:[OCMArg checkWithBlock:^BOOL(id obj) {
if(![obj isKindOfClass:[NSURLRequest class]]) {
return NO;
}
NSURLRequest *request = obj;
return [request hasPropertiesMatchingRequest:getRequest];
}]];
NSCachedURLResponse *cachedResponse = [self.urlCache cachedResponseForRequest:reportRequest];
XCTAssertEqualObjects(cachedResponse, self.cachedResponseStub);
[self.nsUrlCacheMock verify];
}
-(void)testCachedResponseForRequest_getRequest {
NSURLRequest *getRequest = [self.requestManager flagRequestUsingGetMethodForUser:self.user];
[[[self.nsUrlCacheMock expect] andReturn:self.cachedResponseStub] cachedResponseForRequest:getRequest];
NSCachedURLResponse *cachedResponse = [self.urlCache cachedResponseForRequest:getRequest];
XCTAssertEqualObjects(cachedResponse, self.cachedResponseStub);
[self.nsUrlCacheMock verify];
}
-(void)testGetCachedResponseForDataTask {
NSURLRequest *reportRequest = [self.requestManager flagRequestUsingReportMethodForUser:self.user];
NSURLSessionDataTask *reportDataTask = [[NSURLSession sharedSession] dataTaskWithRequest:reportRequest
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//Empty because its required, but this should never be executed
}];
NSURLRequest *getRequest = [self.requestManager flagRequestUsingGetMethodForUser:self.user];
[[[self.nsUrlCacheMock expect] andReturn:self.cachedResponseStub] cachedResponseForRequest:[OCMArg checkWithBlock:^BOOL(id obj) {
if(![obj isKindOfClass:[NSURLRequest class]]) {
return NO;
}
NSURLRequest *request = obj;
return [request hasPropertiesMatchingRequest:getRequest];
}]];
XCTestExpectation *responseExpectation = [self expectationWithDescription:[NSString stringWithFormat:@"%@.%@.responseExpectation",
NSStringFromClass([self class]), NSStringFromSelector(_cmd)]];
__block NSCachedURLResponse *reportedResponse;
[self.urlCache getCachedResponseForDataTask:reportDataTask completionHandler:^(NSCachedURLResponse * _Nonnull cachedResponse) {
reportedResponse = cachedResponse;
[responseExpectation fulfill];
}];
[self waitForExpectations:@[responseExpectation] timeout:1.0];
XCTAssertEqualObjects(reportedResponse, self.cachedResponseStub);
[self.nsUrlCacheMock verify];
}
-(void)testGetCachedResponseForDataTask_getDataTask {
NSURLRequest *getRequest = [self.requestManager flagRequestUsingGetMethodForUser:self.user];
NSURLSessionDataTask *getDataTask = [[NSURLSession sharedSession] dataTaskWithRequest:getRequest
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//Empty because its required, but this should never be executed
}];
[[self.nsUrlCacheMock expect] getCachedResponseForDataTask:getDataTask completionHandler:[OCMArg invokeBlockWithArgs:self.cachedResponseStub, nil]];
XCTestExpectation *responseExpectation = [self expectationWithDescription:[NSString stringWithFormat:@"%@.%@.responseExpectation",
NSStringFromClass([self class]), NSStringFromSelector(_cmd)]];
__block NSCachedURLResponse *reportedResponse;
[self.urlCache getCachedResponseForDataTask:getDataTask completionHandler:^(NSCachedURLResponse * _Nonnull cachedResponse) {
reportedResponse = cachedResponse;
[responseExpectation fulfill];
}];
[self waitForExpectations:@[responseExpectation] timeout:1.0];
XCTAssertEqualObjects(reportedResponse, self.cachedResponseStub);
[self.nsUrlCacheMock verify];
}
@end