Skip to content

Commit

Permalink
[refactor] replaced assert() ==> NSParameterAssert() for obj-c functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Dodatko committed Jul 30, 2018
1 parent 445b351 commit 136f8d7
Showing 1 changed file with 57 additions and 57 deletions.
114 changes: 57 additions & 57 deletions Source/JiveAuthenticatingHTTPProtocol/JAHPAuthenticatingHTTPProtocol.m
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
{
NSURLRequest * result;

assert(request != nil);
NSParameterAssert(request != nil);
// can be called on any thread

// Canonicalising a request is quite complex, so all the heavy lifting has
Expand All @@ -287,9 +287,9 @@ + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request

- (id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id <NSURLProtocolClient>)client
{
assert(request != nil);
NSParameterAssert(request != nil);
// cachedResponse may be nil
assert(client != nil);
NSParameterAssert(client != nil);
// can be called on any thread

NSMutableURLRequest *mutableRequest = [request mutableCopy];
Expand Down Expand Up @@ -327,9 +327,9 @@ - (void)dealloc
{
// can be called on any thread
[[self class] authenticatingHTTPProtocol:self logWithFormat:@"dealloc"];
assert(self->_task == nil); // we should have cleared it by now
assert(self->_pendingChallenge == nil); // we should have cancelled it by now
assert(self->_pendingChallengeCompletionHandler == nil); // we should have cancelled it by now
NSParameterAssert(self->_task == nil); // we should have cleared it by now
NSParameterAssert(self->_pendingChallenge == nil); // we should have cancelled it by now
NSParameterAssert(self->_pendingChallengeCompletionHandler == nil); // we should have cancelled it by now
}

- (void)startLoading
Expand All @@ -341,8 +341,8 @@ - (void)startLoading
// At this point we kick off the process of loading the URL via NSURLSession.
// The thread that calls this method becomes the client thread.

assert(self.clientThread == nil); // you can't call -startLoading twice
assert(self.task == nil);
NSParameterAssert(self.clientThread == nil); // you can't call -startLoading twice
NSParameterAssert(self.task == nil);

// Calculate our effective run loop modes. In some circumstances (yes I'm looking at
// you UIWebView!) we can be called from a non-standard thread which then runs a
Expand All @@ -353,21 +353,21 @@ - (void)startLoading
// For debugging purposes the non-standard mode is "WebCoreSynchronousLoaderRunLoopMode"
// but it's better not to hard-code that here.

assert(self.modes == nil);
NSParameterAssert(self.modes == nil);
calculatedModes = [NSMutableArray array];
[calculatedModes addObject:NSDefaultRunLoopMode];
currentMode = [[NSRunLoop currentRunLoop] currentMode];
if ( (currentMode != nil) && ! [currentMode isEqual:NSDefaultRunLoopMode] ) {
[calculatedModes addObject:currentMode];
}
self.modes = calculatedModes;
assert([self.modes count] > 0);
NSParameterAssert([self.modes count] > 0);

// Create new request that's a clone of the request we were initialised with,
// except that it has our 'recursive request flag' property set on it.

recursiveRequest = [[self request] mutableCopy];
assert(recursiveRequest != nil);
NSParameterAssert(recursiveRequest != nil);

[[self class] setProperty:@YES forKey:kJAHPRecursiveRequestFlagProperty inRequest:recursiveRequest];

Expand All @@ -385,7 +385,7 @@ - (void)startLoading
// Once everything is ready to go, create a data task with the new request.

self.task = [[[self class] sharedDemux] dataTaskWithRequest:recursiveRequest delegate:self modes:self.modes];
assert(self.task != nil);
NSParameterAssert(self.task != nil);

[self.task resume];
}
Expand All @@ -396,7 +396,7 @@ - (void)stopLoading

[[self class] authenticatingHTTPProtocol:self logWithFormat:@"stop (elapsed %.1f)", [NSDate timeIntervalSinceReferenceDate] - self.startTime];

assert(self.clientThread != nil); // someone must have called -startLoading
NSParameterAssert(self.clientThread != nil); // someone must have called -startLoading

// Check that we're being stopped on the same thread that we were started
// on. Without this invariant things are going to go badly (for example,
Expand All @@ -408,7 +408,7 @@ - (void)stopLoading
// Rather, I rely on our client calling us on the right thread, which is what
// the following assert is about.

assert([NSThread currentThread] == self.clientThread);
NSParameterAssert([NSThread currentThread] == self.clientThread);

[self cancelPendingChallenge];
if (self.task != nil) {
Expand All @@ -432,7 +432,7 @@ - (void)performOnThread:(NSThread *)thread modes:(NSArray *)modes block:(dispatc
{
// thread may be nil
// modes may be nil
assert(block != nil);
NSParameterAssert(block != nil);

if (thread == nil) {
thread = [NSThread mainThread];
Expand All @@ -450,7 +450,7 @@ - (void)performOnThread:(NSThread *)thread modes:(NSArray *)modes block:(dispatc

- (void)onThreadPerformBlock:(dispatch_block_t)block
{
assert(block != nil);
NSParameterAssert(block != nil);
block();
}

Expand All @@ -475,9 +475,9 @@ - (void)onThreadPerformBlock:(dispatch_block_t)block

- (void)didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(JAHPChallengeCompletionHandler)completionHandler
{
assert(challenge != nil);
assert(completionHandler != nil);
assert([NSThread currentThread] == self.clientThread);
NSParameterAssert(challenge != nil);
NSParameterAssert(completionHandler != nil);
NSParameterAssert([NSThread currentThread] == self.clientThread);

[[self class] authenticatingHTTPProtocol:self logWithFormat:@"challenge %@ received", [[challenge protectionSpace] authenticationMethod]];

Expand All @@ -498,9 +498,9 @@ - (void)didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challe

- (void)mainThreadDidReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(JAHPChallengeCompletionHandler)completionHandler
{
assert(challenge != nil);
assert(completionHandler != nil);
assert([NSThread isMainThread]);
NSParameterAssert(challenge != nil);
NSParameterAssert(completionHandler != nil);
NSParameterAssert([NSThread isMainThread]);

if (self.pendingChallenge != nil) {

Expand All @@ -512,7 +512,7 @@ - (void)mainThreadDidReceiveAuthenticationChallenge:(NSURLAuthenticationChalleng
// namely, the client thread.

[[self class] authenticatingHTTPProtocol:self logWithFormat:@"challenge %@ cancelled; other challenge pending", [[challenge protectionSpace] authenticationMethod]];
assert(NO);
NSParameterAssert(NO);
[self clientThreadCancelAuthenticationChallenge:challenge completionHandler:completionHandler];
} else {
id<JAHPAuthenticatingHTTPProtocolDelegate> strongDelegate;
Expand All @@ -526,7 +526,7 @@ - (void)mainThreadDidReceiveAuthenticationChallenge:(NSURLAuthenticationChalleng

if ( ! [strongDelegate respondsToSelector:@selector(authenticatingHTTPProtocol:canAuthenticateAgainstProtectionSpace:)] ) {
[[self class] authenticatingHTTPProtocol:self logWithFormat:@"challenge %@ cancelled; no delegate method", [[challenge protectionSpace] authenticationMethod]];
assert(NO);
NSParameterAssert(NO);
[self clientThreadCancelAuthenticationChallenge:challenge completionHandler:completionHandler];
} else {

Expand Down Expand Up @@ -556,9 +556,9 @@ - (void)mainThreadDidReceiveAuthenticationChallenge:(NSURLAuthenticationChalleng
- (void)clientThreadCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(JAHPChallengeCompletionHandler)completionHandler
{
#pragma unused(challenge)
assert(challenge != nil);
assert(completionHandler != nil);
assert([NSThread isMainThread]);
NSParameterAssert(challenge != nil);
NSParameterAssert(completionHandler != nil);
NSParameterAssert([NSThread isMainThread]);

[self performOnThread:self.clientThread modes:self.modes block:^{
completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
Expand All @@ -574,7 +574,7 @@ - (void)clientThreadCancelAuthenticationChallenge:(NSURLAuthenticationChallenge

- (void)cancelPendingChallenge
{
assert([NSThread currentThread] == self.clientThread);
NSParameterAssert([NSThread currentThread] == self.clientThread);

// Just pass the work off to the main thread. We do this so that all accesses
// to pendingChallenge are done from the main thread, which avoids the need for
Expand Down Expand Up @@ -613,7 +613,7 @@ - (void)cancelPendingChallenge
[[self class] authenticatingHTTPProtocol:self logWithFormat:@"challenge %@ cancellation failed; no delegate method", [[challenge protectionSpace] authenticationMethod]];
// If we managed to send a challenge to the client but can't cancel it, that's bad.
// There's nothing we can do at this point except log the problem.
assert(NO);
NSParameterAssert(NO);
}
}
}];
Expand All @@ -622,8 +622,8 @@ - (void)cancelPendingChallenge
- (void)resolvePendingAuthenticationChallengeWithCredential:(NSURLCredential *)credential
{
// credential may be nil
assert([NSThread isMainThread]);
assert(self.clientThread != nil);
NSParameterAssert([NSThread isMainThread]);
NSParameterAssert(self.clientThread != nil);

JAHPChallengeCompletionHandler completionHandler;
NSURLAuthenticationChallenge *challenge;
Expand All @@ -650,8 +650,8 @@ - (void)resolvePendingAuthenticationChallengeWithCredential:(NSURLCredential *)c
}

- (void)cancelPendingAuthenticationChallenge {
assert([NSThread isMainThread]);
assert(self.clientThread != nil);
NSParameterAssert([NSThread isMainThread]);
NSParameterAssert(self.clientThread != nil);

JAHPChallengeCompletionHandler completionHandler;
NSURLAuthenticationChallenge *challenge;
Expand Down Expand Up @@ -690,12 +690,12 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPer

#pragma unused(session)
#pragma unused(task)
assert(task == self.task);
assert(response != nil);
assert(newRequest != nil);
NSParameterAssert(task == self.task);
NSParameterAssert(response != nil);
NSParameterAssert(newRequest != nil);
#pragma unused(completionHandler)
assert(completionHandler != nil);
assert([NSThread currentThread] == self.clientThread);
NSParameterAssert(completionHandler != nil);
NSParameterAssert([NSThread currentThread] == self.clientThread);

[[self class] authenticatingHTTPProtocol:self logWithFormat:@"will redirect from %@ to %@", [response URL], [newRequest URL]];

Expand All @@ -707,7 +707,7 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPer
// We also cancel our current connection because the client is going to start a new request for
// us anyway.

assert([[self class] propertyForKey:kJAHPRecursiveRequestFlagProperty inRequest:newRequest] != nil);
NSParameterAssert([[self class] propertyForKey:kJAHPRecursiveRequestFlagProperty inRequest:newRequest] != nil);

redirectRequest = [newRequest mutableCopy];
[[self class] removePropertyForKey:kJAHPRecursiveRequestFlagProperty inRequest:redirectRequest];
Expand Down Expand Up @@ -742,10 +742,10 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didRece

#pragma unused(session)
#pragma unused(task)
assert(task == self.task);
assert(challenge != nil);
assert(completionHandler != nil);
assert([NSThread currentThread] == self.clientThread);
NSParameterAssert(task == self.task);
NSParameterAssert(challenge != nil);
NSParameterAssert(completionHandler != nil);
NSParameterAssert([NSThread currentThread] == self.clientThread);

// Ask our delegate whether it wants this challenge. We do this from this thread, not the main thread,
// to avoid the overload of bouncing to the main thread for challenges that aren't going to be customised
Expand Down Expand Up @@ -786,10 +786,10 @@ - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)data

#pragma unused(session)
#pragma unused(dataTask)
assert(dataTask == self.task);
assert(response != nil);
assert(completionHandler != nil);
assert([NSThread currentThread] == self.clientThread);
NSParameterAssert(dataTask == self.task);
NSParameterAssert(response != nil);
NSParameterAssert(completionHandler != nil);
NSParameterAssert([NSThread currentThread] == self.clientThread);

// Pass the call on to our client. The only tricky thing is that we have to decide on a
// cache storage policy, which is based on the actual request we issued, not the request
Expand All @@ -799,7 +799,7 @@ - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)data
cacheStoragePolicy = JAHPCacheStoragePolicyForRequestAndResponse(self.task.originalRequest, (NSHTTPURLResponse *) response);
statusCode = [((NSHTTPURLResponse *) response) statusCode];
} else {
assert(NO);
NSParameterAssert(NO);
cacheStoragePolicy = NSURLCacheStorageNotAllowed;
statusCode = 42;
}
Expand All @@ -823,9 +823,9 @@ - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)data

#pragma unused(session)
#pragma unused(dataTask)
assert(dataTask == self.task);
assert(data != nil);
assert([NSThread currentThread] == self.clientThread);
NSParameterAssert(dataTask == self.task);
NSParameterAssert(data != nil);
NSParameterAssert([NSThread currentThread] == self.clientThread);

// Just pass the call on to our client.

Expand All @@ -846,10 +846,10 @@ - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)data

#pragma unused(session)
#pragma unused(dataTask)
assert(dataTask == self.task);
assert(proposedResponse != nil);
assert(completionHandler != nil);
assert([NSThread currentThread] == self.clientThread);
NSParameterAssert(dataTask == self.task);
NSParameterAssert(proposedResponse != nil);
NSParameterAssert(completionHandler != nil);
NSParameterAssert([NSThread currentThread] == self.clientThread);

// We implement this delegate callback purely for the purposes of logging.

Expand All @@ -863,8 +863,8 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didComp
{
#pragma unused(session)
#pragma unused(task)
assert( (self.task == nil) || (task == self.task) ); // can be nil in the 'cancel from -stopLoading' case
assert([NSThread currentThread] == self.clientThread);
NSParameterAssert( (self.task == nil) || (task == self.task) ); // can be nil in the 'cancel from -stopLoading' case
NSParameterAssert([NSThread currentThread] == self.clientThread);

// Just log and then, in most cases, pass the call on to our client.

Expand Down

0 comments on commit 136f8d7

Please sign in to comment.