Skip to content

Commit fd51628

Browse files
(fix):When a ephemeral session configuration causes an exception, catch try background or return nil. (#417)
* this needs to be tested and i think we should come up with some sort of test for this. * wrote a unit test and am not trying to create a background configuration which can cause a different exception. * remove redundant method
1 parent d0daf2e commit fd51628

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

OptimizelySDKCore/OptimizelySDKCore/OPTLYHTTPRequestManager.m

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,34 @@ - (NSURLSession *)session;
3838
@implementation OPTLYHTTPRequestManager
3939

4040
- (NSURLSession *)session {
41-
NSURLSession *ephemeralSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]];
42-
ephemeralSession.configuration.TLSMinimumSupportedProtocol = kTLSProtocol12;
41+
NSURLSession *ephemeralSession = nil;
42+
43+
@try {
44+
ephemeralSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]];
45+
ephemeralSession.configuration.TLSMinimumSupportedProtocol = kTLSProtocol12;
4346

47+
}
48+
@catch (NSException *e) {
49+
OPTLYLogError(e.description);
50+
//return self.backgroundSession;
51+
}
52+
4453
return ephemeralSession;
4554
}
4655

56+
- (NSURLSession *)backgroundSession {
57+
NSURLSession *backgroundSession = nil;
58+
59+
@try {
60+
backgroundSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"backgroundFlushEvent"]];
61+
backgroundSession.configuration.TLSMinimumSupportedProtocol = kTLSProtocol12;
62+
}
63+
@catch (NSException *e) {
64+
OPTLYLogError(e.description);
65+
}
66+
return backgroundSession;
67+
}
68+
4769
# pragma mark - Object Initializers
4870

4971
- (id)init

OptimizelySDKCore/OptimizelySDKCoreTests/OPTLYHTTPRequestManagerTest.m

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#import <XCTest/XCTest.h>
1818
#import <OHHTTPStubs/OHHTTPStubs.h>
19+
#import <OCMock/OCMock.h>
1920
#import "OPTLYHTTPRequestManager.h"
2021
#import "OPTLYTestHelper.h"
2122

@@ -161,6 +162,33 @@ - (void)testPOSTWithParametersFailure
161162
}];
162163
}
163164

165+
- (void)testPOSTWithException
166+
{
167+
168+
169+
[self swizzleConfig];
170+
171+
OPTLYHTTPRequestManager *requestManager = [OPTLYHTTPRequestManager new];
172+
173+
XCTestExpectation *expectation = [self expectationWithDescription:@"Wait for POSTWithParameters to timeout."];
174+
// we are not expecting this to be filled. the session should just disappear.
175+
expectation.inverted = true;
176+
177+
[requestManager POSTWithParameters:self.parameters url:self.testURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
178+
// we never get here
179+
[expectation fulfill];
180+
}];
181+
182+
[self waitForExpectationsWithTimeout:5.0 handler:^(NSError *error) {
183+
if (error) {
184+
NSLog(@"Error Occurred: %@", error);
185+
XCTAssert(false);
186+
}
187+
XCTAssert(true);
188+
[self swizzleConfig];
189+
}];
190+
}
191+
164192
// Tests the following for the POST with backoff retry:
165193
// 1. correct number of recursive calls
166194
// 2. the right delays are set at each retry attempt
@@ -350,4 +378,20 @@ - (void)checkMaxRetries:(OPTLYHTTPRequestManager *)requestManager {
350378
XCTAssertTrue(requestManager.retryAttemptTest == kRetryAttempts+1, @"Invalid number of retries.");
351379
XCTAssertTrue([requestManager.delaysTest isEqualToArray:self.expectedDelays], @"Invalid delays set for backoff retry.");
352380
}
381+
- (void)swizzleConfig {
382+
Method method = class_getClassMethod(NSURLSessionConfiguration.class, @selector(ephemeralSessionConfiguration));
383+
Method swizzle_method = class_getClassMethod(NSURLSessionConfiguration.class, @selector(getEphemeral));
384+
385+
method_exchangeImplementations(method, swizzle_method);
386+
}
387+
@end
388+
389+
@interface NSURLSessionConfiguration(OPTLYSTubs)
390+
+ (NSURLSessionConfiguration *)getEphemeral;
391+
@end
392+
@implementation NSURLSessionConfiguration(OPTLYSTubs)
393+
+ (NSURLSessionConfiguration *)getEphemeral {
394+
[NSException raise:@"problem" format:@"problem"];
395+
return nil;
396+
}
353397
@end

0 commit comments

Comments
 (0)