Skip to content

Commit

Permalink
Fix main thread stalling (again).
Browse files Browse the repository at this point in the history
Catching exceptions was not such a good idea. Now when [OHHTTPStubsResponse initWithFileAtPath:statusCode:headers:] is passed a nil filePath, we return an empty body and log some warning. More consistant.
  • Loading branch information
AliSoftware committed Sep 30, 2014
1 parent b1d68fd commit 4a3f9b9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 18 deletions.
10 changes: 1 addition & 9 deletions OHHTTPStubs/Sources/OHHTTPStubs.m
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,7 @@ - (void)startLoading
return;
}

OHHTTPStubsResponse* responseStub;
@try {
responseStub = self.stub.responseBlock(request);
}
@catch (NSException *exception) {
NSError* err = [NSError errorWithDomain:@"OHHTTPStubs" code:500 userInfo:@{@"NSException": exception}];
[client URLProtocol:self didFailWithError:err];
@throw exception;
}
OHHTTPStubsResponse* responseStub = self.stub.responseBlock(request);

if (OHHTTPStubs.sharedInstance.onStubActivationBlock)
{
Expand Down
12 changes: 10 additions & 2 deletions OHHTTPStubs/Sources/OHHTTPStubsResponse.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,16 @@ -(instancetype)initWithFileAtPath:(NSString*)filePath
statusCode:(int)statusCode
headers:(NSDictionary*)httpHeaders
{
NSParameterAssert(filePath != nil);
NSInputStream* inputStream = [NSInputStream inputStreamWithFileAtPath:filePath];
NSInputStream* inputStream;
if (filePath)
{
inputStream = [NSInputStream inputStreamWithFileAtPath:filePath];
}
else
{
NSLog(@"%s: nil file path. Returning empty data", __PRETTY_FUNCTION__);
inputStream = [NSInputStream inputStreamWithData:[NSData data]];
}

NSDictionary* attributes = [NSFileManager.defaultManager attributesOfItemAtPath:filePath error:nil];
unsigned long long fileSize = [[attributes valueForKey:NSFileSize] unsignedLongLongValue];
Expand Down
2 changes: 1 addition & 1 deletion OHHTTPStubs/UnitTests/AFNetworking
Submodule AFNetworking updated 43 files
+7 −0 .cocoadocs.yml
+5 −3 AFNetworking.podspec
+0 −1 AFNetworking/AFHTTPRequestOperation.h
+10 −6 AFNetworking/AFHTTPRequestOperation.m
+18 −4 AFNetworking/AFHTTPRequestOperationManager.h
+16 −3 AFNetworking/AFHTTPRequestOperationManager.m
+1 −1 AFNetworking/AFHTTPSessionManager.h
+80 −89 AFNetworking/AFHTTPSessionManager.m
+4 −8 AFNetworking/AFNetworkReachabilityManager.h
+14 −1 AFNetworking/AFNetworkReachabilityManager.m
+42 −20 AFNetworking/AFSecurityPolicy.m
+10 −39 AFNetworking/AFURLConnectionOperation.h
+26 −25 AFNetworking/AFURLConnectionOperation.m
+59 −21 AFNetworking/AFURLRequestSerialization.h
+104 −37 AFNetworking/AFURLRequestSerialization.m
+44 −4 AFNetworking/AFURLResponseSerialization.h
+67 −43 AFNetworking/AFURLResponseSerialization.m
+21 −4 AFNetworking/AFURLSessionManager.h
+113 −59 AFNetworking/AFURLSessionManager.m
+206 −0 CHANGES
+2 −0 CONTRIBUTING.md
+6 −2 Example/AFNetworking iOS Example.xcodeproj/project.pbxproj
+1 −1 Example/Classes/AFAppDotNetAPIClient.m
+2 −2 Example/Classes/Models/User.h
+43 −38 Example/Classes/Models/User.m
+ Example/adn.cer
+18 −26 README.md
+24 −6 Tests/AFNetworking Tests.xcodeproj/project.pbxproj
+ Tests/Resources/ADN.net/ADNNetServerTrustChain/adn_0.cer
+25 −56 Tests/Tests/AFHTTPRequestOperationTests.m
+64 −59 Tests/Tests/AFHTTPRequestSerializationTests.m
+68 −0 Tests/Tests/AFHTTPResponseSerializationTests.m
+98 −0 Tests/Tests/AFHTTPSessionManagerTests.m
+43 −34 Tests/Tests/AFJSONSerializationTests.m
+100 −0 Tests/Tests/AFNetworkActivityManagerTests.m
+9 −3 Tests/Tests/AFPropertyListResponseSerializerTests.m
+82 −0 Tests/Tests/AFURLSessionManagerTests.m
+39 −6 UIKit+AFNetworking/UIButton+AFNetworking.h
+139 −62 UIKit+AFNetworking/UIButton+AFNetworking.m
+2 −4 UIKit+AFNetworking/UIImageView+AFNetworking.h
+10 −4 UIKit+AFNetworking/UIImageView+AFNetworking.m
+1 −0 UIKit+AFNetworking/UIKit+AFNetworking.h
+4 −4 UIKit+AFNetworking/UIProgressView+AFNetworking.m
5 changes: 3 additions & 2 deletions OHHTTPStubs/UnitTests/Test Suites/NSURLConnectionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ -(void)test_NSURLConnection_sendAsyncronousRequest_parallelQueue

-(void)_test_NSURLConnection_sendMultipleAsyncronousRequestsOnOperationQueue:(NSOperationQueue*)queue
{

BOOL testFinished = NO;
NSData* (^dataForRequest)(NSURLRequest*) = ^(NSURLRequest* req) {
return [[NSString stringWithFormat:@"<Response for URL %@>",req.URL.absoluteString] dataUsingEncoding:NSUTF8StringEncoding];
};
Expand Down Expand Up @@ -160,7 +160,7 @@ -(void)_test_NSURLConnection_sendMultipleAsyncronousRequestsOnOperationQueue:(NS
XCTAssertEqualObjects(data, dataForRequest(req), @"Invalid data response");
XCTAssertEqualWithAccuracy(-[startDate timeIntervalSinceNow], (responseTime*.1)+responseTime, kResponseTimeTolerence, @"Invalid response time");

[expectation fulfill];
if (!testFinished) [expectation fulfill];
}];
};

Expand All @@ -170,6 +170,7 @@ -(void)_test_NSURLConnection_sendMultipleAsyncronousRequestsOnOperationQueue:(NS
sendAsyncRequest(time1); // send this one last, should receive first

[self waitForExpectationsWithTimeout:MAX(time1,MAX(time2,time3))+kResponseTimeTolerence handler:nil];
testFinished = YES;
}

-(void)test_NSURLConnection_sendMultipleAsyncronousRequests_mainQueue
Expand Down
4 changes: 0 additions & 4 deletions OHHTTPStubs/UnitTests/Test Suites/NilValuesTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ - (void)test_NilPath
completionHandler:^(NSURLResponse* resp, NSData* data, NSError* error)
{
XCTAssertEqual(data.length, (NSUInteger)0, @"Data should be empty");
XCTAssertEqualObjects(error.domain, @"OHHTTPStubs");
XCTAssertEqual(error.code, 500);
NSException* ex = error.userInfo[ @"NSException"];
XCTAssertEqualObjects([ex description], @"Invalid parameter not satisfying: filePath != nil");

[expectation fulfill];
}];
Expand Down

0 comments on commit 4a3f9b9

Please sign in to comment.