Skip to content

Commit

Permalink
Making NSURLSession extensible
Browse files Browse the repository at this point in the history
  • Loading branch information
shahmharsh committed Oct 31, 2017
1 parent 96ea3e3 commit 69d0b3b
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 3 deletions.
10 changes: 10 additions & 0 deletions AppAuth.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
039697461FA8258D003D1FB2 /* OIDURLSessionProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 039697451FA8258D003D1FB2 /* OIDURLSessionProvider.m */; };
0396974D1FA827AD003D1FB2 /* OIDURLSessionProviderTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 0396974C1FA827AD003D1FB2 /* OIDURLSessionProviderTests.m */; };
340DAE571D5821A100EC285B /* OIDAuthorizationService+Mac.m in Sources */ = {isa = PBXBuildFile; fileRef = 340DAE261D581FE700EC285B /* OIDAuthorizationService+Mac.m */; };
340DAE581D5821A100EC285B /* OIDAuthorizationUICoordinatorMac.m in Sources */ = {isa = PBXBuildFile; fileRef = 340DAE281D581FE700EC285B /* OIDAuthorizationUICoordinatorMac.m */; };
340DAE591D5821A100EC285B /* OIDAuthState+Mac.m in Sources */ = {isa = PBXBuildFile; fileRef = 340DAE2A1D581FE700EC285B /* OIDAuthState+Mac.m */; };
Expand Down Expand Up @@ -446,6 +448,9 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
039697441FA8258D003D1FB2 /* OIDURLSessionProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OIDURLSessionProvider.h; sourceTree = "<group>"; };
039697451FA8258D003D1FB2 /* OIDURLSessionProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OIDURLSessionProvider.m; sourceTree = "<group>"; };
0396974C1FA827AD003D1FB2 /* OIDURLSessionProviderTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OIDURLSessionProviderTests.m; sourceTree = "<group>"; };
340DAE251D581FE700EC285B /* OIDAuthorizationService+Mac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "OIDAuthorizationService+Mac.h"; sourceTree = "<group>"; };
340DAE261D581FE700EC285B /* OIDAuthorizationService+Mac.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "OIDAuthorizationService+Mac.m"; sourceTree = "<group>"; };
340DAE271D581FE700EC285B /* OIDAuthorizationUICoordinatorMac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OIDAuthorizationUICoordinatorMac.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -762,6 +767,8 @@
341741D61C5D8243000EF209 /* OIDTokenUtilities.m */,
341741D71C5D8243000EF209 /* OIDURLQueryComponent.h */,
341741D81C5D8243000EF209 /* OIDURLQueryComponent.m */,
039697441FA8258D003D1FB2 /* OIDURLSessionProvider.h */,
039697451FA8258D003D1FB2 /* OIDURLSessionProvider.m */,
);
path = Source;
sourceTree = "<group>";
Expand Down Expand Up @@ -796,6 +803,7 @@
60140F851DE43CC700DA0DC3 /* OIDRegistrationResponseTests.m */,
34D5EC441E6D1AD900814354 /* OIDSwiftTests.swift */,
34D5EC431E6D1AD900814354 /* OIDAppAuthTests-Bridging-Header.h */,
0396974C1FA827AD003D1FB2 /* OIDURLSessionProviderTests.m */,
);
path = UnitTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -1444,6 +1452,7 @@
340DAECD1D582DE100EC285B /* OIDAuthorizationUICoordinatorIOS.m in Sources */,
341741EB1C5D8243000EF209 /* OIDURLQueryComponent.m in Sources */,
341741E11C5D8243000EF209 /* OIDFieldMapping.m in Sources */,
039697461FA8258D003D1FB2 /* OIDURLSessionProvider.m in Sources */,
341741DF1C5D8243000EF209 /* OIDError.m in Sources */,
341741DB1C5D8243000EF209 /* OIDAuthorizationRequest.m in Sources */,
60140F801DE4344200DA0DC3 /* OIDRegistrationResponse.m in Sources */,
Expand All @@ -1466,6 +1475,7 @@
3417421F1C5D82D3000EF209 /* OIDTokenRequestTests.m in Sources */,
341742181C5D82D3000EF209 /* OIDAuthorizationResponseTests.m in Sources */,
341742171C5D82D3000EF209 /* OIDAuthorizationRequestTests.m in Sources */,
0396974D1FA827AD003D1FB2 /* OIDURLSessionProviderTests.m in Sources */,
3417421A1C5D82D3000EF209 /* OIDGrantTypesTests.m in Sources */,
3417421B1C5D82D3000EF209 /* OIDResponseTypesTests.m in Sources */,
60140F831DE43BAF00DA0DC3 /* OIDRegistrationRequestTests.m in Sources */,
Expand Down
1 change: 1 addition & 0 deletions Source/AppAuth.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#import "OIDTokenRequest.h"
#import "OIDTokenResponse.h"
#import "OIDTokenUtilities.h"
#import "OIDURLSessionProvider.h"

