-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathFPRNSURLSessionInstrumentTest.m
836 lines (766 loc) · 41.3 KB
/
FPRNSURLSessionInstrumentTest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
// Copyright 2020 Google LLC
//
// 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.
#pragma mark - Unswizzle based tests
#import "FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTestDelegates.h"
#import <XCTest/XCTest.h>
#import <objc/runtime.h>
#import "FirebasePerformance/Sources/Configurations/FPRConfigurations+Private.h"
#import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
#import "FirebasePerformance/Sources/Instrumentation/FPRNetworkTrace.h"
#import "FirebasePerformance/Sources/Instrumentation/Network/FPRNSURLSessionInstrument.h"
#import "FirebasePerformance/Sources/Instrumentation/Network/FPRNSURLSessionInstrument_Private.h"
#import "FirebasePerformance/Sources/Public/FirebasePerformance/FIRPerformance.h"
#import "FirebasePerformance/Tests/Unit/FPRTestCase.h"
#import "FirebasePerformance/Tests/Unit/FPRTestUtils.h"
#import "FirebasePerformance/Tests/Unit/Server/FPRHermeticTestServer.h"
/** This class is used to wrap an NSURLSession object during testing. */
@interface FPRNSURLSessionProxy : NSProxy {
// The wrapped session object.
id _session;
}
/** @return an instance of the session proxy. */
- (instancetype)initWithSession:(id)session;
@end
@implementation FPRNSURLSessionProxy
- (instancetype)initWithSession:(id)session {
if (self) {
_session = session;
}
return self;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
return [_session methodSignatureForSelector:selector];
}
- (void)forwardInvocation:(NSInvocation *)invocation {
[invocation invokeWithTarget:_session];
}
@end
@interface FPRNSURLSessionInstrumentTest : FPRTestCase
/** Test server to create connections to. */
@property(nonatomic) FPRHermeticTestServer *testServer;
@end
@implementation FPRNSURLSessionInstrumentTest
- (void)setUp {
[super setUp];
FIRPerformance *performance = [FIRPerformance sharedInstance];
[performance setDataCollectionEnabled:YES];
XCTAssertFalse(self.testServer.isRunning);
self.testServer = [[FPRHermeticTestServer alloc] init];
[self.testServer registerTestPaths];
[self.testServer start];
}
- (void)tearDown {
[super tearDown];
FIRPerformance *performance = [FIRPerformance sharedInstance];
[performance setDataCollectionEnabled:NO];
[self.testServer stop];
[FPRConfigurations reset];
}
/** Waits for the server connection to finish by giving a block to run just before a response is
* sent.
*
* @param block A block to run just after the server response is sent.
*/
- (void)waitAndRunBlockAfterResponse:(void (^)(id self,
GCDWebServerRequest *_Nonnull request,
GCDWebServerResponse *_Nonnull response))block {
__block BOOL loopingMainThread = YES;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
__weak id weakSelf = self;
self.testServer.responseCompletedBlock =
^(GCDWebServerRequest *_Nonnull request, GCDWebServerResponse *_Nonnull response) {
block(weakSelf, request, response);
dispatch_semaphore_signal(sema);
};
XCTAssertEqual(
dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC)), 0);
loopingMainThread = NO;
});
// This is necessary because the FPRHermeticTestServer callbacks occur on the main thread.
while (loopingMainThread) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
}
#pragma mark - Testing infrastructure and subclass instrumenting
/** Tests initing of FPRNSURLSessionInstrument also inits NSURLSessionDelegate instrument. */
- (void)testInit {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
self.testServer.responseCompletedBlock =
^(GCDWebServerRequest *_Nonnull request, GCDWebServerResponse *_Nonnull response) {
XCTAssert(instrument);
XCTAssert(instrument.delegateInstrument);
};
[instrument deregisterInstrumentors];
}
/** Tests that creating a shared session returns a non-nil object. */
- (void)testSharedSession {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSession *session = [NSURLSession sharedSession];
XCTAssertNotNil(session);
[instrument deregisterInstrumentors];
}
/** Tests that a method that returns an NSURLSession subclass registers that subclass. */
- (void)testSubclassRegistration {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSession *session = [NSURLSession sharedSession];
XCTAssertNotNil(session);
XCTAssertEqual(instrument.classInstrumentors.count, 2);
[instrument deregisterInstrumentors];
}
/** Tests that a discovered subclass isn't registered more than once. */
- (void)testSubclassIsNotRegisteredMoreThanOnce {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSession *session2 = [NSURLSession sharedSession];
XCTAssertNotNil(session);
XCTAssertNotNil(session2);
XCTAssertEqual(instrument.classInstrumentors.count, 2);
[instrument deregisterInstrumentors];
}
/** Tests sessionWithConfiguration: with the default configuration returns a non-nil object. */
- (void)testSessionWithDefaultSessionConfiguration {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
XCTAssertNotNil(session);
XCTAssertEqual(instrument.classInstrumentors.count, 2);
[instrument deregisterInstrumentors];
}
/** Tests sessionWithConfiguration: with an ephemeral configuration returns a non-nil object. */
- (void)testSessionWithEphemeralSessionConfiguration {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration ephemeralSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
XCTAssertNotNil(session);
XCTAssertEqual(instrument.classInstrumentors.count, 2);
[instrument deregisterInstrumentors];
}
/** Tests sessionWithConfiguration: with a background configuration returns a non-nil object. */
- (void)testSessionWithBackgroundSessionConfiguration {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"madeUpID"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
XCTAssertNotNil(session);
XCTAssertEqual(instrument.classInstrumentors.count, 2);
[instrument deregisterInstrumentors];
}
/** Tests instrumenting an NSProxy wrapped NSURLSession object works. */
- (void)testProxyWrappedSharedSession {
Method method = class_getClassMethod([NSURLSession class], @selector(sharedSession));
IMP originalImp = method_getImplementation(method);
IMP swizzledImp = imp_implementationWithBlock(^(id session) {
typedef NSURLSession *(*OriginalImp)(id, SEL);
NSURLSession *originalSession = ((OriginalImp)originalImp)(session, @selector(sharedSession));
return [[FPRNSURLSessionProxy alloc] initWithSession:originalSession];
});
method_setImplementation(method, swizzledImp);
XCTAssertEqual([[NSURLSession sharedSession] class], [FPRNSURLSessionProxy class]);
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSession *session;
XCTAssertNoThrow(session = [NSURLSession sharedSession]);
NSURL *url = self.testServer.serverURL;
XCTestExpectation *expectation = [self expectationWithDescription:@"completionHandler"];
NSURLSessionDownloadTask *task =
[session downloadTaskWithURL:url
completionHandler:^(NSURL *_Nullable location, NSURLResponse *_Nullable response,
NSError *_Nullable error) {
[expectation fulfill];
}];
[task resume];
XCTAssertNotNil(task);
[self waitForExpectationsWithTimeout:10.0 handler:nil];
[instrument deregisterInstrumentors];
method_setImplementation(method, originalImp);
XCTAssertNotEqual([[NSURLSession sharedSession] class], [FPRNSURLSessionProxy class]);
}
/** Tests instrumenting an NSProxy wrapped NSURLSession object works. */
- (void)testProxyWrappedSessionWithConfiguration {
Method method = class_getClassMethod([NSURLSession class], @selector(sessionWithConfiguration:));
IMP originalImp = method_getImplementation(method);
IMP swizzledImp =
imp_implementationWithBlock(^(id session, NSURLSessionConfiguration *configuration) {
typedef NSURLSession *(*OriginalImp)(id, SEL, NSURLSessionConfiguration *);
NSURLSession *originalSession = ((OriginalImp)originalImp)(
session, @selector(sessionWithConfiguration:), configuration);
return [[FPRNSURLSessionProxy alloc] initWithSession:originalSession];
});
method_setImplementation(method, swizzledImp);
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
XCTAssertEqual([[NSURLSession sessionWithConfiguration:config] class],
[FPRNSURLSessionProxy class]);
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSession *session;
XCTAssertNoThrow(session = [NSURLSession sessionWithConfiguration:config]);
NSURL *url = self.testServer.serverURL;
XCTestExpectation *expectation = [self expectationWithDescription:@"completionHandler"];
NSURLSessionDownloadTask *task =
[session downloadTaskWithURL:url
completionHandler:^(NSURL *_Nullable location, NSURLResponse *_Nullable response,
NSError *_Nullable error) {
[expectation fulfill];
}];
XCTAssertNotNil(task);
[task resume];
[self waitForExpectationsWithTimeout:10.0 handler:nil];
[instrument deregisterInstrumentors];
method_setImplementation(method, originalImp);
XCTAssertNotEqual([[NSURLSession sharedSession] class], [FPRNSURLSessionProxy class]);
}
/** Tests instrumenting an NSProxy wrapped NSURLSession object works. */
- (void)testProxyWrappedSessionWithConfigurationDelegateDelegateQueue {
SEL selector = @selector(sessionWithConfiguration:delegate:delegateQueue:);
Method method = class_getClassMethod([NSURLSession class], selector);
IMP originalImp = method_getImplementation(method);
IMP swizzledImp = imp_implementationWithBlock(
^(id session, NSURLSessionConfiguration *configuration, id<NSURLSessionDelegate> *delegate,
NSOperationQueue *delegateQueue) {
typedef NSURLSession *(*OriginalImp)(id, SEL, NSURLSessionConfiguration *,
id<NSURLSessionDelegate> *, NSOperationQueue *);
NSURLSession *originalSession =
((OriginalImp)originalImp)(session, selector, configuration, delegate, delegateQueue);
return [[FPRNSURLSessionProxy alloc] initWithSession:originalSession];
});
method_setImplementation(method, swizzledImp);
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
XCTAssertEqual([[NSURLSession sessionWithConfiguration:config delegate:nil
delegateQueue:nil] class],
[FPRNSURLSessionProxy class]);
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSession *session;
XCTAssertNoThrow(session = [NSURLSession sessionWithConfiguration:config
delegate:nil
delegateQueue:nil]);
NSURL *url = self.testServer.serverURL;
XCTestExpectation *expectation = [self expectationWithDescription:@"completionHandler"];
NSURLSessionDownloadTask *task =
[session downloadTaskWithURL:url
completionHandler:^(NSURL *_Nullable location, NSURLResponse *_Nullable response,
NSError *_Nullable error) {
[expectation fulfill];
}];
XCTAssertNotNil(task);
[task resume];
[self waitForExpectationsWithTimeout:10.0 handler:nil];
[instrument deregisterInstrumentors];
method_setImplementation(method, originalImp);
}
#pragma mark - Testing delegate method wrapping
/** Tests using a nil delegate still results in tracking responses. */
- (void)testSessionWithConfigurationDelegateDelegateQueueWithNilDelegate {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
delegate:nil
delegateQueue:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:self.testServer.serverURL];
NSURLSessionTask *task;
@autoreleasepool {
task = [session dataTaskWithRequest:request];
XCTAssertNotNil(task);
[task resume];
XCTAssertNotNil(session.delegate); // Not sure this is the desired behavior.
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:task]);
[self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
GCDWebServerResponse *_Nonnull response) {
XCTAssertNil([FPRNetworkTrace networkTraceFromObject:task]);
}];
}
[instrument deregisterInstrumentors];
}
/** Tests that the delegate class isn't instrumented more than once. */
- (void)testDelegateClassOnlyRegisteredOnce {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
FPRNSURLSessionCompleteTestDelegate *delegate =
[[FPRNSURLSessionCompleteTestDelegate alloc] init];
NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration defaultSessionConfiguration];
[NSURLSession sessionWithConfiguration:configuration delegate:delegate delegateQueue:nil];
[NSURLSession sessionWithConfiguration:configuration delegate:delegate delegateQueue:nil];
XCTAssertEqual(instrument.delegateInstrument.classInstrumentors.count, 1);
XCTAssertEqual(instrument.delegateInstrument.instrumentedClasses.count, 1);
[instrument deregisterInstrumentors];
}
/** Tests that the called delegate selector is wrapped and calls through. */
- (void)testDelegateURLSessionTaskDidCompleteWithError {
[self.testServer stop];
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
FPRNSURLSessionCompleteTestDelegate *delegate =
[[FPRNSURLSessionCompleteTestDelegate alloc] init];
// This request needs to fail.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://nonurl"]];
NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
delegate:delegate
delegateQueue:nil];
NSURLSessionTask *task;
@autoreleasepool {
task = [session dataTaskWithRequest:request];
[task resume];
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:task]);
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5.0]];
}
XCTAssertNil([FPRNetworkTrace networkTraceFromObject:task]);
XCTAssertTrue(delegate.URLSessionTaskDidCompleteWithErrorCalled);
[instrument deregisterInstrumentors];
[self.testServer start];
}
/** Tests that the called delegate selector is wrapped and calls through. */
- (void)testDelegateURLSessionTaskDidSendBodyDataTotalBytesSentTotalBytesExpectedToSend {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
FPRNSURLSessionCompleteTestDelegate *delegate =
[[FPRNSURLSessionCompleteTestDelegate alloc] init];
NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
delegate:delegate
delegateQueue:nil];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testUpload"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
request.HTTPMethod = @"POST";
NSBundle *bundle = [FPRTestUtils getBundle];
NSURL *fileURL = [bundle URLForResource:@"smallDownloadFile" withExtension:@""];
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromFile:fileURL];
[uploadTask resume];
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:uploadTask]);
FPRNetworkTrace *networkTrace = [FPRNetworkTrace networkTraceFromObject:uploadTask];
[self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
GCDWebServerResponse *_Nonnull response) {
XCTAssertTrue(delegate.URLSessionTaskDidSendBodyDataTotalBytesSentTotalBytesExpectedCalled);
XCTAssert(networkTrace.requestSize > 0);
XCTAssert(
[networkTrace
timeIntervalBetweenCheckpointState:FPRNetworkTraceCheckpointStateInitiated
andState:FPRNetworkTraceCheckpointStateRequestCompleted] > 0);
XCTAssertNil([FPRNetworkTrace networkTraceFromObject:uploadTask]);
}];
[instrument deregisterInstrumentors];
}
/** Tests that the called delegate selector is wrapped and calls through. */
- (void)testDelegateURLSessionTaskWillPerformHTTPRedirectionNewRequestCompletionHandler {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
FPRNSURLSessionCompleteTestDelegate *delegate =
[[FPRNSURLSessionCompleteTestDelegate alloc] init];
NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration defaultSessionConfiguration];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testRedirect"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
delegate:delegate
delegateQueue:nil];
NSURLSessionTask *task = [session dataTaskWithRequest:request];
[task resume];
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:task]);
[self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
GCDWebServerResponse *_Nonnull response) {
XCTAssertTrue(
delegate.URLSessionTaskWillPerformHTTPRedirectionNewRequestCompletionHandlerCalled);
}];
[instrument deregisterInstrumentors];
}
/** Tests that the called delegate selector is wrapped and calls through. */
- (void)testDelegateURLSessionDataTaskDidReceiveData {
FPRNSURLSessionInstrument *instrument;
NSURLSessionDataTask *dataTask;
@autoreleasepool {
instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
FPRNSURLSessionCompleteTestDelegate *delegate =
[[FPRNSURLSessionCompleteTestDelegate alloc] init];
NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
delegate:delegate
delegateQueue:nil];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testBigDownload"];
dataTask = [session dataTaskWithURL:URL];
[dataTask resume];
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:dataTask]);
[self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
GCDWebServerResponse *_Nonnull response) {
XCTAssertTrue(delegate.URLSessionDataTaskDidReceiveDataCalled);
XCTAssertNil([FPRNetworkTrace networkTraceFromObject:dataTask]);
}];
}
[instrument deregisterInstrumentors];
}
/** Tests that the called delegate selector is wrapped and calls through. */
- (void)testDelegateURLSessionDownloadTaskDidFinishDownloadingToURL {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
FPRNSURLSessionCompleteTestDelegate *delegate =
[[FPRNSURLSessionCompleteTestDelegate alloc] init];
NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
delegate:delegate
delegateQueue:nil];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testDownload"];
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:URL];
[downloadTask resume];
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:downloadTask]);
[self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
GCDWebServerResponse *_Nonnull response) {
XCTAssertTrue(delegate.URLSessionDownloadTaskDidFinishDownloadingToURLCalled);
XCTAssertNil([FPRNetworkTrace networkTraceFromObject:downloadTask]);
}];
[instrument deregisterInstrumentors];
}
/** Tests that the called delegate selector is wrapped and calls through. */
- (void)testDelegateURLSessionDownloadTaskDidResumeAtOffsetExpectedTotalBytes {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
FPRNSURLSessionTestDownloadDelegate *delegate =
[[FPRNSURLSessionTestDownloadDelegate alloc] init];
NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
delegate:delegate
delegateQueue:nil];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testBigDownload"];
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:URL];
[downloadTask resume];
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5.0]];
XCTAssertNil([FPRNetworkTrace networkTraceFromObject:downloadTask]);
XCTAssertTrue(delegate.URLSessionDownloadTaskDidResumeAtOffsetExpectedTotalBytesCalled);
[instrument deregisterInstrumentors];
}
/** Tests that the called delegate selector is wrapped and calls through. */
- (void)testDelegateURLSessionDownloadTaskDidWriteDataTotalBytesWrittenTotalBytesExpectedToWrite {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
FPRNSURLSessionCompleteTestDelegate *delegate =
[[FPRNSURLSessionCompleteTestDelegate alloc] init];
NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
delegate:delegate
delegateQueue:nil];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testBigDownload"];
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:URL];
[downloadTask resume];
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:downloadTask]);
[self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
GCDWebServerResponse *_Nonnull response) {
XCTAssertTrue(delegate.URLSessionDownloadTaskDidWriteDataTotalBytesWrittenTotalBytesCalled);
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
XCTAssertNil([FPRNetworkTrace networkTraceFromObject:downloadTask]);
}];
[instrument deregisterInstrumentors];
}
/** Tests that even if a delegate doesn't implement a method, we add it to the delegate class. */
- (void)testDelegateUnimplementedURLSessionTaskDidCompleteWithError {
FPRNSURLSessionTestDelegate *delegate = [[FPRNSURLSessionTestDelegate alloc] init];
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration defaultSessionConfiguration];
XCTAssertFalse([delegate respondsToSelector:@selector(URLSession:task:didCompleteWithError:)]);
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
delegate:delegate
delegateQueue:nil];
XCTAssertTrue([delegate respondsToSelector:@selector(URLSession:task:didCompleteWithError:)]);
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:self.testServer.serverURL];
[downloadTask resume];
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:downloadTask]);
[self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
GCDWebServerResponse *_Nonnull response) {
XCTAssertNil([FPRNetworkTrace networkTraceFromObject:downloadTask]);
}];
[instrument deregisterInstrumentors];
}
#pragma mark - Testing instance method wrapping
/** Tests that dataTaskWithRequest: returns a non-nil object. */
- (void)testDataTaskWithRequest {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSession *session = [NSURLSession sharedSession];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"test"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];
XCTAssertNotNil(dataTask);
[dataTask resume];
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:dataTask]);
[instrument deregisterInstrumentors];
}
/** Tests that dataTaskWithRequest:completionHandler: returns a non-nil object. */
- (void)testDataTaskWithRequestAndCompletionHandler {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSession *session = [NSURLSession sharedSession];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"test"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
XCTestExpectation *expectation = [self expectationWithDescription:@"completionHandler called"];
void (^completionHandler)(NSData *_Nullable, NSURLResponse *_Nullable, NSError *_Nullable) =
^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
[expectation fulfill];
};
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:completionHandler];
XCTAssertNotNil(dataTask);
[dataTask resume];
[self waitForExpectationsWithTimeout:10.0 handler:nil];
[instrument deregisterInstrumentors];
}
/** Tests that dataTaskWithRequest:completionHandler: for a POST request returns a non-nil object
* and collects request size. */
- (void)testDataTaskWithPostRequestAndCompletionHandler {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSession *session = [NSURLSession sharedSession];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"test"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
request.HTTPMethod = @"POST";
request.HTTPBody = [@"sampleData" dataUsingEncoding:NSUTF8StringEncoding];
XCTestExpectation *expectation = [self expectationWithDescription:@"completionHandler called"];
void (^completionHandler)(NSData *_Nullable, NSURLResponse *_Nullable, NSError *_Nullable) =
^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
[expectation fulfill];
};
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:completionHandler];
XCTAssertNotNil(dataTask);
[dataTask resume];
FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:dataTask];
XCTAssertEqual(trace.requestSize, 10);
[self waitForExpectationsWithTimeout:10.0 handler:nil];
[instrument deregisterInstrumentors];
}
/** Tests that dataTaskWithUrl:completionHandler: returns a non-nil object. */
- (void)testDataTaskWithUrlAndCompletionHandler {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSession *session = [NSURLSession sharedSession];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"test"];
XCTestExpectation *expectation = [self expectationWithDescription:@"completionHandler called"];
NSURLSessionDataTask *dataTask = nil;
void (^completionHandler)(NSData *_Nullable, NSURLResponse *_Nullable, NSError *_Nullable) =
^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
XCTAssertNil([FPRNetworkTrace networkTraceFromObject:dataTask]);
[expectation fulfill];
};
dataTask = [session dataTaskWithURL:URL completionHandler:completionHandler];
XCTAssertNotNil(dataTask);
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:dataTask]);
[dataTask resume];
[self waitForExpectationsWithTimeout:10.0 handler:nil];
[instrument deregisterInstrumentors];
}
/** Tests that uploadTaskWithRequest:fromFile: returns a non-nil object. */
- (void)testUploadTaskWithRequestFromFile {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSession *session = [NSURLSession sharedSession];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testUpload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSBundle *bundle = [FPRTestUtils getBundle];
NSURL *fileURL = [bundle URLForResource:@"smallDownloadFile" withExtension:@""];
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromFile:fileURL];
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:uploadTask]);
XCTAssertNotNil(uploadTask);
[uploadTask resume];
[instrument deregisterInstrumentors];
}
/** Tests that uploadTaskWithRequest:fromFile:completionHandler returns a non-nil object. */
- (void)testUploadTaskWithRequestFromFileCompletionHandler {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSession *session = [NSURLSession sharedSession];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testUpload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSBundle *bundle = [FPRTestUtils getBundle];
NSURL *fileURL = [bundle URLForResource:@"smallDownloadFile" withExtension:@""];
XCTestExpectation *expectation = [self expectationWithDescription:@"completionHandler called"];
void (^completionHandler)(NSData *_Nullable, NSURLResponse *_Nullable, NSError *_Nullable) =
^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
[expectation fulfill];
};
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
fromFile:fileURL
completionHandler:completionHandler];
XCTAssertNotNil(uploadTask);
[uploadTask resume];
[self waitForExpectationsWithTimeout:10.0 handler:nil];
[instrument deregisterInstrumentors];
}
/** Tests that uploadTaskWithRequest:fromData: returns a non-nil object. */
- (void)testUploadTaskWithRequestFromData {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSession *session = [NSURLSession sharedSession];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testUpload"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
request.HTTPMethod = @"POST";
NSData *data = [[NSData alloc] init];
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:data];
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:uploadTask]);
XCTAssertNotNil(uploadTask);
[uploadTask resume];
[self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
GCDWebServerResponse *_Nonnull response) {
XCTAssertEqual(response.statusCode, 200);
[instrument deregisterInstrumentors];
}];
}
/** Tests that uploadTaskWithRequest:fromData:completionHandler: returns a non-nil object. */
- (void)testUploadTaskWithRequestFromDataCompletionHandler {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSession *session = [NSURLSession sharedSession];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testUpload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
XCTestExpectation *expectation = [self expectationWithDescription:@"completionHandler called"];
void (^completionHandler)(NSData *_Nullable, NSURLResponse *_Nullable, NSError *_Nullable) =
^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
[expectation fulfill];
};
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
fromData:[[NSData alloc] init]
completionHandler:completionHandler];
XCTAssertNotNil(uploadTask);
[uploadTask resume];
[self waitForExpectationsWithTimeout:10.0 handler:nil];
[instrument deregisterInstrumentors];
}
/** Tests that uploadTaskWithStreamedRequest: returns a non-nil object. */
- (void)testUploadTaskWithStreamedRequest {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSession *session = [NSURLSession sharedSession];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testUpload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithStreamedRequest:request];
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:uploadTask]);
XCTAssertNotNil(uploadTask);
[uploadTask resume];
[instrument deregisterInstrumentors];
}
/** Tests that downloadTaskWithRequest: returns a non-nil object. */
- (void)testDownloadTaskWithRequest {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSession *session = [NSURLSession sharedSession];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testDownload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request];
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:downloadTask]);
XCTAssertNotNil(downloadTask);
[downloadTask resume];
[instrument deregisterInstrumentors];
}
/** Tests that downloadTaskWithRequest:completionHandler: returns a non-nil object. */
- (void)testDownloadTaskWithRequestCompletionHandler {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSession *session = [NSURLSession sharedSession];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testDownload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
XCTestExpectation *expectation = [self expectationWithDescription:@"completionHandler called"];
void (^completionHandler)(NSURL *_Nullable, NSURLResponse *_Nullable, NSError *_Nullable) =
^(NSURL *_Nullable location, NSURLResponse *_Nullable response, NSError *_Nullable error) {
[expectation fulfill];
};
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request
completionHandler:completionHandler];
XCTAssertNotNil(downloadTask);
[downloadTask resume];
[self waitForExpectationsWithTimeout:10.0 handler:nil];
[instrument deregisterInstrumentors];
}
/** Tests that downloadTaskWithUrl:completionHandler: returns a non-nil object. */
- (void)testDownloadTaskWithURLCompletionHandler {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURLSession *session = [NSURLSession sharedSession];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"test"];
XCTestExpectation *expectation = [self expectationWithDescription:@"completionHandler called"];
NSURLSessionDownloadTask *downloadTask = nil;
void (^completionHandler)(NSURL *_Nullable, NSURLResponse *_Nullable, NSError *_Nullable) =
^(NSURL *_Nullable location, NSURLResponse *_Nullable response, NSError *_Nullable error) {
XCTAssertNil([FPRNetworkTrace networkTraceFromObject:downloadTask]);
[expectation fulfill];
};
downloadTask = [session downloadTaskWithURL:URL completionHandler:completionHandler];
XCTAssertNotNil(downloadTask);
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:downloadTask]);
[downloadTask resume];
[self waitForExpectationsWithTimeout:10.0 handler:nil];
[instrument deregisterInstrumentors];
}
/** Validate that it works with NSMutableURLRequest URLs across data, upload, and download. */
- (void)testMutableRequestURLs {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testDownload"];
NSMutableURLRequest *URLRequest = [NSMutableURLRequest requestWithURL:URL];
NSURLSession *session = [NSURLSession
sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:URLRequest];
[dataTask resume];
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:dataTask]);
[self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
GCDWebServerResponse *_Nonnull response) {
XCTAssertNil([FPRNetworkTrace networkTraceFromObject:dataTask]);
}];
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:URLRequest];
[downloadTask resume];
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:downloadTask]);
[self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
GCDWebServerResponse *_Nonnull response) {
XCTAssertNil([FPRNetworkTrace networkTraceFromObject:downloadTask]);
}];
URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testUpload"];
URLRequest.URL = URL;
URLRequest.HTTPMethod = @"POST";
NSData *uploadData = [[NSData alloc] init];
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:URLRequest
fromData:uploadData];
[uploadTask resume];
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:uploadTask]);
[self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
GCDWebServerResponse *_Nonnull response) {
XCTAssertNil([FPRNetworkTrace networkTraceFromObject:uploadTask]);
}];
NSBundle *bundle = [FPRTestUtils getBundle];
NSURL *fileURL = [bundle URLForResource:@"smallDownloadFile" withExtension:@""];
uploadTask = [session uploadTaskWithRequest:URLRequest fromFile:fileURL];
[uploadTask resume];
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:uploadTask]);
[self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
GCDWebServerResponse *_Nonnull response) {
XCTAssertNil([FPRNetworkTrace networkTraceFromObject:uploadTask]);
}];
[instrument deregisterInstrumentors];
}
@end