FPRNSURLConnectionInstrumentTest.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma mark - Unswizzle based tests
  15. #import "FirebasePerformance/Tests/Unit/Instruments/FPRNSURLConnectionInstrumentTestDelegates.h"
  16. #import <XCTest/XCTest.h>
  17. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations+Private.h"
  18. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
  19. #import "FirebasePerformance/Sources/FPRClient.h"
  20. #import "FirebasePerformance/Sources/Instrumentation/Network/FPRNSURLConnectionInstrument.h"
  21. #import "FirebasePerformance/Sources/Public/FirebasePerformance/FIRPerformance.h"
  22. #import "FirebasePerformance/Tests/Unit/FPRTestCase.h"
  23. #import "FirebasePerformance/Tests/Unit/FPRTestUtils.h"
  24. #import "FirebasePerformance/Tests/Unit/Server/FPRHermeticTestServer.h"
  25. @interface FPRNSURLConnectionInstrumentTest : FPRTestCase
  26. /** Test server to create connections to. */
  27. @property(nonatomic) FPRHermeticTestServer *testServer;
  28. @end
  29. @implementation FPRNSURLConnectionInstrumentTest
  30. - (void)setUp {
  31. [super setUp];
  32. FIRPerformance *performance = [FIRPerformance sharedInstance];
  33. [performance setDataCollectionEnabled:YES];
  34. self.testServer = [[FPRHermeticTestServer alloc] init];
  35. [self.testServer registerTestPaths];
  36. [self.testServer start];
  37. }
  38. - (void)tearDown {
  39. [super tearDown];
  40. FIRPerformance *performance = [FIRPerformance sharedInstance];
  41. [performance setDataCollectionEnabled:NO];
  42. [self.testServer stop];
  43. self.testServer = nil;
  44. }
  45. /** Waits for the server connection to finish by giving a block to run just before a response is
  46. * sent.
  47. *
  48. * @param block A block to run just after the server response is sent.
  49. */
  50. - (void)waitAndRunBlockAfterResponse:(void (^)(id self,
  51. GCDWebServerRequest *_Nonnull request,
  52. GCDWebServerResponse *_Nonnull response))block {
  53. __block BOOL loopingMainThread = YES;
  54. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  55. dispatch_semaphore_t sema = dispatch_semaphore_create(0);
  56. __weak id weakSelf = self;
  57. self.testServer.responseCompletedBlock =
  58. ^(GCDWebServerRequest *_Nonnull request, GCDWebServerResponse *_Nonnull response) {
  59. block(weakSelf, request, response);
  60. dispatch_semaphore_signal(sema);
  61. };
  62. XCTAssertEqual(
  63. dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC)), 0);
  64. loopingMainThread = NO;
  65. });
  66. // This is necessary because the FPRHermeticTestServer callbacks occur on the main thread.
  67. while (loopingMainThread) {
  68. [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
  69. }
  70. }
  71. /** Tests calling +sendAsynchronousRequest:queue:completionHandler: is wrapped and calls
  72. * through.
  73. */
  74. - (void)testSendAsynchronousRequestQueueCompletionHandler {
  75. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  76. [instrument registerInstrumentors];
  77. XCTestExpectation *expectation = [self expectationWithDescription:@"completionHandler was run"];
  78. NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"test"];
  79. NSURLRequest *request = [NSURLRequest requestWithURL:URL];
  80. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  81. [NSURLConnection
  82. sendAsynchronousRequest:request
  83. queue:queue
  84. completionHandler:^(NSURLResponse *_Nullable response, NSData *_Nullable data,
  85. NSError *_Nullable connectionError) {
  86. XCTAssertNil(connectionError);
  87. XCTAssertGreaterThan(data.length, 0);
  88. [expectation fulfill];
  89. }];
  90. [self waitForExpectationsWithTimeout:10.0 handler:nil];
  91. [instrument deregisterInstrumentors];
  92. }
  93. /** Tests calling +sendAsynchronousRequest:queue:completionHandler: is wrapped and calls
  94. * through, even with a nil completionHandler.
  95. */
  96. - (void)testSendAsynchronousRequestQueueWithNilCompletionHandler {
  97. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  98. [instrument registerInstrumentors];
  99. NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"test"];
  100. NSURLRequest *request = [NSURLRequest requestWithURL:URL];
  101. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  102. #pragma clang diagnostic push
  103. #pragma clang diagnostic ignored "-Wnonnull"
  104. [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:nil];
  105. #pragma clang diagnostic pop
  106. // Wait for a moment to ensure that the request has gone through.
  107. [self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
  108. GCDWebServerResponse *_Nonnull response) {
  109. XCTAssertEqualObjects(request.URL.absoluteString, URL.absoluteString);
  110. XCTAssertEqual(response.statusCode, 200);
  111. }];
  112. [instrument deregisterInstrumentors];
  113. }
  114. /** Tests calling -initWithRequest:delegate: is wrapped and calls through. */
  115. - (void)testInitWithRequestDelegate {
  116. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  117. [instrument registerInstrumentors];
  118. NSURLRequest *request = [NSURLRequest requestWithURL:self.testServer.serverURL];
  119. [self.testServer stop];
  120. FPRNSURLConnectionCompleteTestDelegate *delegate =
  121. [[FPRNSURLConnectionCompleteTestDelegate alloc] init];
  122. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
  123. XCTAssertNotNil(connection);
  124. [connection start];
  125. // Only let it check for a connection for a half second.
  126. [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
  127. XCTAssertTrue(delegate.connectionDidFailWithErrorCalled);
  128. [self.testServer start];
  129. [instrument deregisterInstrumentors];
  130. }
  131. /** Tests calling -initWithRequest:delegate: is wrapped and calls through for NSOperation based
  132. * requests.
  133. */
  134. - (void)testInitWithOperationRequestDelegate {
  135. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  136. [instrument registerInstrumentors];
  137. NSURLRequest *request = [NSURLRequest requestWithURL:self.testServer.serverURL];
  138. [self.testServer stop];
  139. FPRNSURLConnectionOperationTestDelegate *delegate =
  140. [[FPRNSURLConnectionOperationTestDelegate alloc] init];
  141. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
  142. XCTAssertNotNil(connection);
  143. [connection start];
  144. // Only let it check for a connection for a half second.
  145. [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
  146. XCTAssertTrue(delegate.connectionDidFailWithErrorCalled);
  147. [self.testServer start];
  148. [instrument deregisterInstrumentors];
  149. }
  150. /** Tests calling -initWithRequest:delegate: is wrapped and calls through with nil delegate. */
  151. - (void)testInitWithRequestAndNilDelegate {
  152. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  153. [instrument registerInstrumentors];
  154. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.testServer.serverURL];
  155. [request setTimeoutInterval:10.0];
  156. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
  157. delegate:nil
  158. startImmediately:NO];
  159. XCTAssertNotNil(connection);
  160. [connection start];
  161. XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:connection]);
  162. [self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
  163. GCDWebServerResponse *_Nonnull response) {
  164. XCTAssertNil([FPRNetworkTrace networkTraceFromObject:connection]);
  165. }];
  166. [instrument deregisterInstrumentors];
  167. }
  168. /** Tests calling -initWithRequest:delegate:startImmediately: doesn't install a delegate. */
  169. - (void)testInitWithRequestDelegateStartImmediately {
  170. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  171. [instrument registerInstrumentors];
  172. NSURLRequest *request = [NSURLRequest requestWithURL:self.testServer.serverURL];
  173. [self.testServer stop];
  174. FPRNSURLConnectionCompleteTestDelegate *delegate =
  175. [[FPRNSURLConnectionCompleteTestDelegate alloc] init];
  176. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
  177. delegate:delegate
  178. startImmediately:NO];
  179. XCTAssertNotNil(connection);
  180. [connection start];
  181. XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:connection]);
  182. [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
  183. XCTAssertTrue(delegate.connectionDidFailWithErrorCalled);
  184. XCTAssertNil([FPRNetworkTrace networkTraceFromObject:connection]);
  185. [self.testServer start];
  186. [instrument deregisterInstrumentors];
  187. }
  188. /** Tests calling +connectionWithRequest:delegate: calls already wrapped methods. */
  189. - (void)testConnectionWithRequestDelegate {
  190. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  191. [instrument registerInstrumentors];
  192. NSURLRequest *request = [NSURLRequest requestWithURL:self.testServer.serverURL];
  193. FPRNSURLConnectionCompleteTestDelegate *delegate =
  194. [[FPRNSURLConnectionCompleteTestDelegate alloc] init];
  195. NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:delegate];
  196. [connection start];
  197. XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:connection]);
  198. [self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
  199. GCDWebServerResponse *_Nonnull response) {
  200. XCTAssertNil([FPRNetworkTrace networkTraceFromObject:connection]);
  201. }];
  202. [instrument deregisterInstrumentors];
  203. }
  204. /** Tests calling -start is wrapped and calls through. */
  205. - (void)testStart {
  206. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  207. [instrument registerInstrumentors];
  208. NSURLRequest *request = [NSURLRequest requestWithURL:self.testServer.serverURL];
  209. FPRNSURLConnectionCompleteTestDelegate *delegate =
  210. [[FPRNSURLConnectionCompleteTestDelegate alloc] init];
  211. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
  212. XCTAssertNotNil(connection);
  213. [connection start];
  214. XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:connection]);
  215. [self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
  216. GCDWebServerResponse *_Nonnull response) {
  217. XCTAssertNil([FPRNetworkTrace networkTraceFromObject:connection]);
  218. }];
  219. [instrument deregisterInstrumentors];
  220. }
  221. /** Tests calling -start through startImmediately: is wrapped and calls through. */
  222. - (void)testStartThroughStartImmediately {
  223. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  224. [instrument registerInstrumentors];
  225. NSURLRequest *request = [NSURLRequest requestWithURL:self.testServer.serverURL];
  226. FPRNSURLConnectionCompleteTestDelegate *delegate =
  227. [[FPRNSURLConnectionCompleteTestDelegate alloc] init];
  228. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
  229. XCTAssertNotNil(connection);
  230. XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:connection]);
  231. [connection start];
  232. [self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
  233. GCDWebServerResponse *_Nonnull response) {
  234. XCTAssertNil([FPRNetworkTrace networkTraceFromObject:connection]);
  235. }];
  236. [instrument deregisterInstrumentors];
  237. }
  238. /** Tests calling -cancel is wrapped and calls through. */
  239. - (void)testCancel {
  240. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  241. [instrument registerInstrumentors];
  242. NSURLRequest *request = [NSURLRequest requestWithURL:self.testServer.serverURL];
  243. FPRNSURLConnectionCompleteTestDelegate *delegate =
  244. [[FPRNSURLConnectionCompleteTestDelegate alloc] init];
  245. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
  246. XCTAssertNotNil(connection);
  247. XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:connection]);
  248. [connection start];
  249. [connection cancel];
  250. XCTAssertNil([FPRNetworkTrace networkTraceFromObject:connection]);
  251. [instrument deregisterInstrumentors];
  252. }
  253. #pragma mark - Delegate methods
  254. /** Tests calling -connection:didFailWithError: is wrapped and calls through. */
  255. - (void)testConnectionDidFailWithError {
  256. self.appFake.fakeIsDataCollectionDefaultEnabled = YES;
  257. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  258. [instrument registerInstrumentors];
  259. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://nonurl/"]];
  260. [self.testServer stop];
  261. [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.2]];
  262. FPRNSURLConnectionCompleteTestDelegate *delegate =
  263. [[FPRNSURLConnectionCompleteTestDelegate alloc] init];
  264. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
  265. [connection start];
  266. XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:connection]);
  267. [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]];
  268. XCTAssertTrue(delegate.connectionDidFailWithErrorCalled);
  269. XCTAssertNil([FPRNetworkTrace networkTraceFromObject:connection]);
  270. [self.testServer start];
  271. [instrument deregisterInstrumentors];
  272. }
  273. /** Tests calling -connection:willSendRequest:redirectResponse: is wrapped and calls through.*/
  274. - (void)testConnectionWillSendRequestRedirectResponse {
  275. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  276. [instrument registerInstrumentors];
  277. NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testRedirect"];
  278. NSURLRequest *request = [NSURLRequest requestWithURL:URL];
  279. FPRNSURLConnectionCompleteTestDelegate *delegate =
  280. [[FPRNSURLConnectionCompleteTestDelegate alloc] init];
  281. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
  282. [connection start];
  283. XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:connection]);
  284. [self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
  285. GCDWebServerResponse *_Nonnull response) {
  286. XCTAssertTrue(delegate.connectionWillSendRequestRedirectResponseCalled);
  287. }];
  288. [instrument deregisterInstrumentors];
  289. }
  290. /** Tests calling -connection:didReceiveResponse: is wrapped and calls through. */
  291. - (void)testConnectionDidReceiveResponse {
  292. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  293. [instrument registerInstrumentors];
  294. NSURLRequest *request = [NSURLRequest requestWithURL:self.testServer.serverURL];
  295. FPRNSURLConnectionCompleteTestDelegate *delegate =
  296. [[FPRNSURLConnectionCompleteTestDelegate alloc] init];
  297. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
  298. [connection start];
  299. XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:connection]);
  300. [self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
  301. GCDWebServerResponse *_Nonnull response) {
  302. XCTAssertTrue(delegate.connectionDidReceiveResponseCalled);
  303. }];
  304. [instrument deregisterInstrumentors];
  305. }
  306. /** Tests calling -connection:didReceiveData: is wrapped and calls through. */
  307. - (void)testConnectionDidReceiveData {
  308. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  309. [instrument registerInstrumentors];
  310. NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testBigDownload"];
  311. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
  312. FPRNSURLConnectionDidReceiveDataDelegate *delegate =
  313. [[FPRNSURLConnectionDidReceiveDataDelegate alloc] init];
  314. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
  315. [connection start];
  316. XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:connection]);
  317. [self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
  318. GCDWebServerResponse *_Nonnull response) {
  319. XCTAssertTrue(delegate.connectionDidReceiveDataCalled);
  320. }];
  321. [instrument deregisterInstrumentors];
  322. }
  323. /** Tests calling -connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: is
  324. * wrapped and calls through.
  325. */
  326. - (void)testConnectionDidSendBodyDataTotalBytesWrittenTotalBytesExpectedToWrite {
  327. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  328. [instrument registerInstrumentors];
  329. NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testUpload"];
  330. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
  331. request.HTTPMethod = @"POST";
  332. NSBundle *bundle = [FPRTestUtils getBundle];
  333. NSURL *fileURL = [bundle URLForResource:@"smallDownloadFile" withExtension:@""];
  334. request.HTTPBody = [NSData dataWithContentsOfURL:fileURL];
  335. FPRNSURLConnectionCompleteTestDelegate *delegate =
  336. [[FPRNSURLConnectionCompleteTestDelegate alloc] init];
  337. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
  338. [connection start];
  339. XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:connection]);
  340. FPRNetworkTrace *networkTrace = [FPRNetworkTrace networkTraceFromObject:connection];
  341. [self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
  342. GCDWebServerResponse *_Nonnull response) {
  343. XCTAssertTrue(
  344. delegate.connectionDidSendBodyDataTotalBytesWrittenTotalBytesExpectedToWriteCalled);
  345. XCTAssert(networkTrace.requestSize > 0);
  346. XCTAssert(
  347. [networkTrace
  348. timeIntervalBetweenCheckpointState:FPRNetworkTraceCheckpointStateInitiated
  349. andState:FPRNetworkTraceCheckpointStateRequestCompleted] > 0);
  350. XCTAssertNil([FPRNetworkTrace networkTraceFromObject:connection]);
  351. }];
  352. [instrument deregisterInstrumentors];
  353. }
  354. /** Tests calling -connectionDidFinishLoading: is wrapped and calls through. */
  355. - (void)testConnectionDidFinishLoading {
  356. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  357. [instrument registerInstrumentors];
  358. NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testDownload"];
  359. NSURLRequest *request = [NSURLRequest requestWithURL:URL];
  360. FPRNSURLConnectionDidReceiveDataDelegate *delegate =
  361. [[FPRNSURLConnectionDidReceiveDataDelegate alloc] init];
  362. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
  363. [connection start];
  364. XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:connection]);
  365. [self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
  366. GCDWebServerResponse *_Nonnull response) {
  367. XCTAssertTrue(delegate.connectionDidFinishLoadingCalled);
  368. XCTAssertNil([FPRNetworkTrace networkTraceFromObject:connection]);
  369. }];
  370. [instrument deregisterInstrumentors];
  371. }
  372. /** Tests calling -connection:didWriteData:totalBytesWritten:expectedTotalBytes is wrapped and
  373. * calls through.
  374. */
  375. - (void)testConnectionDidWriteDataTotalBytesWrittenExpectedTotalBytes {
  376. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  377. [instrument registerInstrumentors];
  378. NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testUpload"];
  379. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
  380. request.HTTPMethod = @"POST";
  381. FPRNSURLConnectionCompleteTestDelegate *delegate =
  382. [[FPRNSURLConnectionCompleteTestDelegate alloc] init];
  383. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
  384. [connection start];
  385. XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:connection]);
  386. [self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
  387. GCDWebServerResponse *_Nonnull response) {
  388. XCTAssertTrue(delegate.connectionDidWriteDataTotalBytesWrittenExpectedTotalBytesCalled);
  389. XCTAssertNil([FPRNetworkTrace networkTraceFromObject:connection]);
  390. }];
  391. [instrument deregisterInstrumentors];
  392. }
  393. /** Tests calling -connectionDidFinishDownloading:destinationURL: is wrapped and calls through.
  394. */
  395. - (void)testConnectionDidFinishDownloadingDestinationURL {
  396. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  397. [instrument registerInstrumentors];
  398. dispatch_queue_t queue = dispatch_queue_create([NSStringFromSelector(_cmd) UTF8String], 0);
  399. dispatch_async(queue, ^{
  400. });
  401. NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testDownload"];
  402. NSURLRequest *request = [NSURLRequest requestWithURL:URL];
  403. FPRNSURLConnectionCompleteTestDelegate *delegate =
  404. [[FPRNSURLConnectionCompleteTestDelegate alloc] init];
  405. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
  406. [connection start];
  407. XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:connection]);
  408. [self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
  409. GCDWebServerResponse *_Nonnull response) {
  410. XCTAssertTrue(delegate.connectionDidFinishDownloadingDestinationURLCalled);
  411. XCTAssertNil([FPRNetworkTrace networkTraceFromObject:connection]);
  412. }];
  413. [instrument deregisterInstrumentors];
  414. }
  415. /** Tests NSURLDownloadDelegate completion methods gets called even after SDK swizzles that
  416. * APIs.
  417. */
  418. - (void)testDownloadDelegateCompletionAPIGetsCalled {
  419. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  420. [instrument registerInstrumentors];
  421. NSURLRequest *request = [NSURLRequest requestWithURL:self.testServer.serverURL];
  422. FPRNSURLConnectionDownloadTestDelegate *delegate =
  423. [[FPRNSURLConnectionDownloadTestDelegate alloc] init];
  424. XCTAssertFalse([delegate respondsToSelector:@selector(connectionDidFinishLoading:)]);
  425. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
  426. [connection start];
  427. XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:connection]);
  428. [self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
  429. GCDWebServerResponse *_Nonnull response) {
  430. XCTAssertTrue(delegate.connectionDidFinishDownloadingDestinationURLCalled);
  431. XCTAssertNil([FPRNetworkTrace networkTraceFromObject:connection]);
  432. }];
  433. [instrument deregisterInstrumentors];
  434. }
  435. /** Tests NSURLDataDelegate completion handler gets called even after SDK swizzles that APIs.
  436. */
  437. - (void)testDataDelegateCompletionAPIGetsCalled {
  438. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  439. [instrument registerInstrumentors];
  440. NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testDownload"];
  441. NSURLRequest *request = [NSURLRequest requestWithURL:URL];
  442. FPRNSURLConnectionDataTestDelegate *delegate = [[FPRNSURLConnectionDataTestDelegate alloc] init];
  443. SEL selector = @selector(connectionDidFinishDownloading:destinationURL:);
  444. XCTAssertFalse([delegate respondsToSelector:selector]);
  445. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
  446. [connection start];
  447. XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:connection]);
  448. [self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
  449. GCDWebServerResponse *_Nonnull response) {
  450. XCTAssertTrue(delegate.connectionDidFinishLoadingCalled);
  451. XCTAssertNil([FPRNetworkTrace networkTraceFromObject:connection]);
  452. }];
  453. [instrument deregisterInstrumentors];
  454. }
  455. /** Tests NSURLDownloadDelegate completion methods gets called even if NSURLDataDelegate is
  456. * implemented.
  457. */
  458. - (void)testDownloadDelegateCompletionAPIGetsCalledEvenIfDataDelegateIsImplemented {
  459. FPRNSURLConnectionInstrument *instrument = [[FPRNSURLConnectionInstrument alloc] init];
  460. [instrument registerInstrumentors];
  461. NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testDownload"];
  462. NSURLRequest *request = [NSURLRequest requestWithURL:URL];
  463. FPRNSURLConnectionCompleteTestDelegate *delegate =
  464. [[FPRNSURLConnectionCompleteTestDelegate alloc] init];
  465. SEL selector = @selector(connectionDidFinishDownloading:destinationURL:);
  466. XCTAssertTrue([delegate respondsToSelector:selector]);
  467. XCTAssertTrue([delegate respondsToSelector:@selector(connectionDidFinishLoading:)]);
  468. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
  469. [connection start];
  470. XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:connection]);
  471. [self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
  472. GCDWebServerResponse *_Nonnull response) {
  473. XCTAssertTrue(delegate.connectionDidFinishDownloadingDestinationURLCalled);
  474. XCTAssertFalse(delegate.connectionDidFinishLoadingCalled);
  475. XCTAssertNil([FPRNetworkTrace networkTraceFromObject:connection]);
  476. }];
  477. [instrument deregisterInstrumentors];
  478. }
  479. @end