Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix):When a ephemeral session configuration causes an exception, catch try background or return nil. #417

Merged
merged 3 commits into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions OptimizelySDKCore/OptimizelySDKCore/OPTLYHTTPRequestManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,34 @@ - (NSURLSession *)session;
@implementation OPTLYHTTPRequestManager

- (NSURLSession *)session {
NSURLSession *ephemeralSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]];
ephemeralSession.configuration.TLSMinimumSupportedProtocol = kTLSProtocol12;
NSURLSession *ephemeralSession = nil;

@try {
ephemeralSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]];
ephemeralSession.configuration.TLSMinimumSupportedProtocol = kTLSProtocol12;

}
@catch (NSException *e) {
OPTLYLogError(e.description);
//return self.backgroundSession;
}

return ephemeralSession;
}

- (NSURLSession *)backgroundSession {
NSURLSession *backgroundSession = nil;

@try {
backgroundSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"backgroundFlushEvent"]];
backgroundSession.configuration.TLSMinimumSupportedProtocol = kTLSProtocol12;
}
@catch (NSException *e) {
OPTLYLogError(e.description);
}
return backgroundSession;
}

# pragma mark - Object Initializers

- (id)init
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#import <XCTest/XCTest.h>
#import <OHHTTPStubs/OHHTTPStubs.h>
#import <OCMock/OCMock.h>
#import "OPTLYHTTPRequestManager.h"
#import "OPTLYTestHelper.h"

Expand Down Expand Up @@ -161,6 +162,33 @@ - (void)testPOSTWithParametersFailure
}];
}

- (void)testPOSTWithException
{


[self swizzleConfig];

OPTLYHTTPRequestManager *requestManager = [OPTLYHTTPRequestManager new];

XCTestExpectation *expectation = [self expectationWithDescription:@"Wait for POSTWithParameters to timeout."];
// we are not expecting this to be filled. the session should just disappear.
expectation.inverted = true;

[requestManager POSTWithParameters:self.parameters url:self.testURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// we never get here
[expectation fulfill];
}];

[self waitForExpectationsWithTimeout:5.0 handler:^(NSError *error) {
if (error) {
NSLog(@"Error Occurred: %@", error);
XCTAssert(false);
}
XCTAssert(true);
[self swizzleConfig];
}];
}

// Tests the following for the POST with backoff retry:
// 1. correct number of recursive calls
// 2. the right delays are set at each retry attempt
Expand Down Expand Up @@ -350,4 +378,20 @@ - (void)checkMaxRetries:(OPTLYHTTPRequestManager *)requestManager {
XCTAssertTrue(requestManager.retryAttemptTest == kRetryAttempts+1, @"Invalid number of retries.");
XCTAssertTrue([requestManager.delaysTest isEqualToArray:self.expectedDelays], @"Invalid delays set for backoff retry.");
}
- (void)swizzleConfig {
Method method = class_getClassMethod(NSURLSessionConfiguration.class, @selector(ephemeralSessionConfiguration));
Method swizzle_method = class_getClassMethod(NSURLSessionConfiguration.class, @selector(getEphemeral));

method_exchangeImplementations(method, swizzle_method);
}
@end

@interface NSURLSessionConfiguration(OPTLYSTubs)
+ (NSURLSessionConfiguration *)getEphemeral;
@end
@implementation NSURLSessionConfiguration(OPTLYSTubs)
+ (NSURLSessionConfiguration *)getEphemeral {
[NSException raise:@"problem" format:@"problem"];
return nil;
}
@end