Source

NSURLConnectionVCR / NSURLConnectionVCRTests / NSURLConnectionVCRTests.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
//
//  NSURLConnectionVCRTests.m
//  NSURLConnectionVCRTests
//
//  Created by Martijn Thé on 2/25/12.
//  Copyright (c) 2012 martijnthe.nl. All rights reserved.
//

#import "NSURLConnectionVCRTests.h"
#import "NSURLConnectionVCR.h"
#import "HTTPServer.h"
#import "HTTPConnection.h"

// The SRCROOT preprocessor variable is set using the "Preprocessor Macros Not Used in Precompiled Headers" (GCC_PREPROCESSOR_DEFINITIONS_NOT_USED_IN_PRECOMPS) build setting.
// This way the caches can be committed together with the tests as part of your project's source, which is makes the tests independent on internet connectivity, if run once and committed.

#define QUOTE(str) #str
#define EXPAND_AND_QUOTE(str) QUOTE(str)

@interface BreakableHTTPConnection : HTTPConnection
+ (void)setBroken:(BOOL)broken;
@end

@interface NSURLResponse (Testing)
- (BOOL)isEqualIgnoringVolatileHTTPHeaders:(NSURLResponse*)otherResponse;
@end

@interface ConnectionDelegate : NSObject <NSURLConnectionDelegate>
@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

@implementation NSURLConnectionVCRTests {
    NSString* tapesPath;
    NSString* docRoot;
    NSString* testFilename;
    NSURL* testURL;
    HTTPServer* httpServer;
    NSOperationQueue* bgQueue;
}

- (void)setUp {
    [super setUp];
    
    NSString* srcroot = [NSString stringWithFormat:@"%s", EXPAND_AND_QUOTE(SRCROOT)];
    tapesPath = [srcroot stringByAppendingPathComponent:@"NSURLConnectionVCRTests/VCRTapes"];
    
    // These tests will run against a local server, so they run fast and are not dependent on an external server:
    httpServer = [[HTTPServer alloc] init];
    [httpServer setConnectionClass:[BreakableHTTPConnection class]];
    UInt16 port = 2048;
    [httpServer setPort:port];
    docRoot = [srcroot stringByAppendingPathComponent:@"NSURLConnectionVCRTests/htdocs"];
    [httpServer setDocumentRoot:docRoot];
    
    NSError *error = nil;
    
    int retries = 0;
    while ([httpServer start:&error] == NO) {
        [NSThread sleepForTimeInterval:0.1];
        ++retries;
        if (retries > 20) {
            STFail(@"Error starting HTTP Server: %@", error);
            break;
        }
    }
    
    // testURL = [NSURL URLWithString:@"https://raw.github.com/gist/1909468/267a404619b83a3d897b10d9904fd9644f5486d2/NSURLConnectionVCRTest.json"];
    testFilename = @"TestResponse.json";
    testURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:%u/%@", port, testFilename]];
    
    bgQueue = [[NSOperationQueue alloc] init];
    [bgQueue setMaxConcurrentOperationCount:1];
    
    [BreakableHTTPConnection setBroken:NO];
    [NSURLConnectionVCR startVCRWithPath:tapesPath error:nil];
}

- (void)tearDown {
    [super tearDown];
    [NSURLConnectionVCR stopVCRWithError:nil];
    [httpServer stop];
}

- (void)testBrokenServer {
    [NSURLConnectionVCR stopVCRWithError:nil];
    NSURLRequest* request = [NSURLRequest requestWithURL:testURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
    NSHTTPURLResponse* response = nil;
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    STAssertTrue([response statusCode] == 200, @"Expecting 200 status");
    
    [BreakableHTTPConnection setBroken:YES];
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    STAssertTrue([response statusCode] == 400, @"Expecting 400 status after breaking the server");
}

- (void)testStarted {
    STAssertTrue([NSURLConnectionVCR isVCRStarted], @"VCR expected to be started");
    id connection = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:testURL] delegate:self];
    STAssertTrue([connection class] == [NSURLConnectionVCR class], @"[NSURLConnection alloc] is expected to create a NSURLConnectionVCR object.");
}

- (void)testAlreadyStarted {
    NSError* error = nil;
    [NSURLConnectionVCR startVCRWithPath:tapesPath error:&error];
    STAssertTrue(error && [error code] == NSURLConnectionVCRErrorAlreadyStarted, @"Expecting error, because already started.");
}

- (void)testAlreadyStopped {
    NSError* error = nil;
    BOOL success;
    success = [NSURLConnectionVCR stopVCRWithError:&error];
    STAssertTrue(success && error == nil, @"Expecting no errors, because should be able to stop when VCR is started.");
    success = [NSURLConnectionVCR stopVCRWithError:&error];
    STAssertTrue(success == NO && error && [error code] == NSURLConnectionVCRErrorAlreadyStopped, @"Expecting error, because already stopped.");
}