#if TARGET_OS_TV
#elif TARGET_OS_WATCH
Expand Down
7 changes: 4 additions & 3 deletions Source/OIDAuthorizationService.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#import "OIDTokenRequest.h"
#import "OIDTokenResponse.h"
#import "OIDURLQueryComponent.h"
#import "OIDURLSessionProvider.h"

/*! @brief Path appended to an OpenID Connect issuer for discovery
@see https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig
Expand Down Expand Up @@ -191,7 +192,7 @@ + (void)discoverServiceConfigurationForIssuer:(NSURL *)issuerURL
+ (void)discoverServiceConfigurationForDiscoveryURL:(NSURL *)discoveryURL
completion:(OIDDiscoveryCallback)completion {

NSURLSession *session = [NSURLSession sharedSession];
NSURLSession *session = [OIDURLSessionProvider session];
NSURLSessionDataTask *task =
[session dataTaskWithURL:discoveryURL
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
Expand Down Expand Up @@ -261,7 +262,7 @@ + (void)discoverServiceConfigurationForDiscoveryURL:(NSURL *)discoveryURL

+ (void)performTokenRequest:(OIDTokenRequest *)request callback:(OIDTokenCallback)callback {
NSURLRequest *URLRequest = [request URLRequest];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSession *session = [OIDURLSessionProvider session];
[[session dataTaskWithRequest:URLRequest
completionHandler:^(NSData *_Nullable data,
NSURLResponse *_Nullable response,
Expand Down Expand Up @@ -372,7 +373,7 @@ + (void)performRegistrationRequest:(OIDRegistrationRequest *)request
return;
}

NSURLSession *session = [NSURLSession sharedSession];
NSURLSession *session = [OIDURLSessionProvider session];
[[session dataTaskWithRequest:URLRequest
completionHandler:^(NSData *_Nullable data,
NSURLResponse *_Nullable response,
Expand Down
40 changes: 40 additions & 0 deletions Source/OIDURLSessionProvider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*! @file OIDURLSessionProvider.h
@brief AppAuth iOS SDK
@copyright
Copyright 2015 Google Inc. All Rights Reserved.
@copydetails
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/*! @brief A NSURLSession provider that allows clients to provide custom implementation
for NSURLSession
*/
@interface OIDURLSessionProvider : NSObject

/*! @brief Obtains the current @c NSURLSession; using the +[NSURLSession sharedSession] if
no custom implementation provided
@return NSURLSession object to be used for making network requests
*/
+ (NSURLSession *)session;

/*! @brief Allows library consumers to change the @c NSURLSession used to create make
network requests
@param session The @c NSURLSession instance that should be used for making network requests
*/
+ (void)setSession:(NSURLSession *)session;
@end
NS_ASSUME_NONNULL_END
39 changes: 39 additions & 0 deletions Source/OIDURLSessionProvider.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*! @file OIDURLSessionProvider.m
@brief AppAuth iOS SDK
@copyright
Copyright 2015 Google Inc. All Rights Reserved.
@copydetails
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import "OIDURLSessionProvider.h"

NS_ASSUME_NONNULL_BEGIN

static NSURLSession * __nullable gURLSession;

@implementation OIDURLSessionProvider

+ (NSURLSession *)session {
if (!gURLSession) {
gURLSession = [NSURLSession sharedSession];
}
return gURLSession;
}

+ (void)setSession:(NSURLSession *)session {
NSAssert(session, @"Parameter: |session| must be non-nil.");
gURLSession = session;
}
@end
NS_ASSUME_NONNULL_END
43 changes: 43 additions & 0 deletions UnitTests/OIDURLSessionProviderTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*! @file OIDURLSessionProviderTests.m
@brief AppAuth iOS SDK
@copyright
Copyright 2015 Google Inc. All Rights Reserved.
@copydetails
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import <XCTest/XCTest.h>
#import "OIDURLSessionProvider.h"

@interface OIDURLSessionProviderTests : XCTestCase

@end

/*! @brief Unit tests for @c OIDURLSessionProvider
*/
@implementation OIDURLSessionProviderTests

- (void)tearDown {
// Setting the session back to default sharedSession for future test cases
[OIDURLSessionProvider setSession:[NSURLSession sharedSession]];
}

- (void)testCustomSession {
NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
NSURLSession *customSession = [NSURLSession sessionWithConfiguration:config];
[OIDURLSessionProvider setSession:customSession];
NSURLSession *session = [OIDURLSessionProvider session];
XCTAssertEqual(session, customSession);
}

@end

0 comments on commit 69d0b3b

Please sign in to comment.