Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added isEnabled getters for OHHTTPStubs. #159

Merged
merged 4 commits into from
Mar 13, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions OHHTTPStubs/Sources/OHHTTPStubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ typedef OHHTTPStubsResponse* __nonnull (^OHHTTPStubsResponseBlock)( NSURLRequest
*/
+(void)setEnabled:(BOOL)enabled;

/**
* Whether or not stubs are enabled for the shared session or for `NSURLConnection`
*
* @return If `YES` the stubs are enabled. If `NO` then the stubs are disabled
*/
+(BOOL)isEnabled;

#if defined(__IPHONE_7_0) || defined(__MAC_10_9)
/**
* Enable or disable the stubs on a given `NSURLSessionConfiguration`.
Expand All @@ -149,6 +156,15 @@ typedef OHHTTPStubsResponse* __nonnull (^OHHTTPStubsResponseBlock)( NSURLRequest
* created sessions.
*/
+ (void)setEnabled:(BOOL)enabled forSessionConfiguration:(NSURLSessionConfiguration *)sessionConfig;

/**
* Whether stubs are enabled or disabled on a given `NSURLSessionConfiguration`
*
* @param sessionConfig The NSURLSessionConfiguration on which to enable/disable the stubs
*
* @return If `YES` the stubs are enabled for sessionConfig. If `NO` then the stubs are disabled
*/
+ (BOOL)isEnabledForSessionConfiguration:(NSURLSessionConfiguration *)sessionConfig;
#endif

#pragma mark - Debug Methods
Expand Down
34 changes: 33 additions & 1 deletion OHHTTPStubs/Sources/OHHTTPStubs.m
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@ +(void)removeAllStubs

#pragma mark > Disabling & Re-Enabling stubs

static BOOL currentEnabledState = NO;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

global variables are not thread safe, so this is not an acceptable solution. You could instead add an atomic property on the shared instance for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion!


+(void)setEnabled:(BOOL)enable
{
static BOOL currentEnabledState = NO;
currentEnabledState = NO;
if (enable && !currentEnabledState)
{
[NSURLProtocol registerClass:OHHTTPStubsProtocol.class];
Expand All @@ -163,6 +165,11 @@ +(void)setEnabled:(BOOL)enable
currentEnabledState = enable;
}

+ (BOOL)isEnabled
{
return currentEnabledState;
}

#if defined(__IPHONE_7_0) || defined(__MAC_10_9)
+ (void)setEnabled:(BOOL)enable forSessionConfiguration:(NSURLSessionConfiguration*)sessionConfig
{
Expand All @@ -189,6 +196,31 @@ + (void)setEnabled:(BOOL)enable forSessionConfiguration:(NSURLSessionConfigurati
@"this method if the user is running iOS7+/OSX9+.", NSStringFromSelector(_cmd));
}
}

+ (BOOL)isEnabledForSessionConfiguration:(NSURLSessionConfiguration *)sessionConfig
{
// Runtime check to make sure the API is available on this version
if ( [sessionConfig respondsToSelector:@selector(protocolClasses)]
&& [sessionConfig respondsToSelector:@selector(setProtocolClasses:)])
{
NSMutableArray * urlProtocolClasses = [NSMutableArray arrayWithArray:sessionConfig.protocolClasses];
Class protoCls = OHHTTPStubsProtocol.class;
if ([urlProtocolClasses containsObject:protoCls])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not directly return [urlProtocolClasses containsObject:protoCls];?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

{
return YES;
} else
{
return NO;
}
}
else
{
NSLog(@"[OHHTTPStubs] %@ is only available when running on iOS7+/OSX9+. "
@"Use conditions like 'if ([NSURLSessionConfiguration class])' to only call "
@"this method if the user is running iOS7+/OSX9+.", NSStringFromSelector(_cmd));
return NO;
}
}
#endif

#pragma mark > Debug Methods
Expand Down