ASIHTTPRequest中的DELETE、PUT、GET、POST请求实例-备用
感谢分享
// ASIFormDataRequestTests.m
// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest
//
// Created by Ben Copsey on 08/11/2008.
// Copyright 2008 All-Seeing Interactive. All rights reserved.
//
#import "ASIFormDataRequestTests.h"
#import "ASIFormDataRequest.h"
// Used for subclass test
@interface ASIFormDataRequestSubclass : ASIFormDataRequest {}
@end
@implementation ASIFormDataRequestSubclass;
@end
@implementation ASIFormDataRequestTests
-(void)testDefaultMethod
{
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:[NSURLURLWithString:@"http://wedontcare.com"]] autorelease];
GHAssertTrue([[request requestMethod] isEqualToString:@"POST"], @"Default request method should be POST");
}
- (void)testAddNilKeysAndValues
{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURLURLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/empty-post-value"]];
[request setPostValue:nil forKey:@"key1"];
[request setPostValue:@"value2" forKey:@"key2"];
[request setData:nil forKey:@"file1"];
[request setData:[@"hello" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"file2"];
[request startSynchronous];
BOOL success = ([[request responseString] isEqualToString:@"key1: \r\nkey2: value2\r\nfile1: \r\nfile2: hello"]);
GHAssertTrue(success, @"Sent wrong data");
// Test nil key (no key or value should be sent to the server)
request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com"]];
[request addPostValue:@"value1" forKey:nil];
[request addPostValue:@"value2" forKey:@"key2"];
[request buildPostBody];
NSString *postBody = [[[NSString alloc] initWithData:[request postBody] encoding:NSUTF8StringEncoding] autorelease];
success = ([postBody isEqualToString:@"key2=value2"]);
GHAssertTrue(success, @"Sent wrong data");
}
- (void)testPostWithFileUpload
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/post"];
//Create a 32kb file
unsigned int size = 1024*32;
NSMutableData *data = [NSMutableData dataWithLength:size];
NSString *path = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"bigfile"];
[data writeToFile:path atomically:NO];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSDate *d = [NSDate date];
#if TARGET_OS_IPHONE
NSValue *v = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
#else
NSValue *v = [NSValue valueWithRect:NSMakeRect(0, 0, 200, 200)];
#endif
[request setPostValue:@"foo" forKey:@"post_var"];
[request setPostValue:d forKey:@"post_var2"];
[request setPostValue:v forKey:@"post_var3"];
[request setFile:path forKey:@"file"];
[request startSynchronous];
BOOL success = ([[request responseString] isEqualToString:[NSStringstringWithFormat:@"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"]]);
GHAssertTrue(success,@"Failed to upload the correct data (using local file)");
//Try the same with the raw data
request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:@"foo" forKey:@"post_var"];
[request setPostValue:d forKey:@"post_var2"];
[request setPostValue:v forKey:@"post_var3"];
[request setData:data forKey:@"file"];
[request startSynchronous];
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"]]);
GHAssertTrue(success,@"Failed to upload the correct data (using NSData)");
//Post with custom content-type and file name
request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:@"foo" forKey:@"post_var"];
[request setPostValue:d forKey:@"post_var2"];
[request setPostValue:v forKey:@"post_var3"];
[request setFile:path withFileName:@"myfile" andContentType:@"text/plain" forKey:@"file"];
[request startSynchronous];
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"]]);
GHAssertTrue(success,@"Failed to send the correct content-type / file name");
//Post raw data with custom content-type and file name
request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:@"foo" forKey:@"post_var"];
[request setPostValue:d forKey:@"post_var2"];
[request setPostValue:v forKey:@"post_var3"];
[request setData:data withFileName:@"myfile" andContentType:@"text/plain" forKey:@"file"];
[request startSynchronous];
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"]]);
GHAssertTrue(success,@"Failed to send the correct content-type / file name");
}
// 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)
- (void)testEmptyData
{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURLURLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/post-empty"]];
[request setPostValue:@"hello" forKey:@"a_non_empty_string"];
[request setPostValue:@"" forKey:@"zzz_empty_string"];
[request setPostValue:@"there" forKey:@"xxx_non_empty_string"];
[request setShouldStreamPostDataFromDisk:YES];
[request buildPostBody];
[request startSynchronous];
BOOL success = ([[request responseString] isEqualToString:@"a_non_empty_string: hello\r\nzzz_empty_string: \r\nxxx_non_empty_string: there"]);
GHAssertTrue(success,@"Failed to send the correct post data");
}
// Ensure class convenience constructor returns an instance of our subclass
- (void)testSubclass
{
ASIFormDataRequestSubclass *instance = [ASIFormDataRequestSubclass requestWithURL:[NSURLURLWithString:@"http://allseeing-i.com"]];
BOOL success = [instance isKindOfClass:[ASIFormDataRequestSubclass class]];
GHAssertTrue(success,@"Convenience constructor failed to return an instance of the correct class");
}
- (void)testURLEncodedPost
{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURLURLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/url-encoded-post"]];
[request setPostValue:@"value1" forKey:@"value1"];
[request setPostValue:@"(%20 ? =)" forKey:@"value2"];
[request setPostValue:@"£100.00" forKey:@"value3"];
[request setPostValue:@"" forKey:@"value4"];
[request setPostValue:@"&??aaa=//ciaoèèè" forKey:@"teskey&aa"];
[request setShouldStreamPostDataFromDisk:YES];
[request setPostFormat:ASIURLEncodedPostFormat];
[request startSynchronous];
BOOL success = ([[request responseString] isEqualToString:@"value1: value1\r\nvalue2: (%20 ? =)\r\nvalue3: £100.00\r\nvalue4: \r\nteskey&aa: &??aaa=//ciaoèèè"]);
GHAssertTrue(success,@"Failed to send the correct post data");
}
- (void)testCharset
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/formdata-charset"];
NSString *testString = @"£££s don't seem to buy me many €€€s these days";
// Test the default (UTF-8) with a url-encoded request
NSString *charset = @"utf-8";
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:testString forKey:@"value"];
[request startSynchronous];
BOOL success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"Got data in %@: %@",charset,testString]]);
GHAssertTrue(success,@"Failed to correctly encode the data");
// Test the default (UTF-8) with a multipart/form-data request
request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:testString forKey:@"value"];
[request setPostFormat:ASIMultipartFormDataPostFormat];
[request startSynchronous];
success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"Got data in %@: %@",charset,testString]]);
GHAssertTrue(success,@"Failed to correctly encode the data");
// Test a different charset
testString = @"£££s don't seem to buy me many $$$s these days";
charset = @"iso-8859-1";
request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:testString forKey:@"value"];
[request setStringEncoding:NSISOLatin1StringEncoding];
[request startSynchronous];
success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"Got data in %@: %@",charset,testString]]);
GHAssertTrue(success,@"Failed to correctly encode the data");
// And again with multipart/form-data request
request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:testString forKey:@"value"];
[request setPostFormat:ASIMultipartFormDataPostFormat];
[request setStringEncoding:NSISOLatin1StringEncoding];
[request startSynchronous];
success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"Got data in %@: %@",charset,testString]]);
GHAssertTrue(success,@"Failed to correctly encode the data");
// Once more for luck
charset = @"windows-1252";
request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:testString forKey:@"value"];
[request setStringEncoding:NSWindowsCP1252StringEncoding];
[request startSynchronous];
success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"Got data in %@: %@",charset,testString]]);
GHAssertTrue(success,@"Failed to correctly encode the data");
request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:testString forKey:@"value"];
[request setPostFormat:ASIMultipartFormDataPostFormat];
[request setStringEncoding:NSWindowsCP1252StringEncoding];
[request startSynchronous];
success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"Got data in %@: %@",charset,testString]]);
GHAssertTrue(success,@"Failed to correctly encode the data");
// Ensure charset isn't added to file post (GH issue 36)
request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/Tests/return-raw-request"]];
[request setData:[@"test 123" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"file"];
[request setRequestMethod:@"PUT"];
[request startSynchronous];
success = ([[request responseString] rangeOfString:@"charset=utf-8"].location == NSNotFound);
GHAssertTrue(success,@"Sent a charset header for an uploaded file");
}
- (void)testPUT
{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURLURLWithString:@"http://allseeing-i.com/ASIHTTPRequest/Tests/put_form_data"]];
[request setRequestMethod:@"PUT"];
[request setPostValue:@"cheep cheep" forKey:@"hello"];
[request startSynchronous];
NSString *expectedResponse = [[[NSString alloc] initWithBytes:[[request postBody] bytes] length:[[request postBody] length] encoding:[request stringEncoding]] autorelease];
BOOL success = ([[request responseString] isEqualToString:expectedResponse]);
GHAssertTrue(success,@"Failed to send form data using PUT");
// Ensure that other methods still default to POST
request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/Tests/put_form_data"]];
[request setRequestMethod:@"DELETE"];
[request setPostValue:@"cheep cheep" forKey:@"hello"];
[request startSynchronous];
success = ([[request responseString] isEqualToString:@"Got POST instead"]);
GHAssertTrue(success,@"Failed to send form data using PUT");
}
- (void)testCopy
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURLURLWithString:@"http://allseeing-i.com"]];
ASIFormDataRequest *request2 = [request copy];
GHAssertNotNil(request2,@"Failed to create a copy");
[pool release];
BOOL success = ([request2 retainCount] == 1);
GHAssertTrue(success,@"Failed to create a retained copy");
success = ([request2 isKindOfClass:[ASIFormDataRequest class]]);
GHAssertTrue(success,@"Copy is of wrong class");
[request2 release];
}
- (void)testMultipleValuesForASingleKey
{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURLURLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/multiple-values"]];
[request addPostValue:@"here" forKey:@"test_value[]"];
[request addPostValue:@"are" forKey:@"test_value[]"];
[request addPostValue:@"some" forKey:@"test_value[]"];
[request addPostValue:@"values" forKey:@"test_value[]"];
NSString *path1 = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"file1.txt"];
NSString *path2 = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"file2.txt"];
[@"hello" writeToFile:path1 atomically:NO encoding:NSUTF8StringEncoding error:nil];
[@"there" writeToFile:path2 atomically:NO encoding:NSUTF8StringEncoding error:nil];
[request addFile:path1 forKey:@"test_file[]"];
[request addFile:path2 forKey:@"test_file[]"];
[request startSynchronous];
NSString *expectedOutput = @"here\r\nare\r\nsome\r\nvalues\r\nfile1.txt\r\nfile2.txt\r\n";
BOOL success = [[request responseString] isEqualToString:expectedOutput];
GHAssertTrue(success,@"Failed to send the correct data");
// Check data replaces older data
request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/single-values"]];
[request addPostValue:@"here" forKey:@"test_value[]"];
[request addPostValue:@"are" forKey:@"test_value[]"];
[request addPostValue:@"some" forKey:@"test_value[]"];
[request addPostValue:@"values" forKey:@"test_value[]"];
[request setPostValue:@"this is new data" forKey:@"test_value[]"];
[request addFile:path1 forKey:@"test_file[]"];
[request addFile:path2 forKey:@"test_file[]"];
[request setData:[@"this is new data" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"test_file[]"];
[request startSynchronous];
expectedOutput = @"this is new data\r\nfile\r\n";
success = [[request responseString] isEqualToString:expectedOutput];
GHAssertTrue(success,@"Failed to send the correct data");
}
@end
ASIHTTPRequest中的DELETE、PUT、GET、POST请求实例-备用的更多相关文章
- 高版本(8以上)tomcat不支持rest中的delete和put方式请求怎么办
出现问题 当我们去访问delete方式和put方式: 后来才知道tomcat8以上是不支持delete方式和put方式 解决方法: 在跳转目标的jsp头文件上改为(加上了isErrorPage=&qu ...
- Springboot的 get查看,post创建,put更新,delete删除 -四种请求实例(form + controller)
总结 --get查看数据, post创建新数据行, put更新数据, delete删除数据行-- add和select功能都共用这一个页面, 需要进行区分显示 ,使用thymeleaf的三元选择,判断 ...
- ASIHTTPRequest中数据压缩问题与gzip
出现了类似问题,先mark,以后修改 最近使用asi发现,部分网络回调数据不能正常的json解析,将responseHeaders头打印出来,才发现公司服务器部分数据添加了gzip压缩传输. 最近简单 ...
- jquery ajax 请求中多出现一次OPTIONS请求及其解决办法
http://www.tangshuang.net/2271.html 在上一篇<服务端php解决jquery ajax跨域请求restful api问题及实践>中,我简单介绍了如何通过服 ...
- 在类的成员函数中调用delete this
最近面试的时候被问到一个问题是,在C++中,能否在类的成员函数中调用delete this,后来网上查了一下资料,关于这个问题说得比较好的有http://blog.sina.com.cn/s/blog ...
- ASIHTTPRequest系列(一):同步和异步请求
ASIHTTPRequest系列(一):同步和异步请求 发表于8个月前(2013-11-27 19:21) 阅读(431) | 评论(0) 6人收藏此文章, 我要收藏 赞0 ASIHTTPRequ ...
- MySQL_(Java)使用JDBC向数据库中删除(delete)数据
MySQL_(Java)使用JDBC向数据库发起查询请求 传送门 MySQL_(Java)使用JDBC向数据库中插入(insert)数据 传送门 MySQL_(Java)使用JDBC向数据库中删除(d ...
- 关于JavaScript中的delete操作
关于JavaScript中的delete操作 看到一道题,是这样的: (function(x){ delete x; return x; })(1); 1 null undefined Error 我 ...
- jQuery中ajax的4种常用请求方式
jQuery中ajax的4种常用请求方式: 1.$.ajax()返回其创建的 XMLHttpRequest 对象. $.ajax() 只有一个参数:参数 key/value 对象,包含各配置及回调函数 ...
随机推荐
- 说说自己对RESTful API的理解s
REST不是英文上的rest单词,其英文缩写为presentational State Transfer ,直译为表现状态转移,咋看起来很学术,不懂,其实不用去死抠这个词的意思.REST是一种约束和架 ...
- css3 渐变linear-gradient
background: -moz-linear-gradient(top, #FC641C, #FC761C); 参数:其共有三个参数,第一个参数表示线性渐变的方向,top 是从上到下.left 是从 ...
- MyEclipse 8.0注冊码+原版下载_Java开发软件
MyEclipse是一个十分优秀的用于开发Java, J2EE的Eclipse插件集合,MyEclipse的功能很强大,支持也十分广泛,尤其是对各种开元产品的支持十分不错.MyEclipse眼下支持J ...
- [React Testing] The Redux Store - Multiple Actions
When using Redux, we can test that our application state changes are working by testing that dispatc ...
- HashMap学习笔记
概述 HashMap是Map接口的一个哈希表的实现,内部是一个数组表示的.数组中的元素叫做一个Node,一个Node可以一个是一个简单的表示键值对的二元组,也可以是一个复杂的TreeNod ...
- Java基础知识强化68:基本类型包装类之Character概述和Character常见方法
1. Character概述: public final class Character extends Object implements Serializable,Comparable<Ch ...
- AIX-df命令
df 命令显示文件系统的总空间和可用空间信息.FileSystem 参数指定文件系统驻留的设备的名称,文件系统的安装目录或文件系统的相对路径名.File 参数指定非安装点的文件或目录.如果指定 Fil ...
- window成员和document成员
输出浏览器成员和DOM成员(以下为safari浏览器测试)(浏览器不同对象成员有差异) window成员 <script type="text/javascript"> ...
- 关于为什么RAID5往往掉一个盘后第二个盘也立刻挂掉的原因分析
很多人遇到过服务器RAID5挂掉,往往掉一个盘后,第二个盘也立刻挂掉. 大家都知道RAID5 一次允许一个盘缺失, RAID 5也是以数据的校验位来保证数据的安全,但它不是以单独硬盘来存放数据的校验位 ...
- oracle获得每周,每月,每季度,每年的第一天
当前年月日 SELECT trunc(sysdate) , trunc(sysdate,'dd') FROM dual 当年第一天 SELECT trunc(sysdate,'yyyy') FRO ...