Commits

Richard Lee  committed a8e585d

Fix possible exception of negative expectedContentLength.

  • Participants
  • Parent commits 658b26f

Comments (0)

Files changed (2)

File NSURLConnectionVCR/NSURLConnectionVCR.m

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
     if (responseData == nil) {
         long long expectedContentLength = [response expectedContentLength];
-        responseData = [NSMutableData dataWithCapacity:expectedContentLength];
+        if (expectedContentLength > 0) {
+            responseData = [NSMutableData dataWithCapacity:expectedContentLength];
+        } else {
+            responseData = [NSMutableData data];
+        }
     }
     [responseData appendData:data];
     [realDelegate connection:connection didReceiveData:data];

File NSURLConnectionVCRTests/NSURLConnectionVCRTests.m

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
     if (responseData == nil) {
         long long expectedContentLength = [response expectedContentLength];
-        responseData = [NSMutableData dataWithLength:expectedContentLength];
+        if (expectedContentLength > 0) {
+            responseData = [NSMutableData dataWithCapacity:expectedContentLength];
+        } else {
+            responseData = [NSMutableData data];
+        }
     }
-    [responseData appendData:data];  
+    [responseData appendData:data];
 }
 
 - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
     isDone = YES;
 }
 
-@end
+@end