Ben Copsey

Prevent delegate auth from being used when running on main thread

Added a test for same
... ... @@ -1453,6 +1453,11 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
- (BOOL)askDelegateForProxyCredentials
{
// Can't use delegate authentication when running on the main thread
if ([NSThread isMainThread]) {
return NO;
}
// If we have a delegate, we'll see if it can handle proxyAuthenticationNeededForRequest:.
// Otherwise, we'll try the queue (if this request is part of one) and it will pass the message on to its own delegate
id authenticationDelegate = [self delegate];
... ... @@ -1635,6 +1640,11 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
- (BOOL)askDelegateForCredentials
{
// Can't use delegate authentication when running on the main thread
if ([NSThread isMainThread]) {
return NO;
}
// If we have a delegate, we'll see if it can handle proxyAuthenticationNeededForRequest:.
// Otherwise, we'll try the queue (if this request is part of one) and it will pass the message on to its own delegate
id authenticationDelegate = [self delegate];
... ...
... ... @@ -39,4 +39,5 @@
- (void)testTimeOutWithoutDownloadDelegate;
- (void)testThrottlingDownloadBandwidth;
- (void)testThrottlingUploadBandwidth;
- (void)testMainThreadDelegateAuthenticationFailure;
@end
... ...
... ... @@ -857,6 +857,28 @@
}
- (void)authenticationNeededForRequest:(ASIHTTPRequest *)request
{
GHAssertTrue(NO,@"Delegate asked for authentication when running on the main thread");
}
- (void)testMainThreadDelegateAuthenticationFailure
{
[ASIHTTPRequest clearSession];
//GHUnit will not run this function on the main thread, so we'll need to move it there
[self performSelectorOnMainThread:@selector(fetchOnMainThread) withObject:nil waitUntilDone:YES];
}
- (void)fetchOnMainThread
{
// Ensure the delegate is not called when we are running on the main thread
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/basic-authentication"]];
[request setDelegate:self];
[request start];
GHAssertNotNil([request error],@"Failed to generate an authentication error");
}
@end
... ...