Skip to content

Commit

Permalink
Use distinct sessions for parallel loop tests to secure results
Browse files Browse the repository at this point in the history
  • Loading branch information
AliSoftware committed Nov 19, 2017
1 parent dd23bc5 commit 6bf1982
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions OHHTTPStubs/UnitTests/Test Suites/NSURLSessionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -296,20 +296,20 @@ - (void)test_NSURLSessionDefaultConfig_notFollowingRedirects
**/
- (void)test_NSURLSessionDefaultConfig_MethodAndDataRetentionOnRedirect
{
NSURLSessionTestDelegate* delegate = [NSURLSessionTestDelegate delegateFollowingRedirects:YES fulfillOnCompletion:nil];

if ([NSURLSessionConfiguration class] && [NSURLSession class])
{
NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:delegate delegateQueue:nil];

NSDictionary* json = @{ @"query": @"Hello World" };
NSArray<NSString*>* allMethods = @[@"GET", @"HEAD", @"POST", @"PATCH", @"PUT"];

/** 301, 302, 307, 308: GET, HEAD, POST, PATCH, PUT should all maintain HTTP method and body unchanged **/
for (NSNumber* redirectStatusCode in @[@301, @302, @307, @308]) {
int statusCode = redirectStatusCode.intValue;
for (NSString* method in allMethods) {

NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSessionTestDelegate* delegate = [NSURLSessionTestDelegate delegateFollowingRedirects:YES fulfillOnCompletion:nil];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:delegate delegateQueue:nil];

[self _test_redirect_NSURLSession:session httpMethod:method jsonBody:json delays:0.0 redirectStatusCode:statusCode
completion:^(NSString *redirectedRequestMethod, id redirectedRequestJSONBody, NSHTTPURLResponse *redirectHTTPResponse, id finalJSONResponse, NSError *errorResponse)
{
Expand All @@ -323,11 +323,18 @@ - (void)test_NSURLSessionDefaultConfig_MethodAndDataRetentionOnRedirect
@"Unexpected JSON response received after %d redirect", statusCode);
XCTAssertNil(errorResponse, @"Unexpected error during %d redirect", statusCode);
}];

[session finishTasksAndInvalidate];
}
}

/** 303: GET, HEAD, POST, PATCH, PUT should use a GET HTTP method after redirection and not forward the body **/
for (NSString* method in allMethods) {

NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSessionTestDelegate* delegate = [NSURLSessionTestDelegate delegateFollowingRedirects:YES fulfillOnCompletion:nil];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:delegate delegateQueue:nil];

[self _test_redirect_NSURLSession:session httpMethod:method jsonBody:json delays:0.0 redirectStatusCode:303
completion:^(NSString *redirectedRequestMethod, id redirectedRequestJSONBody, NSHTTPURLResponse *redirectHTTPResponse, id finalJSONResponse, NSError *errorResponse)
{
Expand All @@ -337,9 +344,9 @@ - (void)test_NSURLSessionDefaultConfig_MethodAndDataRetentionOnRedirect
XCTAssertEqualObjects(finalJSONResponse, @{ @"RequestBody": json }, @"Unexpected JSON response received after 303 redirect");
XCTAssertNil(errorResponse, @"Unexpected error during 303 redirect");
}];
}

[session finishTasksAndInvalidate];
[session finishTasksAndInvalidate];
}
}
else
{
Expand Down

0 comments on commit 6bf1982

Please sign in to comment.