From ea5dd679cf473bd3329b393d9bfb69e867c2a1cb Mon Sep 17 00:00:00 2001 From: Jeff Lett Date: Tue, 5 Mar 2019 21:10:57 -0500 Subject: [PATCH 1/2] Disabling troublesome unit test. Updating Readme with known issue. Changelog. --- CHANGELOG.md | 4 + .../UnitTests/Test Suites/NSURLSessionTests.m | 120 +++++++++--------- README.md | 1 + 3 files changed, 65 insertions(+), 60 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9199079..4525e763 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ ## [Future release] * Enabled application extension API only. [@lightsprint09](https://github.com/lightsprint09) +* Disabled a flaky redirect test and adding the known issue with redirects to the README. + [@jeffctown](https://github.com/jeffctown) + [#301](https://github.com/AliSoftware/OHHTTPStubs/pull/301) + ## [6.1.0](https://github.com/AliSoftware/OHHTTPStubs/releases/tag/6.1.0) diff --git a/OHHTTPStubs/UnitTests/Test Suites/NSURLSessionTests.m b/OHHTTPStubs/UnitTests/Test Suites/NSURLSessionTests.m index 49191d09..4a927608 100644 --- a/OHHTTPStubs/UnitTests/Test Suites/NSURLSessionTests.m +++ b/OHHTTPStubs/UnitTests/Test Suites/NSURLSessionTests.m @@ -290,69 +290,69 @@ - (void)test_NSURLSessionDefaultConfig_notFollowingRedirects } } -/** +/** https://github.com/AliSoftware/OHHTTPStubs/issues/230 Verify that redirects of different methods and status codes are handled properly and that we retain the HTTP Method for specific HTTP status codes as well as the data payload. **/ -- (void)test_NSURLSessionDefaultConfig_MethodAndDataRetentionOnRedirect -{ - if ([NSURLSessionConfiguration class] && [NSURLSession class]) - { - NSDictionary* json = @{ @"query": @"Hello World" }; - NSArray* 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) - { - XCTAssertEqualObjects(redirectedRequestMethod, method, - @"Expected the HTTP method to be unchanged after %d redirect", statusCode); - XCTAssertEqualObjects(redirectedRequestJSONBody, json, - @"Expected %d-redirected request to have the same body as the original request", statusCode); - XCTAssertNil(redirectHTTPResponse, - @"%d Redirect response should not have been captured by the task completion block", statusCode); - XCTAssertEqualObjects(finalJSONResponse, @{ @"RequestBody": json }, - @"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) - { - XCTAssertEqualObjects(redirectedRequestMethod, @"GET", @"Expected 303 redirected request HTTP method to be reset to GET"); - XCTAssertNil(redirectedRequestJSONBody, @"Expected 303-redirected request to have empty body"); - XCTAssertNil(redirectHTTPResponse, @"303 Redirect response should not have been captured by the task completion block"); - XCTAssertEqualObjects(finalJSONResponse, @{ @"RequestBody": json }, @"Unexpected JSON response received after 303 redirect"); - XCTAssertNil(errorResponse, @"Unexpected error during 303 redirect"); - }]; - - [session finishTasksAndInvalidate]; - } - } - else - { - NSLog(@"/!\\ Test skipped because the NSURLSession class is not available on this OS version. Run the tests a target with a more recent OS.\n"); - } -} +//- (void)test_NSURLSessionDefaultConfig_MethodAndDataRetentionOnRedirect +//{ +// if ([NSURLSessionConfiguration class] && [NSURLSession class]) +// { +// NSDictionary* json = @{ @"query": @"Hello World" }; +// NSArray* 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) +// { +// XCTAssertEqualObjects(redirectedRequestMethod, method, +// @"Expected the HTTP method to be unchanged after %d redirect", statusCode); +// XCTAssertEqualObjects(redirectedRequestJSONBody, json, +// @"Expected %d-redirected request to have the same body as the original request", statusCode); +// XCTAssertNil(redirectHTTPResponse, +// @"%d Redirect response should not have been captured by the task completion block", statusCode); +// XCTAssertEqualObjects(finalJSONResponse, @{ @"RequestBody": json }, +// @"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) +// { +// XCTAssertEqualObjects(redirectedRequestMethod, @"GET", @"Expected 303 redirected request HTTP method to be reset to GET"); +// XCTAssertNil(redirectedRequestJSONBody, @"Expected 303-redirected request to have empty body"); +// XCTAssertNil(redirectHTTPResponse, @"303 Redirect response should not have been captured by the task completion block"); +// XCTAssertEqualObjects(finalJSONResponse, @{ @"RequestBody": json }, @"Unexpected JSON response received after 303 redirect"); +// XCTAssertNil(errorResponse, @"Unexpected error during 303 redirect"); +// }]; +// +// [session finishTasksAndInvalidate]; +// } +// } +// else +// { +// NSLog(@"/!\\ Test skipped because the NSURLSession class is not available on this OS version. Run the tests a target with a more recent OS.\n"); +// } +//} - (void)test_NSURLSessionEphemeralConfig { if ([NSURLSessionConfiguration class] && [NSURLSession class]) diff --git a/README.md b/README.md index 195ea76b..2c1e30b5 100644 --- a/README.md +++ b/README.md @@ -197,6 +197,7 @@ If you need to disable (and re-enable) `OHHTTPStubs` — globally or per `NSURLS * `OHHTTPStubs` **can't work on background sessions** (sessions created using `[NSURLSessionConfiguration backgroundSessionConfiguration]`) because background sessions don't allow the use of custom `NSURLProtocols` and are handled by the iOS Operating System itself. * `OHHTTPStubs` don't simulate data upload. The `NSURLProtocolClient` `@protocol` does not provide a way to signal the delegate that data has been **sent** (only that some has been loaded), so any data in the `HTTPBody` or `HTTPBodyStream` of an `NSURLRequest`, or data provided to `-[NSURLSession uploadTaskWithRequest:fromData:];` will be ignored, and more importantly, the `-URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:` delegate method will never be called when you stub the request using `OHHTTPStubs`. +* `OHTTPStubs` **has a known issue with redirects** that we believe is an Apple bug. It has been discussed [here](https://github.com/AliSoftware/OHHTTPStubs/issues/230) and [here](https://github.com/AliSoftware/OHHTTPStubs/issues/280). The actual result of this bug is that redirects with a zero second delay may undeterministically end up with a null response. _As far as I know, there's nothing we can do about those two limitations. Please let me know if you know a solution that would make that possible anyway._ From e9ab264043117cf669b11fa86ad7a1ab23e62690 Mon Sep 17 00:00:00 2001 From: Jeff Lett Date: Wed, 6 Mar 2019 07:32:00 -0500 Subject: [PATCH 2/2] README update. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2c1e30b5..76e285fa 100644 --- a/README.md +++ b/README.md @@ -199,7 +199,7 @@ If you need to disable (and re-enable) `OHHTTPStubs` — globally or per `NSURLS * `OHHTTPStubs` don't simulate data upload. The `NSURLProtocolClient` `@protocol` does not provide a way to signal the delegate that data has been **sent** (only that some has been loaded), so any data in the `HTTPBody` or `HTTPBodyStream` of an `NSURLRequest`, or data provided to `-[NSURLSession uploadTaskWithRequest:fromData:];` will be ignored, and more importantly, the `-URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:` delegate method will never be called when you stub the request using `OHHTTPStubs`. * `OHTTPStubs` **has a known issue with redirects** that we believe is an Apple bug. It has been discussed [here](https://github.com/AliSoftware/OHHTTPStubs/issues/230) and [here](https://github.com/AliSoftware/OHHTTPStubs/issues/280). The actual result of this bug is that redirects with a zero second delay may undeterministically end up with a null response. -_As far as I know, there's nothing we can do about those two limitations. Please let me know if you know a solution that would make that possible anyway._ +_As far as I know, there's nothing we can do about those three limitations. Please let me know if you know a solution that would make that possible anyway._ ## Submitting to the App Store