Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 8dad29758b
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 109 lines (83 sloc) 3.422 kb
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
//
// GHNSURLConnectionMockTest.m
// GHUnit
//
// Created by Gabriel Handford on 4/9/09.
// Copyright 2009. All rights reserved.
//

#import "GHAsyncTestCase.h"
#import "GHMockNSURLConnection.h"

@interface GHMockNSURLConnectionTest : GHAsyncTestCase {
  NSDictionary *testHeaders_;
  NSData *testData_;
}
@end

@implementation GHMockNSURLConnectionTest

- (void)setUpClass {
  testHeaders_= [[NSDictionary dictionaryWithObjectsAndKeys:@"somehexdata", @"ETag", nil] retain];
  testData_ = [[@"This is test data" dataUsingEncoding:NSUTF8StringEncoding] retain];
}

- (void)tearDownClass {
  [testHeaders_ release];
  testHeaders_ = nil;
  [testData_ release];
  testData_ = nil;
}

- (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], (NSInteger)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

@interface GHMockNSURLConnectionErrorTest : GHAsyncTestCase {
  NSError *error_;
}
@end

@implementation GHMockNSURLConnectionErrorTest

- (void)testError {
  [self prepare];
  GHMockNSURLConnection *connection = [[GHMockNSURLConnection alloc] initWithRequest:nil delegate:self];
  error_ = [[NSError alloc] initWithDomain:NSURLErrorDomain code:NSURLErrorNotConnectedToInternet userInfo:nil];
  [connection failWithError:error_ afterDelay:0.2];
  [self waitForStatus:kGHUnitWaitStatusFailure timeout:1.0];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  GHAssertEqualObjects(error, error_, nil);
  [self notify:kGHUnitWaitStatusFailure forSelector:@selector(testError)];
}

@end


@interface GHMockNSURLConnectionPathTest : GHAsyncTestCase { }
@end

@implementation GHMockNSURLConnectionPathTest

- (void)testMock {
  [self prepare];
  GHMockNSURLConnection *connection = [[GHMockNSURLConnection alloc] initWithRequest:nil delegate:self];
  [connection receiveFromPath:@"example.json" statusCode:200 MIMEType:@"text/json" afterDelay:0.1];
  [self waitForStatus:kGHUnitWaitStatusSuccess timeout:1.0];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  GHAssertEquals([(NSHTTPURLResponse *)response statusCode], (NSInteger)200, nil);

  NSDictionary *headers = [(NSHTTPURLResponse *)response allHeaderFields];
  GHTestLog(@"headers=%@", headers);
  //GHAssertEqualStrings(@"text/json", [headers objectForKey:@"Content-Type"], nil);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  // TODO(gabe): Assert data
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  [self notify:kGHUnitWaitStatusSuccess forSelector:@selector(testMock)];
}

@end

Something went wrong with that request. Please try again.