Added lots of comments
Added missing setDownloadProgressDelegate to ASIHTTPRequest
Showing
9 changed files
with
81 additions
and
82 deletions
@@ -10,20 +10,20 @@ | @@ -10,20 +10,20 @@ | ||
10 | 10 | ||
11 | @interface ASIFormDataRequest : ASIHTTPRequest { | 11 | @interface ASIFormDataRequest : ASIHTTPRequest { |
12 | 12 | ||
13 | - //Parameters that will be POSTed to the url | 13 | + // Parameters that will be POSTed to the url |
14 | NSMutableDictionary *postData; | 14 | NSMutableDictionary *postData; |
15 | 15 | ||
16 | - //Files that will be POSTed to the url | 16 | + // Files that will be POSTed to the url |
17 | NSMutableDictionary *fileData; | 17 | NSMutableDictionary *fileData; |
18 | 18 | ||
19 | } | 19 | } |
20 | 20 | ||
21 | #pragma mark setup request | 21 | #pragma mark setup request |
22 | 22 | ||
23 | -//Add a POST variable to the request | 23 | +// Add a POST variable to the request |
24 | - (void)setPostValue:(id)value forKey:(NSString *)key; | 24 | - (void)setPostValue:(id)value forKey:(NSString *)key; |
25 | 25 | ||
26 | -//Add the contents of a local file as a POST variable to the request | 26 | +// Add the contents of a local file as a POST variable to the request |
27 | - (void)setFile:(NSString *)filePath forKey:(NSString *)key; | 27 | - (void)setFile:(NSString *)filePath forKey:(NSString *)key; |
28 | 28 | ||
29 | @end | 29 | @end |
@@ -61,11 +61,11 @@ | @@ -61,11 +61,11 @@ | ||
61 | 61 | ||
62 | NSMutableData *body = [[[NSMutableData alloc] init] autorelease]; | 62 | NSMutableData *body = [[[NSMutableData alloc] init] autorelease]; |
63 | 63 | ||
64 | - //Set your own boundary string only if really obsessive. We don't bother to check if post data contains the boundary, since it's pretty unlikely that it does. | 64 | + // Set your own boundary string only if really obsessive. We don't bother to check if post data contains the boundary, since it's pretty unlikely that it does. |
65 | NSString *stringBoundary = @"0xKhTmLbOuNdArY"; | 65 | NSString *stringBoundary = @"0xKhTmLbOuNdArY"; |
66 | 66 | ||
67 | if ([fileData count] > 0) { | 67 | if ([fileData count] > 0) { |
68 | - //We need to use multipart/form-data when using file upload | 68 | + // We need to use multipart/form-data when using file upload |
69 | [self addRequestHeader:@"Content-Type" value:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary]]; | 69 | [self addRequestHeader:@"Content-Type" value:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary]]; |
70 | } | 70 | } |
71 | 71 | ||
@@ -73,7 +73,7 @@ | @@ -73,7 +73,7 @@ | ||
73 | 73 | ||
74 | [body appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; | 74 | [body appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; |
75 | 75 | ||
76 | - //Adds post data | 76 | + // Adds post data |
77 | NSData *endItemBoundary = [[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]; | 77 | NSData *endItemBoundary = [[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]; |
78 | NSEnumerator *e = [postData keyEnumerator]; | 78 | NSEnumerator *e = [postData keyEnumerator]; |
79 | NSString *key; | 79 | NSString *key; |
@@ -87,7 +87,7 @@ | @@ -87,7 +87,7 @@ | ||
87 | } | 87 | } |
88 | } | 88 | } |
89 | 89 | ||
90 | - //Adds files to upload | 90 | + // Adds files to upload |
91 | NSData *contentTypeHeader = [[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]; | 91 | NSData *contentTypeHeader = [[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]; |
92 | e = [fileData keyEnumerator]; | 92 | e = [fileData keyEnumerator]; |
93 | i=0; | 93 | i=0; |
@@ -97,14 +97,14 @@ | @@ -97,14 +97,14 @@ | ||
97 | [body appendData:contentTypeHeader]; | 97 | [body appendData:contentTypeHeader]; |
98 | [body appendData:[NSData dataWithContentsOfMappedFile:filePath]]; | 98 | [body appendData:[NSData dataWithContentsOfMappedFile:filePath]]; |
99 | i++; | 99 | i++; |
100 | - if (i != [fileData count]) { //Only add the boundary if this is not the last item in the post body | 100 | + // Only add the boundary if this is not the last item in the post body |
101 | + if (i != [fileData count]) { | ||
101 | [body appendData:endItemBoundary]; | 102 | [body appendData:endItemBoundary]; |
102 | } | 103 | } |
103 | } | 104 | } |
104 | 105 | ||
105 | [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; | 106 | [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; |
106 | 107 | ||
107 | - //Since we've got post data, let's set the post body to an empty NSMutableData object | ||
108 | [self setPostBody:body]; | 108 | [self setPostBody:body]; |
109 | 109 | ||
110 | //Now we've created our post data, construct the request | 110 | //Now we've created our post data, construct the request |
@@ -163,8 +163,13 @@ | @@ -163,8 +163,13 @@ | ||
163 | - (void)updateUploadProgress; | 163 | - (void)updateUploadProgress; |
164 | - (void)resetDownloadProgress:(NSNumber *)max; | 164 | - (void)resetDownloadProgress:(NSNumber *)max; |
165 | - (void)updateDownloadProgress; | 165 | - (void)updateDownloadProgress; |
166 | + | ||
167 | +// Called when authorisation is needed, as we only find out we don't have permission to something when the upload is complete | ||
166 | - (void)removeUploadProgressSoFar; | 168 | - (void)removeUploadProgressSoFar; |
167 | 169 | ||
170 | +// Helper method for interacting with progress indicators to abstract the details of different APIS (NSProgressIndicator and UIProgressView) | ||
171 | ++ (void)setProgress:(double)progress forProgressIndicator:(id)indicator; | ||
172 | + | ||
168 | #pragma mark handling request complete / failure | 173 | #pragma mark handling request complete / failure |
169 | 174 | ||
170 | // Called when a request completes successfully - defaults to: @selector(requestFinished:) | 175 | // Called when a request completes successfully - defaults to: @selector(requestFinished:) |
@@ -228,8 +233,6 @@ | @@ -228,8 +233,6 @@ | ||
228 | // Dump all session data (authentication and cookies) | 233 | // Dump all session data (authentication and cookies) |
229 | + (void)clearSession; | 234 | + (void)clearSession; |
230 | 235 | ||
231 | -//Helper method for interacting with progress indicators to abstract the details of different APIS for cocoa and cocoa touch | ||
232 | -+ (void)setProgress:(double)progress forProgressIndicator:(id)indicator; | ||
233 | 236 | ||
234 | @property (retain) NSString *username; | 237 | @property (retain) NSString *username; |
235 | @property (retain) NSString *password; | 238 | @property (retain) NSString *password; |
This diff is collapsed. Click to expand it.
@@ -20,7 +20,6 @@ | @@ -20,7 +20,6 @@ | ||
20 | - (void)testFileDownload; | 20 | - (void)testFileDownload; |
21 | - (void)testDownloadProgress; | 21 | - (void)testDownloadProgress; |
22 | - (void)testUploadProgress; | 22 | - (void)testUploadProgress; |
23 | -- (void)testOperationQueue; | ||
24 | - (void)testCookies; | 23 | - (void)testCookies; |
25 | 24 | ||
26 | @end | 25 | @end |
@@ -138,66 +138,6 @@ More tests needed for: | @@ -138,66 +138,6 @@ More tests needed for: | ||
138 | { | 138 | { |
139 | progress = newProgress; | 139 | progress = newProgress; |
140 | } | 140 | } |
141 | - | ||
142 | - | ||
143 | - | ||
144 | -- (void)testOperationQueue | ||
145 | -{ | ||
146 | - NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease]; | ||
147 | - | ||
148 | - NSURL *url; | ||
149 | - url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/first"] autorelease]; | ||
150 | - ASIHTTPRequest *request1 = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; | ||
151 | - [queue addOperation:request1]; | ||
152 | - | ||
153 | - url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/second"] autorelease]; | ||
154 | - ASIHTTPRequest *request2 = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; | ||
155 | - [queue addOperation:request2]; | ||
156 | - | ||
157 | - url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/third"] autorelease]; | ||
158 | - ASIHTTPRequest *request3 = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; | ||
159 | - [queue addOperation:request3]; | ||
160 | - | ||
161 | - url = [[[NSURL alloc] initWithString:@""] autorelease]; | ||
162 | - ASIHTTPRequest *request4 = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; | ||
163 | - [queue addOperation:request4]; | ||
164 | - | ||
165 | - url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/broken"] autorelease]; | ||
166 | - ASIHTTPRequest *request5 = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; | ||
167 | - [queue addOperation:request5]; | ||
168 | - | ||
169 | - [queue waitUntilAllOperationsAreFinished]; | ||
170 | - | ||
171 | - BOOL success; | ||
172 | - | ||
173 | - success = ([request1 error] == nil); | ||
174 | - STAssertTrue(success,@"Request 1 failed"); | ||
175 | - | ||
176 | - success = [[request1 dataString] isEqualToString:@"This is the expected content for the first string"]; | ||
177 | - STAssertTrue(success,@"Failed to download the correct data for request 1"); | ||
178 | - | ||
179 | - success = ([request2 error] == nil); | ||
180 | - STAssertTrue(success,@"Request 2 failed"); | ||
181 | - | ||
182 | - success = [[request2 dataString] isEqualToString:@"This is the expected content for the second string"]; | ||
183 | - STAssertTrue(success,@"Failed to download the correct data for request 2"); | ||
184 | - | ||
185 | - success = ([request3 error] == nil); | ||
186 | - STAssertTrue(success,@"Request 3 failed"); | ||
187 | - | ||
188 | - success = [[request3 dataString] isEqualToString:@"This is the expected content for the third string"]; | ||
189 | - STAssertTrue(success,@"Failed to download the correct data for request 3"); | ||
190 | - | ||
191 | - success = ([request4 error] != nil); | ||
192 | - STAssertTrue(success,@"Request 4 succeed when it should have failed"); | ||
193 | - | ||
194 | - success = ([request5 error] == nil); | ||
195 | - STAssertTrue(success,@"Request 5 failed"); | ||
196 | - | ||
197 | - success = ([request5 responseStatusCode] == 404); | ||
198 | - STAssertTrue(success,@"Failed to obtain the correct status code for request 5"); | ||
199 | - | ||
200 | -} | ||
201 | 141 | ||
202 | 142 | ||
203 | 143 |
@@ -9,27 +9,44 @@ | @@ -9,27 +9,44 @@ | ||
9 | 9 | ||
10 | 10 | ||
11 | @interface ASINetworkQueue : NSOperationQueue { | 11 | @interface ASINetworkQueue : NSOperationQueue { |
12 | + | ||
13 | + // Delegate will get didFail + didFinish messages (if set), as well as authorizationNeededForRequest messages | ||
12 | id delegate; | 14 | id delegate; |
15 | + | ||
16 | + // Will be called when a request completes with the request as the argument | ||
17 | + SEL requestDidFinishSelector; | ||
18 | + | ||
19 | + // Will be called when a request fails with the request as the argument | ||
20 | + SEL requestDidFailSelector; | ||
21 | + | ||
22 | + // Will be called when the queue finishes with the queue as the argument | ||
23 | + SEL queueDidFinishSelector; | ||
13 | 24 | ||
25 | + // Upload progress indicator, probably an NSProgressIndicator or UIProgressView | ||
14 | id uploadProgressDelegate; | 26 | id uploadProgressDelegate; |
27 | + | ||
28 | + // Total amount uploaded so far for all requests in this queue | ||
15 | int uploadProgressBytes; | 29 | int uploadProgressBytes; |
30 | + | ||
31 | + // Total amount to be uploaded for all requests in this queue - requests add to this figure as they work out how much data they have to transmit | ||
16 | int uploadProgressTotalBytes; | 32 | int uploadProgressTotalBytes; |
17 | 33 | ||
34 | + // Download progress indicator, probably an NSProgressIndicator or UIProgressView | ||
18 | id downloadProgressDelegate; | 35 | id downloadProgressDelegate; |
36 | + | ||
37 | + // Total amount downloaded so far for all requests in this queue | ||
19 | int downloadProgressBytes; | 38 | int downloadProgressBytes; |
20 | - int downloadProgressTotalBytes; | ||
21 | 39 | ||
22 | - SEL requestDidFinishSelector; | 40 | + // Total amount to be downloaded for all requests in this queue - requests add to this figure as they receive Content-Length headers |
23 | - SEL requestDidFailSelector; | 41 | + int downloadProgressTotalBytes; |
24 | - SEL queueDidFinishSelector; | ||
25 | 42 | ||
43 | + // When YES, the queue will cancel all requests when a request fails. Default is YES | ||
26 | BOOL shouldCancelAllRequestsOnFailure; | 44 | BOOL shouldCancelAllRequestsOnFailure; |
27 | 45 | ||
28 | int requestsCount; | 46 | int requestsCount; |
29 | int requestsCompleteCount; | 47 | int requestsCompleteCount; |
30 | } | 48 | } |
31 | 49 | ||
32 | -// | ||
33 | 50 | ||
34 | // Called at the start of a request to add on the size of this upload to the total | 51 | // Called at the start of a request to add on the size of this upload to the total |
35 | - (void)incrementUploadSizeBy:(int)bytes; | 52 | - (void)incrementUploadSizeBy:(int)bytes; |
@@ -48,6 +48,8 @@ | @@ -48,6 +48,8 @@ | ||
48 | - (void)setUploadProgressDelegate:(id)newDelegate | 48 | - (void)setUploadProgressDelegate:(id)newDelegate |
49 | { | 49 | { |
50 | uploadProgressDelegate = newDelegate; | 50 | uploadProgressDelegate = newDelegate; |
51 | + | ||
52 | + // If the uploadProgressDelegate is an NSProgressIndicator, we set it's MaxValue to 1.0 so we can treat it similarly to UIProgressViews | ||
51 | SEL selector = @selector(setMaxValue:); | 53 | SEL selector = @selector(setMaxValue:); |
52 | if ([uploadProgressDelegate respondsToSelector:selector]) { | 54 | if ([uploadProgressDelegate respondsToSelector:selector]) { |
53 | double max = 1.0; | 55 | double max = 1.0; |
@@ -64,6 +66,8 @@ | @@ -64,6 +66,8 @@ | ||
64 | - (void)setDownloadProgressDelegate:(id)newDelegate | 66 | - (void)setDownloadProgressDelegate:(id)newDelegate |
65 | { | 67 | { |
66 | downloadProgressDelegate = newDelegate; | 68 | downloadProgressDelegate = newDelegate; |
69 | + | ||
70 | + // If the downloadProgressDelegate is an NSProgressIndicator, we set it's MaxValue to 1.0 so we can treat it similarly to UIProgressViews | ||
67 | SEL selector = @selector(setMaxValue:); | 71 | SEL selector = @selector(setMaxValue:); |
68 | if ([downloadProgressDelegate respondsToSelector:selector]) { | 72 | if ([downloadProgressDelegate respondsToSelector:selector]) { |
69 | double max = 1.0; | 73 | double max = 1.0; |
@@ -75,7 +79,7 @@ | @@ -75,7 +79,7 @@ | ||
75 | } | 79 | } |
76 | } | 80 | } |
77 | 81 | ||
78 | -//Only add ASIHTTPRequests to this queue | 82 | +// Only add ASIHTTPRequests to this queue!! |
79 | - (void)addOperation:(NSOperation *)operation | 83 | - (void)addOperation:(NSOperation *)operation |
80 | { | 84 | { |
81 | if ([operation isKindOfClass:[ASIHTTPRequest class]]) { | 85 | if ([operation isKindOfClass:[ASIHTTPRequest class]]) { |
@@ -160,6 +164,7 @@ | @@ -160,6 +164,7 @@ | ||
160 | [ASIHTTPRequest setProgress:progress forProgressIndicator:downloadProgressDelegate]; | 164 | [ASIHTTPRequest setProgress:progress forProgressIndicator:downloadProgressDelegate]; |
161 | } | 165 | } |
162 | 166 | ||
167 | +// Since this queue takes over as the delegate for all requests it contains, it should forward authorisation requests to its own delegate | ||
163 | - (void)authorizationNeededForRequest:(ASIHTTPRequest *)request | 168 | - (void)authorizationNeededForRequest:(ASIHTTPRequest *)request |
164 | { | 169 | { |
165 | if ([delegate respondsToSelector:@selector(authorizationNeededForRequest:)]) { | 170 | if ([delegate respondsToSelector:@selector(authorizationNeededForRequest:)]) { |
@@ -82,12 +82,48 @@ | @@ -82,12 +82,48 @@ | ||
82 | url = [[[NSURL alloc] initWithString:@""] autorelease]; | 82 | url = [[[NSURL alloc] initWithString:@""] autorelease]; |
83 | requestThatShouldFail = [[ASIHTTPRequest alloc] initWithURL:url]; | 83 | requestThatShouldFail = [[ASIHTTPRequest alloc] initWithURL:url]; |
84 | [networkQueue addOperation:requestThatShouldFail]; | 84 | [networkQueue addOperation:requestThatShouldFail]; |
85 | + | ||
86 | + url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/broken"] autorelease]; | ||
87 | + ASIHTTPRequest *request5 = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; | ||
88 | + [networkQueue addOperation:request5]; | ||
89 | + | ||
85 | 90 | ||
86 | NSDate* endDate = [NSDate distantFuture]; | 91 | NSDate* endDate = [NSDate distantFuture]; |
87 | while (!complete) { | 92 | while (!complete) { |
88 | [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:endDate]; | 93 | [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:endDate]; |
89 | } | 94 | } |
90 | 95 | ||
96 | + | ||
97 | + BOOL success; | ||
98 | + success = ([request1 error] == nil); | ||
99 | + STAssertTrue(success,@"Request 1 failed"); | ||
100 | + | ||
101 | + success = [[request1 dataString] isEqualToString:@"This is the expected content for the first string"]; | ||
102 | + STAssertTrue(success,@"Failed to download the correct data for request 1"); | ||
103 | + | ||
104 | + success = ([request2 error] == nil); | ||
105 | + STAssertTrue(success,@"Request 2 failed"); | ||
106 | + | ||
107 | + success = [[request2 dataString] isEqualToString:@"This is the expected content for the second string"]; | ||
108 | + STAssertTrue(success,@"Failed to download the correct data for request 2"); | ||
109 | + | ||
110 | + success = ([request3 error] == nil); | ||
111 | + STAssertTrue(success,@"Request 3 failed"); | ||
112 | + | ||
113 | + success = [[request3 dataString] isEqualToString:@"This is the expected content for the third string"]; | ||
114 | + STAssertTrue(success,@"Failed to download the correct data for request 3"); | ||
115 | + | ||
116 | + success = ([requestThatShouldFail error] != nil); | ||
117 | + STAssertTrue(success,@"Request 4 succeed when it should have failed"); | ||
118 | + | ||
119 | + success = ([request5 error] == nil); | ||
120 | + STAssertTrue(success,@"Request 5 failed"); | ||
121 | + | ||
122 | + success = ([request5 responseStatusCode] == 404); | ||
123 | + STAssertTrue(success,@"Failed to obtain the correct status code for request 5"); | ||
124 | + | ||
125 | + | ||
126 | + | ||
91 | [requestThatShouldFail release]; | 127 | [requestThatShouldFail release]; |
92 | 128 | ||
93 | } | 129 | } |
@@ -118,14 +154,14 @@ | @@ -118,14 +154,14 @@ | ||
118 | url = [[[NSURL alloc] initWithString:@""] autorelease]; | 154 | url = [[[NSURL alloc] initWithString:@""] autorelease]; |
119 | requestThatShouldFail = [[ASIHTTPRequest alloc] initWithURL:url]; | 155 | requestThatShouldFail = [[ASIHTTPRequest alloc] initWithURL:url]; |
120 | [networkQueue addOperation:requestThatShouldFail]; | 156 | [networkQueue addOperation:requestThatShouldFail]; |
121 | - | 157 | + |
122 | NSDate* endDate = [NSDate distantFuture]; | 158 | NSDate* endDate = [NSDate distantFuture]; |
123 | while (!complete) { | 159 | while (!complete) { |
124 | [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:endDate]; | 160 | [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:endDate]; |
125 | } | 161 | } |
126 | 162 | ||
127 | - [requestThatShouldFail release]; | ||
128 | 163 | ||
164 | + [requestThatShouldFail release]; | ||
129 | } | 165 | } |
130 | 166 | ||
131 | - (void)requestFailedCancellingOthers:(ASIHTTPRequest *)request | 167 | - (void)requestFailedCancellingOthers:(ASIHTTPRequest *)request |
@@ -144,7 +180,6 @@ | @@ -144,7 +180,6 @@ | ||
144 | - (void)queueFinished:(ASINetworkQueue *)queue | 180 | - (void)queueFinished:(ASINetworkQueue *)queue |
145 | { | 181 | { |
146 | complete = YES; | 182 | complete = YES; |
147 | - | ||
148 | } | 183 | } |
149 | 184 | ||
150 | 185 |
-
Please register or login to post a comment