root / asi-http-request-with-pithos / Classes / Tests / ASIFormDataRequestTests.m @ be116d22
History | View | Annotate | Download (13.7 kB)
1 |
// |
---|---|
2 |
// ASIFormDataRequestTests.m |
3 |
// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest |
4 |
// |
5 |
// Created by Ben Copsey on 08/11/2008. |
6 |
// Copyright 2008 All-Seeing Interactive. All rights reserved. |
7 |
// |
8 |
|
9 |
#import "ASIFormDataRequestTests.h" |
10 |
#import "ASIFormDataRequest.h" |
11 |
|
12 |
// Used for subclass test |
13 |
@interface ASIFormDataRequestSubclass : ASIFormDataRequest {} |
14 |
@end |
15 |
@implementation ASIFormDataRequestSubclass; |
16 |
@end |
17 |
|
18 |
@implementation ASIFormDataRequestTests |
19 |
|
20 |
- (void)testAddNilKeysAndValues |
21 |
{ |
22 |
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/empty-post-value"]]; |
23 |
[request setPostValue:nil forKey:@"key1"]; |
24 |
[request setPostValue:@"value2" forKey:@"key2"]; |
25 |
[request setData:nil forKey:@"file1"]; |
26 |
[request setData:[@"hello" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"file2"]; |
27 |
[request startSynchronous]; |
28 |
BOOL success = ([[request responseString] isEqualToString:@"key1: \r\nkey2: value2\r\nfile1: \r\nfile2: hello"]); |
29 |
GHAssertTrue(success, @"Sent wrong data"); |
30 |
|
31 |
// Test nil key (no key or value should be sent to the server) |
32 |
request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com"]]; |
33 |
[request addPostValue:@"value1" forKey:nil]; |
34 |
[request addPostValue:@"value2" forKey:@"key2"]; |
35 |
[request buildPostBody]; |
36 |
NSString *postBody = [[[NSString alloc] initWithData:[request postBody] encoding:NSUTF8StringEncoding] autorelease]; |
37 |
success = ([postBody isEqualToString:@"key2=value2"]); |
38 |
GHAssertTrue(success, @"Sent wrong data"); |
39 |
} |
40 |
|
41 |
- (void)testPostWithFileUpload |
42 |
{ |
43 |
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/post"]; |
44 |
|
45 |
//Create a 32kb file |
46 |
unsigned int size = 1024*32; |
47 |
NSMutableData *data = [NSMutableData dataWithLength:size]; |
48 |
NSString *path = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"bigfile"]; |
49 |
[data writeToFile:path atomically:NO]; |
50 |
|
51 |
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; |
52 |
|
53 |
NSDate *d = [NSDate date]; |
54 |
#if TARGET_OS_IPHONE |
55 |
NSValue *v = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)]; |
56 |
#else |
57 |
NSValue *v = [NSValue valueWithRect:NSMakeRect(0, 0, 200, 200)]; |
58 |
#endif |
59 |
[request setPostValue:@"foo" forKey:@"post_var"]; |
60 |
[request setPostValue:d forKey:@"post_var2"]; |
61 |
[request setPostValue:v forKey:@"post_var3"]; |
62 |
[request setFile:path forKey:@"file"]; |
63 |
[request startSynchronous]; |
64 |
|
65 |
BOOL success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"post_var: %@\r\npost_var2: %@\r\npost_var3: %@\r\nfile_name: %@\r\nfile_size: %hu\r\ncontent_type: %@",@"foo",d,v,@"bigfile",size,@"application/octet-stream"]]); |
66 |
GHAssertTrue(success,@"Failed to upload the correct data (using local file)"); |
67 |
|
68 |
//Try the same with the raw data |
69 |
request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease]; |
70 |
[request setPostValue:@"foo" forKey:@"post_var"]; |
71 |
[request setPostValue:d forKey:@"post_var2"]; |
72 |
[request setPostValue:v forKey:@"post_var3"]; |
73 |
[request setData:data forKey:@"file"]; |
74 |
[request startSynchronous]; |
75 |
|
76 |
success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"post_var: %@\r\npost_var2: %@\r\npost_var3: %@\r\nfile_name: %@\r\nfile_size: %hu\r\ncontent_type: %@",@"foo",d,v,@"file",size,@"application/octet-stream"]]); |
77 |
GHAssertTrue(success,@"Failed to upload the correct data (using NSData)"); |
78 |
|
79 |
//Post with custom content-type and file name |
80 |
request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease]; |
81 |
[request setPostValue:@"foo" forKey:@"post_var"]; |
82 |
[request setPostValue:d forKey:@"post_var2"]; |
83 |
[request setPostValue:v forKey:@"post_var3"]; |
84 |
[request setFile:path withFileName:@"myfile" andContentType:@"text/plain" forKey:@"file"]; |
85 |
[request startSynchronous]; |
86 |
|
87 |
success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"post_var: %@\r\npost_var2: %@\r\npost_var3: %@\r\nfile_name: %@\r\nfile_size: %hu\r\ncontent_type: %@",@"foo",d,v,@"myfile",size,@"text/plain"]]); |
88 |
GHAssertTrue(success,@"Failed to send the correct content-type / file name"); |
89 |
|
90 |
//Post raw data with custom content-type and file name |
91 |
request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease]; |
92 |
[request setPostValue:@"foo" forKey:@"post_var"]; |
93 |
[request setPostValue:d forKey:@"post_var2"]; |
94 |
[request setPostValue:v forKey:@"post_var3"]; |
95 |
[request setData:data withFileName:@"myfile" andContentType:@"text/plain" forKey:@"file"]; |
96 |
[request startSynchronous]; |
97 |
|
98 |
success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"post_var: %@\r\npost_var2: %@\r\npost_var3: %@\r\nfile_name: %@\r\nfile_size: %hu\r\ncontent_type: %@",@"foo",d,v,@"myfile",size,@"text/plain"]]); |
99 |
GHAssertTrue(success,@"Failed to send the correct content-type / file name"); |
100 |
|
101 |
} |
102 |
|
103 |
// Test fix for bug where setting an empty string for a form post value would cause the rest of the post body to be ignored (because an NSOutputStream won't like it if you try to write 0 bytes) |
104 |
- (void)testEmptyData |
105 |
{ |
106 |
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/post-empty"]]; |
107 |
[request setPostValue:@"hello" forKey:@"a_non_empty_string"]; |
108 |
[request setPostValue:@"" forKey:@"zzz_empty_string"]; |
109 |
[request setPostValue:@"there" forKey:@"xxx_non_empty_string"]; |
110 |
[request setShouldStreamPostDataFromDisk:YES]; |
111 |
[request buildPostBody]; |
112 |
[request startSynchronous]; |
113 |
|
114 |
BOOL success = ([[request responseString] isEqualToString:@"a_non_empty_string: hello\r\nzzz_empty_string: \r\nxxx_non_empty_string: there"]); |
115 |
GHAssertTrue(success,@"Failed to send the correct post data"); |
116 |
|
117 |
} |
118 |
|
119 |
// Ensure class convenience constructor returns an instance of our subclass |
120 |
- (void)testSubclass |
121 |
{ |
122 |
ASIFormDataRequestSubclass *instance = [ASIFormDataRequestSubclass requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com"]]; |
123 |
BOOL success = [instance isKindOfClass:[ASIFormDataRequestSubclass class]]; |
124 |
GHAssertTrue(success,@"Convenience constructor failed to return an instance of the correct class"); |
125 |
} |
126 |
|
127 |
- (void)testURLEncodedPost |
128 |
{ |
129 |
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/url-encoded-post"]]; |
130 |
[request setPostValue:@"value1" forKey:@"value1"]; |
131 |
[request setPostValue:@"(%20 ? =)" forKey:@"value2"]; |
132 |
[request setPostValue:@"£100.00" forKey:@"value3"]; |
133 |
[request setPostValue:@"" forKey:@"value4"]; |
134 |
[request setPostValue:@"&??aaa=//ciaoèèè" forKey:@"teskey&aa"]; |
135 |
|
136 |
[request setShouldStreamPostDataFromDisk:YES]; |
137 |
[request setPostFormat:ASIURLEncodedPostFormat]; |
138 |
[request startSynchronous]; |
139 |
|
140 |
|
141 |
BOOL success = ([[request responseString] isEqualToString:@"value1: value1\r\nvalue2: (%20 ? =)\r\nvalue3: £100.00\r\nvalue4: \r\nteskey&aa: &??aaa=//ciaoèèè"]); |
142 |
GHAssertTrue(success,@"Failed to send the correct post data"); |
143 |
} |
144 |
|
145 |
- (void)testCharset |
146 |
{ |
147 |
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/formdata-charset"]; |
148 |
NSString *testString = @"£££s don't seem to buy me many €€€s these days"; |
149 |
|
150 |
// Test the default (UTF-8) with a url-encoded request |
151 |
NSString *charset = @"utf-8"; |
152 |
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; |
153 |
[request setPostValue:testString forKey:@"value"]; |
154 |
[request startSynchronous]; |
155 |
BOOL success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"Got data in %@: %@",charset,testString]]); |
156 |
GHAssertTrue(success,@"Failed to correctly encode the data"); |
157 |
|
158 |
// Test the default (UTF-8) with a multipart/form-data request |
159 |
request = [ASIFormDataRequest requestWithURL:url]; |
160 |
[request setPostValue:testString forKey:@"value"]; |
161 |
[request setPostFormat:ASIMultipartFormDataPostFormat]; |
162 |
[request startSynchronous]; |
163 |
success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"Got data in %@: %@",charset,testString]]); |
164 |
GHAssertTrue(success,@"Failed to correctly encode the data"); |
165 |
|
166 |
// Test a different charset |
167 |
testString = @"£££s don't seem to buy me many $$$s these days"; |
168 |
charset = @"iso-8859-1"; |
169 |
request = [ASIFormDataRequest requestWithURL:url]; |
170 |
[request setPostValue:testString forKey:@"value"]; |
171 |
[request setStringEncoding:NSISOLatin1StringEncoding]; |
172 |
[request startSynchronous]; |
173 |
success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"Got data in %@: %@",charset,testString]]); |
174 |
GHAssertTrue(success,@"Failed to correctly encode the data"); |
175 |
|
176 |
// And again with multipart/form-data request |
177 |
request = [ASIFormDataRequest requestWithURL:url]; |
178 |
[request setPostValue:testString forKey:@"value"]; |
179 |
[request setPostFormat:ASIMultipartFormDataPostFormat]; |
180 |
[request setStringEncoding:NSISOLatin1StringEncoding]; |
181 |
[request startSynchronous]; |
182 |
success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"Got data in %@: %@",charset,testString]]); |
183 |
GHAssertTrue(success,@"Failed to correctly encode the data"); |
184 |
|
185 |
// Once more for luck |
186 |
charset = @"windows-1252"; |
187 |
request = [ASIFormDataRequest requestWithURL:url]; |
188 |
[request setPostValue:testString forKey:@"value"]; |
189 |
[request setStringEncoding:NSWindowsCP1252StringEncoding]; |
190 |
[request startSynchronous]; |
191 |
success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"Got data in %@: %@",charset,testString]]); |
192 |
GHAssertTrue(success,@"Failed to correctly encode the data"); |
193 |
|
194 |
request = [ASIFormDataRequest requestWithURL:url]; |
195 |
[request setPostValue:testString forKey:@"value"]; |
196 |
[request setPostFormat:ASIMultipartFormDataPostFormat]; |
197 |
[request setStringEncoding:NSWindowsCP1252StringEncoding]; |
198 |
[request startSynchronous]; |
199 |
success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"Got data in %@: %@",charset,testString]]); |
200 |
GHAssertTrue(success,@"Failed to correctly encode the data"); |
201 |
|
202 |
// Ensure charset isn't added to file post (GH issue 36) |
203 |
request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/Tests/return-raw-request"]]; |
204 |
[request setData:[@"test 123" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"file"]; |
205 |
[request setRequestMethod:@"PUT"]; |
206 |
[request startSynchronous]; |
207 |
success = ([[request responseString] rangeOfString:@"charset=utf-8"].location == NSNotFound); |
208 |
GHAssertTrue(success,@"Sent a charset header for an uploaded file"); |
209 |
|
210 |
|
211 |
} |
212 |
|
213 |
- (void)testPUT |
214 |
{ |
215 |
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/Tests/put_form_data"]]; |
216 |
[request setRequestMethod:@"PUT"]; |
217 |
[request setPostValue:@"cheep cheep" forKey:@"hello"]; |
218 |
[request startSynchronous]; |
219 |
|
220 |
NSString *expectedResponse = [[[NSString alloc] initWithBytes:[[request postBody] bytes] length:[[request postBody] length] encoding:[request stringEncoding]] autorelease]; |
221 |
BOOL success = ([[request responseString] isEqualToString:expectedResponse]); |
222 |
GHAssertTrue(success,@"Failed to send form data using PUT"); |
223 |
|
224 |
// Ensure that other methods still default to POST |
225 |
request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/Tests/put_form_data"]]; |
226 |
[request setRequestMethod:@"DELETE"]; |
227 |
[request setPostValue:@"cheep cheep" forKey:@"hello"]; |
228 |
[request startSynchronous]; |
229 |
|
230 |
success = ([[request responseString] isEqualToString:@"Got POST instead"]); |
231 |
GHAssertTrue(success,@"Failed to send form data using PUT"); |
232 |
} |
233 |
|
234 |
- (void)testCopy |
235 |
{ |
236 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
237 |
|
238 |
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com"]]; |
239 |
ASIFormDataRequest *request2 = [request copy]; |
240 |
GHAssertNotNil(request2,@"Failed to create a copy"); |
241 |
|
242 |
[pool release]; |
243 |
BOOL success = ([request2 retainCount] == 1); |
244 |
GHAssertTrue(success,@"Failed to create a retained copy"); |
245 |
success = ([request2 isKindOfClass:[ASIFormDataRequest class]]); |
246 |
GHAssertTrue(success,@"Copy is of wrong class"); |
247 |
|
248 |
[request2 release]; |
249 |
} |
250 |
|
251 |
- (void)testMultipleValuesForASingleKey |
252 |
{ |
253 |
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/multiple-values"]]; |
254 |
[request addPostValue:@"here" forKey:@"test_value[]"]; |
255 |
[request addPostValue:@"are" forKey:@"test_value[]"]; |
256 |
[request addPostValue:@"some" forKey:@"test_value[]"]; |
257 |
[request addPostValue:@"values" forKey:@"test_value[]"]; |
258 |
|
259 |
NSString *path1 = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"file1.txt"]; |
260 |
NSString *path2 = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"file2.txt"]; |
261 |
[@"hello" writeToFile:path1 atomically:NO encoding:NSUTF8StringEncoding error:nil]; |
262 |
[@"there" writeToFile:path2 atomically:NO encoding:NSUTF8StringEncoding error:nil]; |
263 |
[request addFile:path1 forKey:@"test_file[]"]; |
264 |
[request addFile:path2 forKey:@"test_file[]"]; |
265 |
|
266 |
[request startSynchronous]; |
267 |
NSString *expectedOutput = @"here\r\nare\r\nsome\r\nvalues\r\nfile1.txt\r\nfile2.txt\r\n"; |
268 |
BOOL success = [[request responseString] isEqualToString:expectedOutput]; |
269 |
GHAssertTrue(success,@"Failed to send the correct data"); |
270 |
|
271 |
// Check data replaces older data |
272 |
request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/single-values"]]; |
273 |
[request addPostValue:@"here" forKey:@"test_value[]"]; |
274 |
[request addPostValue:@"are" forKey:@"test_value[]"]; |
275 |
[request addPostValue:@"some" forKey:@"test_value[]"]; |
276 |
[request addPostValue:@"values" forKey:@"test_value[]"]; |
277 |
|
278 |
[request setPostValue:@"this is new data" forKey:@"test_value[]"]; |
279 |
|
280 |
[request addFile:path1 forKey:@"test_file[]"]; |
281 |
[request addFile:path2 forKey:@"test_file[]"]; |
282 |
|
283 |
[request setData:[@"this is new data" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"test_file[]"]; |
284 |
|
285 |
[request startSynchronous]; |
286 |
expectedOutput = @"this is new data\r\nfile\r\n"; |
287 |
success = [[request responseString] isEqualToString:expectedOutput]; |
288 |
GHAssertTrue(success,@"Failed to send the correct data"); |
289 |
} |
290 |
|
291 |
@end |