Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 107 lines (81 sloc) 3.355 kb
233b95f Gabriel Handford Renamed
gabriel authored
1 //
2 // GHNSURLConnectionMockTest.m
3 // GHUnit
4 //
5 // Created by Gabriel Handford on 4/9/09.
6 // Copyright 2009. All rights reserved.
7 //
8
9 #import "GHAsyncTestCase.h"
10 #import "GHMockNSURLConnection.h"
11
12 @interface GHMockNSURLConnectionTest : GHAsyncTestCase {
e771172 Gabriel Handford Tabs to spaces:
gabriel authored
13 NSDictionary *testHeaders_;
14 NSData *testData_;
233b95f Gabriel Handford Renamed
gabriel authored
15 }
16 @end
17
18 @implementation GHMockNSURLConnectionTest
19
20 - (void)setUpClass {
0530b1a Gabriel Handford Converting to ARC. Fixing build issues.
gabriel authored
21 testHeaders_= [NSDictionary dictionaryWithObjectsAndKeys:@"somehexdata", @"ETag", nil];
22 testData_ = [@"This is test data" dataUsingEncoding:NSUTF8StringEncoding];
233b95f Gabriel Handford Renamed
gabriel authored
23 }
24
25 - (void)tearDownClass {
f441b7e Gabriel Handford - Fix autorun env on iPhone
gabriel authored
26 testHeaders_ = nil;
27 testData_ = nil;
233b95f Gabriel Handford Renamed
gabriel authored
28 }
29
30 - (void)testMock {
e771172 Gabriel Handford Tabs to spaces:
gabriel authored
31 [self prepare];
32 GHMockNSURLConnection *connection = [[GHMockNSURLConnection alloc] initWithRequest:nil delegate:self];
33 [connection receiveHTTPResponseWithStatusCode:204 headers:testHeaders_ afterDelay:0.1];
34 [connection receiveData:testData_ afterDelay:0.2];
35 [connection finishAfterDelay:0.3];
36 [self waitForStatus:kGHUnitWaitStatusSuccess timeout:1.0];
233b95f Gabriel Handford Renamed
gabriel authored
37 }
e771172 Gabriel Handford Tabs to spaces:
gabriel authored
38
233b95f Gabriel Handford Renamed
gabriel authored
39 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
e771172 Gabriel Handford Tabs to spaces:
gabriel authored
40 GHAssertEquals([(NSHTTPURLResponse *)response statusCode], (NSInteger)204, nil);
41 GHAssertEqualObjects([(NSHTTPURLResponse *)response allHeaderFields], testHeaders_, nil);
233b95f Gabriel Handford Renamed
gabriel authored
42 }
43
44 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
e771172 Gabriel Handford Tabs to spaces:
gabriel authored
45 GHAssertEqualObjects(data, testData_, nil);
233b95f Gabriel Handford Renamed
gabriel authored
46 }
47
48 - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
e771172 Gabriel Handford Tabs to spaces:
gabriel authored
49 [self notify:kGHUnitWaitStatusSuccess forSelector:@selector(testMock)];
233b95f Gabriel Handford Renamed
gabriel authored
50 }
51
52 @end
53
64ae7b8 Add failWithError: to GHMockNSURLConnection
Alex Pretzlav authored
54 @interface GHMockNSURLConnectionErrorTest : GHAsyncTestCase {
e771172 Gabriel Handford Tabs to spaces:
gabriel authored
55 NSError *error_;
64ae7b8 Add failWithError: to GHMockNSURLConnection
Alex Pretzlav authored
56 }
57 @end
58
59 @implementation GHMockNSURLConnectionErrorTest
60
61 - (void)testError {
e771172 Gabriel Handford Tabs to spaces:
gabriel authored
62 [self prepare];
63 GHMockNSURLConnection *connection = [[GHMockNSURLConnection alloc] initWithRequest:nil delegate:self];
64 error_ = [[NSError alloc] initWithDomain:NSURLErrorDomain code:NSURLErrorNotConnectedToInternet userInfo:nil];
65 [connection failWithError:error_ afterDelay:0.2];
66 [self waitForStatus:kGHUnitWaitStatusFailure timeout:1.0];
64ae7b8 Add failWithError: to GHMockNSURLConnection
Alex Pretzlav authored
67 }
68
69 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
e771172 Gabriel Handford Tabs to spaces:
gabriel authored
70 GHAssertEqualObjects(error, error_, nil);
71 [self notify:kGHUnitWaitStatusFailure forSelector:@selector(testError)];
64ae7b8 Add failWithError: to GHMockNSURLConnection
Alex Pretzlav authored
72 }
73
74 @end
75
76
233b95f Gabriel Handford Renamed
gabriel authored
77 @interface GHMockNSURLConnectionPathTest : GHAsyncTestCase { }
78 @end
79
80 @implementation GHMockNSURLConnectionPathTest
81
82 - (void)testMock {
e771172 Gabriel Handford Tabs to spaces:
gabriel authored
83 [self prepare];
84 GHMockNSURLConnection *connection = [[GHMockNSURLConnection alloc] initWithRequest:nil delegate:self];
85 [connection receiveFromPath:@"example.json" statusCode:200 MIMEType:@"text/json" afterDelay:0.1];
86 [self waitForStatus:kGHUnitWaitStatusSuccess timeout:1.0];
233b95f Gabriel Handford Renamed
gabriel authored
87 }
88
89 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
e771172 Gabriel Handford Tabs to spaces:
gabriel authored
90 GHAssertEquals([(NSHTTPURLResponse *)response statusCode], (NSInteger)200, nil);
233b95f Gabriel Handford Renamed
gabriel authored
91
e771172 Gabriel Handford Tabs to spaces:
gabriel authored
92 NSDictionary *headers = [(NSHTTPURLResponse *)response allHeaderFields];
93 GHTestLog(@"headers=%@", headers);
94 //GHAssertEqualStrings(@"text/json", [headers objectForKey:@"Content-Type"], nil);
233b95f Gabriel Handford Renamed
gabriel authored
95 }
96
97 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
e771172 Gabriel Handford Tabs to spaces:
gabriel authored
98 // TODO(gabe): Assert data
233b95f Gabriel Handford Renamed
gabriel authored
99 }
100
101 - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
e771172 Gabriel Handford Tabs to spaces:
gabriel authored
102 [self notify:kGHUnitWaitStatusSuccess forSelector:@selector(testMock)];
233b95f Gabriel Handford Renamed
gabriel authored
103 }
104
105 @end
106
Something went wrong with that request. Please try again.