- (void)testSetNonExistingPath {
    NSError* error = nil;
    NSString* randomPath;
    BOOL fileExists;
    NSFileManager* fm = [NSFileManager defaultManager];
    do {
        randomPath = [NSTemporaryDirectory() stringByAppendingPathExtension:[NSString stringWithFormat:@"%i", arc4random()]];
        fileExists = [fm fileExistsAtPath:randomPath];
    } while (fileExists == YES);
    BOOL success = [NSURLConnectionVCR setPath:randomPath error:&error];
    STAssertTrue(success && error == nil, @"Expecting no errors when setting a non-existing path");
}

- (void)testSetNilPath {
    NSError* error = nil;
    BOOL success = [NSURLConnectionVCR setPath:nil error:&error];
    STAssertTrue(success && error == nil, @"Expecting no error when setting nil path");
}

- (void)testSetNonExistingInvalidPath {
    NSError* error = nil;
    BOOL success = [NSURLConnectionVCR setPath:@"/:invalid:path" error:&error];
    STAssertTrue(success == NO && error && [error code] == NSURLConnectionVCRErrorCouldNotCreateDirectory, @"Expecting error when setting an invalid path");
}

- (void)testStartWithInvalidPath {
    [NSURLConnectionVCR stopVCRWithError:nil];
    NSError* error = nil;
    BOOL success = [NSURLConnectionVCR startVCRWithPath:@"/:invalid:path" error:&error];
    STAssertTrue(success == NO && error && [error code] == NSURLConnectionVCRErrorCouldNotCreateDirectory, @"Expecting error when setting an invalid path");    
}

- (void)testSetPathNotADirectory {
    NSError* error = nil;
    NSBundle* testBundle = [NSBundle bundleForClass:[self class]];
    NSString* filePath = [testBundle executablePath];
    BOOL success = [NSURLConnectionVCR setPath:filePath error:&error];
    STAssertTrue(success == NO && error && [error code] == NSURLConnectionVCRErrorPathIsNotADirectory, @"Expecting error when setting a path that refers to a file");
}

- (void)testSynchronousRequest {
    NSError* error = nil;
    NSURLRequest* request = [NSURLRequest requestWithURL:testURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
    
    // Do request with no VCR:
    [NSURLConnectionVCR stopVCRWithError:nil];
    NSURLResponse* responseNoVCR = nil;
    NSData* dataNoVCR = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseNoVCR error:&error];
    if (dataNoVCR == nil || error) {
        STFail(@"Problem fetching testURL (%@): %@", testURL, error);
    }
    
    // Do request with cold VCR:
    [NSURLConnectionVCR startVCRWithPath:tapesPath error:&error];
    if ([NSURLConnectionVCR hasCacheForRequest:request]) {
        [NSURLConnectionVCR deleteCacheForRequest:request error:nil];
    }
    NSURLResponse* responseColdVCR = nil;
    NSData* dataColdVCR = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseColdVCR error:&error];
    if (dataColdVCR == nil || error) {
        STFail(@"Problem fetching testURL with cold VCR (%@): %@", testURL, error);
    }
    STAssertTrue([dataColdVCR isEqual:dataNoVCR], @"Response body without VCR is expected to be equal to response body with cold VCR.");
    STAssertTrue([responseColdVCR isEqualIgnoringVolatileHTTPHeaders:responseNoVCR], @"Response without VCR is expected to be equal to response with cold VCR.");

    // Do request with hot VCR:
    [BreakableHTTPConnection setBroken:YES];
    STAssertTrue([NSURLConnectionVCR hasCacheForRequest:request], @"VCR is expected to have a cache for the request by now.");
    NSURLResponse* responseHotVCR = nil;
    NSData* dataHotVCR = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseHotVCR error:&error];
    if (dataHotVCR == nil || error) {
        STFail(@"Problem fetching testURL with hot VCR (%@): %@", testURL, error);
    }
    STAssertTrue([dataHotVCR isEqual:dataNoVCR], @"Response body without VCR is expected to be equal to response body with cold VCR.");
    STAssertTrue([responseHotVCR isEqualIgnoringVolatileHTTPHeaders:responseNoVCR], @"Response without VCR is expected to be equal to response with cold VCR.");
}

