Anders Carlsson avatar Anders Carlsson committed 3ae454d

Add test for startImmediately:NO behaviour.

Comments (0)

Files changed (1)

NSURLConnectionVCRTests/NSURLConnectionVCRTests.m

 @property (nonatomic, readonly) NSURLResponse* response;
 @property (nonatomic, readonly) NSData* responseData;
 @property (nonatomic, readonly) NSError* error;
+@property (nonatomic, readonly) BOOL isStarted;
 @property (nonatomic, readonly) BOOL isDone;
 @end
 
     STAssertTrue([hotVCRDelegate.response isEqualIgnoringVolatileHTTPHeaders:noVCRDelegate.response], @"Response without VCR is expected to be equal to response with cold VCR.");
 }
 
+- (void)testNotStartingImmediately {
+    NSURLConnection* connection;
+    NSURLRequest* request = [NSURLRequest requestWithURL:testURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
+
+    // Do request with no VCR:
+    [NSURLConnectionVCR stopVCRWithError:nil];
+    ConnectionDelegate *noVCRDelegate = [[ConnectionDelegate alloc] init];
+    connection = [[NSURLConnection alloc] initWithRequest:request delegate:noVCRDelegate startImmediately:NO];
+    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
+    STAssertFalse(noVCRDelegate.isStarted, @"Request should not start emmediately");
+
+    [connection start];
+    while ([noVCRDelegate isDone] == NO) {
+        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
+    }
+    if (noVCRDelegate.responseData == nil || noVCRDelegate.error) {
+        STFail(@"Problem fetching testURL (%@): %@", testURL, noVCRDelegate.error);
+    }
+
+    // Do request with cold VCR:
+    [NSURLConnectionVCR startVCRWithPath:tapesPath error:nil];
+    if ([NSURLConnectionVCR hasCacheForRequest:request]) {
+        [NSURLConnectionVCR deleteCacheForRequest:request error:nil];
+    }
+    ConnectionDelegate *coldVCRDelegate = [[ConnectionDelegate alloc] init];
+    connection = [[NSURLConnection alloc] initWithRequest:request delegate:coldVCRDelegate startImmediately:NO];
+    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
+    STAssertFalse(coldVCRDelegate.isStarted, @"Request should not start emmediately");
+
+    [connection start];
+    while ([coldVCRDelegate isDone] == NO) {
+        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
+    }
+    if (coldVCRDelegate.responseData == nil || coldVCRDelegate.error) {
+        STFail(@"Problem fetching testURL with cold VCR (%@): %@", testURL, coldVCRDelegate.error);
+    }
+    STAssertTrue([coldVCRDelegate.responseData isEqual:noVCRDelegate.responseData], @"Response body without VCR is expected to be equal to response body with cold VCR.");
+    STAssertTrue([coldVCRDelegate.response isEqualIgnoringVolatileHTTPHeaders:noVCRDelegate.response], @"Response without VCR is expected to be equal to response with cold VCR.");
+
+    // Do request with hot VCR:
+    [BreakableHTTPConnection setBroken:YES];
+    ConnectionDelegate *hotVCRDelegate = [[ConnectionDelegate alloc] init];
+    connection = [[NSURLConnection alloc] initWithRequest:request delegate:hotVCRDelegate startImmediately:NO];
+    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
+    STAssertFalse(hotVCRDelegate.isStarted, @"Request should not start emmediately");
+
+    [connection start];
+    while ([hotVCRDelegate isDone] == NO) {
+        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
+    }
+    if (hotVCRDelegate.responseData == nil || hotVCRDelegate.error) {
+        STFail(@"Problem fetching testURL with hot VCR (%@): %@", testURL, hotVCRDelegate.error);
+    }
+    STAssertTrue([hotVCRDelegate.responseData isEqual:noVCRDelegate.responseData], @"Response body without VCR is expected to be equal to response body with cold VCR.");
+    STAssertTrue([hotVCRDelegate.response isEqualIgnoringVolatileHTTPHeaders:noVCRDelegate.response], @"Response without VCR is expected to be equal to response with cold VCR.");
+}
+
 //- (void)testZzzSleepSoInstrumentsCanFindLeaks {
 //    [NSThread sleepForTimeInterval:11.];
 //}
     NSURLResponse* response;
     NSMutableData* responseData;
     NSError* error;
+    BOOL isStarted;
     BOOL isDone;
 }
 @synthesize response;
 @synthesize responseData;
 @synthesize error;
+@synthesize isStarted;
 @synthesize isDone;
 
 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)theError {
 }
 
 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)theResponse {
+    isStarted = YES;
     response = theResponse;
 }
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.