2525@interface MockedSessionTask : NSObject
2626
2727@property (atomic , assign ) NSUInteger taskIdentifier;
28+ @property (nonatomic , strong ) NSURLRequest *originalRequest;
2829
2930@property (nonatomic , strong ) void (^resumeBlock)();
3031@property (nonatomic , strong ) void (^cancelBlock)();
@@ -62,20 +63,29 @@ @implementation URLSessionTests
6263- (void )testConstructors {
6364 NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration ];
6465 NSURLSession *URLSession = [NSURLSession sharedSession ];
66+ id delegate = PFStrictProtocolMock (@protocol (PFURLSessionDelegate));
6567
66- PFURLSession *session = [[PFURLSession alloc ] initWithConfiguration: configuration];
68+ PFURLSession *session = [[PFURLSession alloc ] initWithConfiguration: configuration
69+ delegate: delegate];
6770 XCTAssertNotNil (session);
71+ XCTAssertEqual ((id )session.delegate , delegate);
6872 [session invalidateAndCancel ];
6973
70- session = [PFURLSession sessionWithConfiguration: configuration];
74+ session = [PFURLSession sessionWithConfiguration: configuration
75+ delegate: delegate];
7176 XCTAssertNotNil (session);
77+ XCTAssertEqual ((id )session.delegate , delegate);
7278 [session invalidateAndCancel ];
7379
74- session = [[PFURLSession alloc ] initWithURLSession: URLSession];
80+ session = [[PFURLSession alloc ] initWithURLSession: URLSession
81+ delegate: delegate];
7582 XCTAssertNotNil (URLSession);
83+ XCTAssertEqual ((id )session.delegate , delegate);
7684
77- session = [PFURLSession sessionWithURLSession: URLSession];
85+ session = [PFURLSession sessionWithURLSession: URLSession
86+ delegate: delegate];
7887 XCTAssertNotNil (session);
88+ XCTAssertEqual ((id )session.delegate , delegate);
7989
8090 PFAssertThrowsInconsistencyException ([PFURLSession new ]);
8191}
@@ -87,6 +97,7 @@ - (void)testPerformDataRequestSuccess {
8797 NSArray *mocks = @[ mockedURLSession, mockedURLRequest, mockedCommand ];
8898
8999 MockedSessionTask *mockedDataTask = [[MockedSessionTask alloc ] init ];
100+ mockedDataTask.originalRequest = mockedURLRequest;
90101
91102 __block id <NSURLSessionDelegate , NSURLSessionTaskDelegate , NSURLSessionDataDelegate > sessionDelegate = nil ;
92103
@@ -115,9 +126,13 @@ - (void)testPerformDataRequestSuccess {
115126 [sessionDelegate URLSession: mockedURLSession task: (id )mockedDataTask didCompleteWithError: nil ];
116127 };
117128
118- PFURLSession *session = [PFURLSession sessionWithURLSession: mockedURLSession];
129+ id delegate = PFStrictProtocolMock (@protocol (PFURLSessionDelegate));
130+ PFURLSession *session = [PFURLSession sessionWithURLSession: mockedURLSession delegate: delegate];
119131 sessionDelegate = (id )session;
120132
133+ OCMExpect ([delegate urlSession: session willPerformURLRequest: mockedURLRequest]);
134+ OCMExpect ([delegate urlSession: session didPerformURLRequest: mockedURLRequest withURLResponse: [OCMArg isNotNil ]]);
135+
121136 XCTestExpectation *expectation = [self currentSelectorTestExpectation ];
122137 [[session performDataURLRequestAsync: mockedURLRequest forCommand: mockedCommand cancellationToken: nil ] continueWithBlock: ^id (BFTask *task) {
123138 PFCommandResult *actualResult = task.result ;
@@ -128,17 +143,19 @@ - (void)testPerformDataRequestSuccess {
128143 [self waitForTestExpectations ];
129144
130145 OCMVerifyAll ((id )mockedURLSession);
146+ OCMVerifyAll (delegate);
131147 [mocks makeObjectsPerformSelector: @selector (stopMocking )];
132148}
133149
134150- (void )testPerformDataRequesPreCancel {
135151 NSURLSession *mockedURLSession = PFStrictClassMock ([NSURLSession class ]);
136152 NSURLRequest *mockedURLRequest = PFStrictClassMock ([NSURLRequest class ]);
137153 PFRESTCommand *mockedCommand = PFStrictClassMock ([PFRESTCommand class ]);
154+ id delegate = PFStrictProtocolMock (@protocol (PFURLSessionDelegate));
138155 NSArray *mocks = @[ mockedURLSession, mockedURLRequest, mockedCommand ];
139156
140157 BFCancellationTokenSource *cancellationTokenSource = [BFCancellationTokenSource cancellationTokenSource ];
141- PFURLSession *session = [PFURLSession sessionWithURLSession: mockedURLSession];
158+ PFURLSession *session = [PFURLSession sessionWithURLSession: mockedURLSession delegate: delegate ];
142159
143160 XCTestExpectation *expectation = [self currentSelectorTestExpectation ];
144161
@@ -162,6 +179,7 @@ - (void)testPerformDataRequestCancellation {
162179 NSArray *mocks = @[ mockedURLSession, mockedURLRequest, mockedCommand ];
163180
164181 MockedSessionTask *mockedDataTask = [[MockedSessionTask alloc ] init ];
182+ mockedDataTask.originalRequest = mockedURLRequest;
165183
166184 BFCancellationTokenSource *cancellationTokenSource = [BFCancellationTokenSource cancellationTokenSource ];
167185 __block id <NSURLSessionDelegate , NSURLSessionTaskDelegate , NSURLSessionDataDelegate > sessionDelegate = nil ;
@@ -199,9 +217,13 @@ - (void)testPerformDataRequestCancellation {
199217 [cancelExpectation fulfill ];
200218 };
201219
202- PFURLSession *session = [PFURLSession sessionWithURLSession: mockedURLSession];
220+ id delegate = PFStrictProtocolMock (@protocol (PFURLSessionDelegate));
221+ PFURLSession *session = [PFURLSession sessionWithURLSession: mockedURLSession delegate: delegate];
203222 sessionDelegate = (id )session;
204223
224+ OCMExpect ([delegate urlSession: session willPerformURLRequest: mockedURLRequest]);
225+ OCMExpect ([delegate urlSession: session didPerformURLRequest: mockedURLRequest withURLResponse: [OCMArg isNotNil ]]);
226+
205227 XCTestExpectation *expectation = [self currentSelectorTestExpectation ];
206228 [[session performDataURLRequestAsync: mockedURLRequest
207229 forCommand: mockedCommand
@@ -213,6 +235,7 @@ - (void)testPerformDataRequestCancellation {
213235 [self waitForTestExpectations ];
214236
215237 OCMVerifyAll ((id )mockedURLSession);
238+ OCMVerifyAll (delegate);
216239 [mocks makeObjectsPerformSelector: @selector (stopMocking )];
217240}
218241
@@ -223,6 +246,7 @@ - (void)testPerformDataRequestError {
223246 NSArray *mocks = @[ mockedURLSession, mockedURLRequest, mockedCommand ];
224247
225248 MockedSessionTask *mockedDataTask = [[MockedSessionTask alloc ] init ];
249+ mockedDataTask.originalRequest = mockedURLRequest;
226250
227251 NSError *expectedError = [NSError errorWithDomain: PFParseErrorDomain code: 1337 userInfo: nil ];
228252 __block id <NSURLSessionDelegate , NSURLSessionTaskDelegate , NSURLSessionDataDelegate > sessionDelegate = nil ;
@@ -237,9 +261,13 @@ - (void)testPerformDataRequestError {
237261 [sessionDelegate URLSession: mockedURLSession task: (id )mockedDataTask didCompleteWithError: expectedError];
238262 };
239263
240- PFURLSession *session = [PFURLSession sessionWithURLSession: mockedURLSession];
264+ id delegate = PFStrictProtocolMock (@protocol (PFURLSessionDelegate));
265+ PFURLSession *session = [PFURLSession sessionWithURLSession: mockedURLSession delegate: delegate];
241266 sessionDelegate = (id )session;
242267
268+ OCMExpect ([delegate urlSession: session willPerformURLRequest: mockedURLRequest]);
269+ OCMExpect ([delegate urlSession: session didPerformURLRequest: mockedURLRequest withURLResponse: nil ]);
270+
243271 XCTestExpectation *expectation = [self currentSelectorTestExpectation ];
244272 [[session performDataURLRequestAsync: mockedURLRequest forCommand: mockedCommand cancellationToken: nil ]
245273 continueWithBlock: ^id (BFTask *task) {
@@ -251,6 +279,7 @@ - (void)testPerformDataRequestError {
251279 [self waitForTestExpectations ];
252280
253281 OCMVerifyAll ((id )mockedURLSession);
282+ OCMVerifyAll (delegate);
254283 [mocks makeObjectsPerformSelector: @selector (stopMocking )];
255284}
256285
@@ -263,7 +292,8 @@ - (void)testFileUploadRequestPreCancel {
263292 BFCancellationTokenSource *cancellationTokenSource = [BFCancellationTokenSource cancellationTokenSource ];
264293 NSString *exampleFile = @" file.txt" ;
265294
266- PFURLSession *session = [PFURLSession sessionWithURLSession: mockedURLSession];
295+ id delegate = PFStrictProtocolMock (@protocol (PFURLSessionDelegate));
296+ PFURLSession *session = [PFURLSession sessionWithURLSession: mockedURLSession delegate: delegate];
267297
268298 XCTestExpectation *expectation = [self currentSelectorTestExpectation ];
269299 [cancellationTokenSource cancel ];
@@ -290,6 +320,7 @@ - (void)testFileUploadSuccess {
290320 NSArray *mocks = @[ mockedURLSession, mockedURLRequest, mockedCommand ];
291321
292322 MockedSessionTask *mockedUploadTask = [[MockedSessionTask alloc ] init ];
323+ mockedUploadTask.originalRequest = mockedURLRequest;
293324
294325 NSString *exampleFile = @" file.txt" ;
295326 __block id <NSURLSessionDelegate , NSURLSessionTaskDelegate , NSURLSessionDataDelegate > sessionDelegate = nil ;
@@ -331,9 +362,13 @@ - (void)testFileUploadSuccess {
331362 didCompleteWithError: nil ];
332363 };
333364
334- PFURLSession *session = [PFURLSession sessionWithURLSession: mockedURLSession];
365+ id delegate = PFStrictProtocolMock (@protocol (PFURLSessionDelegate));
366+ PFURLSession *session = [PFURLSession sessionWithURLSession: mockedURLSession delegate: delegate];
335367 sessionDelegate = (id )session;
336368
369+ OCMExpect ([delegate urlSession: session willPerformURLRequest: mockedURLRequest]);
370+ OCMExpect ([delegate urlSession: session didPerformURLRequest: mockedURLRequest withURLResponse: [OCMArg isNotNil ]]);
371+
337372 __block int lastProgress = 0 ;
338373
339374 XCTestExpectation *expectation = [self currentSelectorTestExpectation ];
@@ -354,6 +389,7 @@ - (void)testFileUploadSuccess {
354389 [self waitForTestExpectations ];
355390
356391 OCMVerifyAll ((id )mockedURLSession);
392+ OCMVerifyAll (delegate);
357393 [mocks makeObjectsPerformSelector: @selector (stopMocking )];
358394}
359395
@@ -365,6 +401,7 @@ - (void)testFileUploadRequestCancellation {
365401 NSArray *mocks = @[ mockedURLSession, mockedURLRequest, mockedCommand ];
366402
367403 MockedSessionTask *mockedUploadTask = [[MockedSessionTask alloc ] init ];
404+ mockedUploadTask.originalRequest = mockedURLRequest;
368405
369406 BFCancellationTokenSource *cancellationTokenSource = [BFCancellationTokenSource cancellationTokenSource ];
370407 NSString *exampleFile = @" file.txt" ;
@@ -415,9 +452,13 @@ - (void)testFileUploadRequestCancellation {
415452 [cancelExpectation fulfill ];
416453 };
417454
418- PFURLSession *session = [PFURLSession sessionWithURLSession: mockedURLSession];
455+ id delegate = PFProtocolMock (@protocol (PFURLSessionDelegate));
456+ PFURLSession *session = [PFURLSession sessionWithURLSession: mockedURLSession delegate: delegate];
419457 sessionDelegate = (id )session;
420458
459+ OCMExpect ([delegate urlSession: session willPerformURLRequest: mockedURLRequest]);
460+ OCMExpect ([delegate urlSession: session didPerformURLRequest: mockedURLRequest withURLResponse: [OCMArg isNotNil ]]);
461+
421462 __block int lastProgress = 0 ;
422463
423464 XCTestExpectation *expectation = [self currentSelectorTestExpectation ];
@@ -437,6 +478,7 @@ - (void)testFileUploadRequestCancellation {
437478 [self waitForTestExpectations ];
438479
439480 OCMVerifyAll ((id )mockedURLSession);
481+ OCMVerifyAll (delegate);
440482 [mocks makeObjectsPerformSelector: @selector (stopMocking )];
441483}
442484
@@ -447,6 +489,7 @@ - (void)testCaching {
447489 NSArray *mocks = @[ mockedURLSession, mockedURLRequest, mockedCommand ];
448490
449491 MockedSessionTask *mockedDataTask = [[MockedSessionTask alloc ] init ];
492+ mockedDataTask.originalRequest = mockedURLRequest;
450493
451494 __block id <NSURLSessionDelegate , NSURLSessionTaskDelegate , NSURLSessionDataDelegate > sessionDelegate = nil ;
452495
@@ -480,9 +523,13 @@ - (void)testCaching {
480523 completionHandler: ^(NSCachedURLResponse *cached) { XCTAssertNil (cached); }];
481524 };
482525
483- PFURLSession *session = [PFURLSession sessionWithURLSession: mockedURLSession];
526+ id delegate = PFProtocolMock (@protocol (PFURLSessionDelegate));
527+ PFURLSession *session = [PFURLSession sessionWithURLSession: mockedURLSession delegate: delegate];
484528 sessionDelegate = (id )session;
485529
530+ OCMExpect ([delegate urlSession: session willPerformURLRequest: mockedURLRequest]);
531+ OCMExpect ([delegate urlSession: session didPerformURLRequest: mockedURLRequest withURLResponse: [OCMArg isNotNil ]]);
532+
486533 XCTestExpectation *expectation = [self currentSelectorTestExpectation ];
487534 [[session performDataURLRequestAsync: mockedURLRequest forCommand: mockedCommand cancellationToken: nil ]
488535 continueWithBlock: ^id (BFTask *task) {
@@ -493,11 +540,14 @@ - (void)testCaching {
493540 [self waitForTestExpectations ];
494541
495542 OCMVerifyAll ((id )mockedURLSession);
543+ OCMVerifyAll (delegate);
496544 [mocks makeObjectsPerformSelector: @selector (stopMocking )];
497545}
498546
499547- (void )testInvalidate {
500- PFURLSession *session = [PFURLSession sessionWithConfiguration: [NSURLSessionConfiguration defaultSessionConfiguration ]];
548+ id delegate = PFStrictProtocolMock (@protocol (PFURLSessionDelegate));
549+ PFURLSession *session = [PFURLSession sessionWithConfiguration: [NSURLSessionConfiguration defaultSessionConfiguration ]
550+ delegate: delegate];
501551 XCTAssertNoThrow ([session invalidateAndCancel ]); // lol?
502552}
503553
0 commit comments