- (void)testAsynchronousRequestWithQueues {
    __block NSError* error = nil;
    __block BOOL done = NO;
    NSURLRequest* request = [NSURLRequest requestWithURL:testURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
    
    // Do request with no VCR:
    [NSURLConnectionVCR stopVCRWithError:nil];
    __block NSURLResponse* responseNoVCR = nil;
    __block NSData* dataNoVCR = nil;
    [NSURLConnection sendAsynchronousRequest:request queue:bgQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *theError) {
        responseNoVCR = response;
        dataNoVCR = data;
        error = theError;
        done = YES;
    }];
    while (done == NO) {
        [NSThread sleepForTimeInterval:0.1];
    }
    if (dataNoVCR == nil || error) {
        STFail(@"Problem fetching testURL (%@): %@", testURL, error);
    }
    
    // Do request with cold VCR:
    [NSURLConnectionVCR startVCRWithPath:tapesPath error:&error];
    if ([NSURLConnectionVCR hasCacheForRequest:request]) {
        [NSURLConnectionVCR deleteCacheForRequest:request error:nil];
    }
    __block NSURLResponse* responseColdVCR = nil;
    __block NSData* dataColdVCR = nil;
    done = NO;
    [NSURLConnection sendAsynchronousRequest:request queue:bgQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *theError) {
        responseColdVCR = response;
        dataColdVCR = data;
        error = theError;
        done = YES;
    }];
    while (done == NO) {
        [NSThread sleepForTimeInterval:0.1];
    }
    if (dataColdVCR == nil || error) {
        STFail(@"Problem fetching testURL with cold VCR (%@): %@", testURL, error);
    }
    STAssertTrue([dataColdVCR isEqual:dataNoVCR], @"Response body without VCR is expected to be equal to response body with cold VCR.");
    STAssertTrue([responseColdVCR isEqualIgnoringVolatileHTTPHeaders:responseNoVCR], @"Response without VCR is expected to be equal to response with cold VCR.");
    
    // Do request with hot VCR:
    [BreakableHTTPConnection setBroken:YES];
    STAssertTrue([NSURLConnectionVCR hasCacheForRequest:request], @"VCR is expected to have a cache for the request by now.");
    __block NSURLResponse* responseHotVCR = nil;
    __block NSData* dataHotVCR = nil;
    done = NO;
    [NSURLConnection sendAsynchronousRequest:request queue:bgQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *theError) {
        responseHotVCR = response;
        dataHotVCR = data;
        error = theError;
        done = YES;
    }];
    while (done == NO) {
        [NSThread sleepForTimeInterval:0.1];
    }
    if (dataHotVCR == nil || error) {
        STFail(@"Problem fetching testURL with hot VCR (%@): %@", testURL, error);
    }
    STAssertTrue([dataHotVCR isEqual:dataNoVCR], @"Response body without VCR is expected to be equal to response body with cold VCR.");
    STAssertTrue([responseHotVCR isEqualIgnoringVolatileHTTPHeaders:responseNoVCR], @"Response without VCR is expected to be equal to response with cold VCR.");
}

- (void)testAsynchronousRequestWithDelegates {    
    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:YES];
    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:YES];
    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:YES];
    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)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.];
//}

@end

@implementation NSURLResponse (Testing)

- (BOOL)isEqualIgnoringVolatileHTTPHeaders:(NSURLResponse*)otherResponse {
    if ([self isKindOfClass:[NSHTTPURLResponse class]] && [otherResponse isKindOfClass:[NSHTTPURLResponse class]]) {
        NSMutableDictionary* headersSelf = [NSMutableDictionary dictionaryWithDictionary:[(NSHTTPURLResponse*)self allHeaderFields]];
        NSMutableDictionary* headersOther = [NSMutableDictionary dictionaryWithDictionary:[(NSHTTPURLResponse*)otherResponse allHeaderFields]];
        NSArray* ignoreKeys = [NSArray arrayWithObjects:@"Date", @"Expires", @"X-Runtime", nil];
        for (NSString* key in ignoreKeys) {
            [headersSelf removeObjectForKey:key];
            [headersOther removeObjectForKey:key];
        }
        // Hmm.. it seems -[NSURLResponse isEqual:] is just doing a pointer comparison...? Let's roll our own then:
        return [self.URL isEqual:otherResponse.URL] && [headersOther isEqualToDictionary:headersSelf] && [(NSHTTPURLResponse*)self statusCode] == [(NSHTTPURLResponse*)otherResponse statusCode];
    } else {
        return [self isEqual:otherResponse];
    }
}

@end


@interface HTTPConnection (Private)
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData*)data withTag:(long)tag; // to avoid compiler warnings/errors
@end


@implementation BreakableHTTPConnection

static BOOL isBroken = NO;
+ (void)setBroken:(BOOL)broken {
    @synchronized(self) {
        isBroken = broken;
    }
}

- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData*)data withTag:(long)tag {
    if (isBroken) {
        [self handleInvalidRequest:nil];
    } else {
        [super socket:sock didReadData:data withTag:tag];
    }
}

@end


@implementation ConnectionDelegate {
    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 {
    error = theError;
}

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response {
    return request;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)theResponse {
    isStarted = YES;
    response = theResponse;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    if (responseData == nil) {
        long long expectedContentLength = [response expectedContentLength];
        responseData = [NSMutableData dataWithLength:expectedContentLength];
    }
    [responseData appendData:data];  
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
    return nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    isDone = YES;
}

@end
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.