GHMockNSURLConnection Class Reference
Inherits from | NSObject |
Declared in | GHMockNSURLConnection.h GHMockNSURLConnection.m |
Overview
NSURLConnection for mocking.
Use with GHAsyncTestCase to mock out connections.
@interface GHNSURLConnectionMockTest : GHAsyncTestCase {}
@end
@implementation GHNSURLConnectionMockTest
- (void)testMock {
[self prepare];
GHMockNSURLConnection *connection = [[GHMockNSURLConnection alloc] initWithRequest:nil delegate:self];
[connection receiveHTTPResponseWithStatusCode:204 headers:testHeaders_ afterDelay:0.1];
[connection receiveData:testData_ afterDelay:0.2];
[connection finishAfterDelay:0.3];
[self waitForStatus:kGHUnitWaitStatusSuccess timeout:1.0];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
GHAssertEquals([(NSHTTPURLResponse *)response statusCode], 204, nil);
GHAssertEqualObjects([(NSHTTPURLResponse *)response allHeaderFields], testHeaders_, nil);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
GHAssertEqualObjects(data, testData_, nil);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[self notify:kGHUnitWaitStatusSuccess forSelector:@selector(testMock)];
}
@end
Tasks
-
– receiveResponse:afterDelay:
Send generic response to delegate after delay. (For asynchronous requests)
-
– receiveHTTPResponseWithStatusCode:headers:afterDelay:
Send HTTP response to delegate with status code, headers, after delay. This is only the HTTP response (and not data or finished). (For asynchronous requests)
-
– receiveData:afterDelay:
Send data to connection delegate after delay.
-
– receiveData:statusCode:MIMEType:afterDelay:
Send data to connection delegate.
-
– receiveDataFromPath:afterDelay:
Send data (from file in bundle resource) to connection delegate after delay. (For asynchronous requests)
-
– finishAfterDelay:
Calls connectionDidFinish: delegate after delay. (For asynchronous requests)
-
– receiveFromPath:statusCode:MIMEType:afterDelay:
Sends mock response, sends data, and then calls finish. (For asynchronous requests)
-
– failWithError:afterDelay:
Calls connection:didFailWithError: on delegate after specified delay.
Instance Methods
failWithError:afterDelay:
Calls connection:didFailWithError: on delegate after specified delay.
- (void)failWithError:(NSError *)error afterDelay:(NSTimeInterval)afterDelay
Parameters
- error
The error to pass to the delegate.
- afterDelay
Delay before responding (if < 0, there is no delay)
Declared In
GHMockNSURLConnection.h
finishAfterDelay:
Calls connectionDidFinish: delegate after delay. (For asynchronous requests)
- (void)finishAfterDelay:(NSTimeInterval)delay
Parameters
- delay
Delay in seconds (if < 0, there is no delay)
Declared In
GHMockNSURLConnection.h
receiveData:afterDelay:
Send data to connection delegate after delay.
- (void)receiveData:(NSData *)data afterDelay:(NSTimeInterval)afterDelay
Parameters
- data
Data to send
- afterDelay
Delay in seconds
Declared In
GHMockNSURLConnection.h
receiveData:statusCode:MIMEType:afterDelay:
Send data to connection delegate.
- (void)receiveData:(NSData *)data statusCode:(NSInteger)statusCode MIMEType:(NSString *)MIMEType afterDelay:(NSTimeInterval)afterDelay
Parameters
- data
Data to send
- statusCode
HTTP status code
- MIMEType
Mime type
- afterDelay
Delay
Declared In
GHMockNSURLConnection.h
receiveDataFromPath:afterDelay:
Send data (from file in bundle resource) to connection delegate after delay. (For asynchronous requests)
- (void)receiveDataFromPath:(NSString *)path afterDelay:(NSTimeInterval)afterDelay
Parameters
- path
Path to file
- afterDelay
Delay in seconds
Declared In
GHMockNSURLConnection.h
receiveFromPath:statusCode:MIMEType:afterDelay:
Sends mock response, sends data, and then calls finish. (For asynchronous requests)
- (void)receiveFromPath:(NSString *)path statusCode:(NSInteger)statusCode MIMEType:(NSString *)MIMEType afterDelay:(NSTimeInterval)afterDelay
Parameters
- path
Path to load data from. File should be available in Test target (bundle)
- statusCode
Status code for response
- MIMEType
Content type for response header
- afterDelay
Delay before responding (if < 0, there is no delay)
Declared In
GHMockNSURLConnection.h
receiveHTTPResponseWithStatusCode:headers:afterDelay:
Send HTTP response to delegate with status code, headers, after delay. This is only the HTTP response (and not data or finished). (For asynchronous requests)
- (void)receiveHTTPResponseWithStatusCode:(int)statusCode headers:(NSDictionary *)headers afterDelay:(NSTimeInterval)afterDelay
Parameters
- statusCode
HTTP status code
- headers
Headers
- afterDelay
Delay in seconds (if < 0, there is no delay)
Declared In
GHMockNSURLConnection.h
receiveResponse:afterDelay:
Send generic response to delegate after delay. (For asynchronous requests)
- (void)receiveResponse:(NSURLResponse *)response afterDelay:(NSTimeInterval)afterDelay
Parameters
- response
Response
- afterDelay
Delay in seconds (if < 0, there is no delay)
Declared In
GHMockNSURLConnection.h