你能使用OHHTTPStubs做什么
OHHTTPStubs的主要功能有兩點(diǎn):
- 偽造網(wǎng)絡(luò)請(qǐng)求返回的數(shù)據(jù)
- 模擬網(wǎng)絡(luò)請(qǐng)求時(shí)的慢網(wǎng)環(huán)境
我們通常會(huì)在以下情況下使用:
- 偽造數(shù)據(jù)、模擬慢網(wǎng)環(huán)境剧罩,檢測(cè)APP在網(wǎng)絡(luò)環(huán)境不好的情況下的行為。
- 單元測(cè)試時(shí)姓建,使用OHHTTPStubs提供模擬數(shù)據(jù)。
- 在開(kāi)發(fā)階段缤苫,模擬網(wǎng)絡(luò)請(qǐng)求數(shù)據(jù)速兔,不依賴服務(wù)器而進(jìn)行本地開(kāi)發(fā),通過(guò)這樣的方式來(lái)加速開(kāi)發(fā)活玲。
基本用法
- 攔截域名為
mywebservice.com
的http請(qǐng)求涣狗,并返回一個(gè)數(shù)組對(duì)象。
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [request.URL.host isEqualToString:@"mywebservice.com"];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
NSArray *array = @[@"Hello", @"world"];
return [OHHTTPStubsResponse responseWithJSONObject:array statusCode:200 headers:nil];
}];
- 向
mywebservice.com
發(fā)送GET請(qǐng)求舒憾,這個(gè)時(shí)候會(huì)收到成功的返回镀钓,并且responseObject
為數(shù)組[@"Hello", @"world"]
。
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:@"http://mywebservice.com" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable
responseObject) {
NSArray *data = responseObject;
NSLog(@"%@", data);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@", error);
}];
可以看到镀迂,OHHTTPStubs的使用非常簡(jiǎn)單方便掸宛,設(shè)置需要攔截的URL,提供攔截到請(qǐng)求之后招拙,提供需要返回的OHHTTPStubsResponse
對(duì)象唧瘾。
OHHTTPStubs主要接口
- 添加stub
+(id<OHHTTPStubsDescriptor>)stubRequestsPassingTest:(OHHTTPStubsTestBlock)testBlock
withStubResponse:(OHHTTPStubsResponseBlock)responseBlock;
這是OHHTTPStubs
的類方法,我們通過(guò)調(diào)用這個(gè)方法來(lái)對(duì)URL進(jìn)行攔截别凤。
在參數(shù)testBlock
中進(jìn)行URL的匹配饰序,如果返回YES,則該請(qǐng)求將會(huì)被攔截规哪。
在參數(shù)responseBlock
中求豫,返回?cái)r截了該請(qǐng)求之后的響應(yīng)數(shù)據(jù)OHHTTPStubsResponse
對(duì)象。
-
OHHTTPStubsResponse
對(duì)象
OHHTTPStubsResponse
對(duì)象是作為被攔截的請(qǐng)求的響應(yīng)诉稍,它包含HTTP響應(yīng)頭蝠嘉、響應(yīng)正文、狀態(tài)碼杯巨、響應(yīng)時(shí)間等信息蚤告。
OHHTTPStubsResponse
這個(gè)類提供了快捷方法來(lái)創(chuàng)建該對(duì)象。
- 使用
NSData
作為響應(yīng)數(shù)據(jù):
+(instancetype)responseWithData:(NSData*)data
statusCode:(int)statusCode
headers:(nullable NSDictionary*)httpHeaders;
- 使用文件的內(nèi)容來(lái)作為響應(yīng)數(shù)據(jù):
+(instancetype)responseWithFileAtPath:(NSString *)filePath
statusCode:(int)statusCode
headers:(nullable NSDictionary*)httpHeaders;
+(instancetype)responseWithFileURL:(NSURL *)fileURL
statusCode:(int)statusCode
headers:(nullable NSDictionary *)httpHeaders;
- 使用JSON對(duì)象來(lái)作為響應(yīng)數(shù)據(jù):
+ (instancetype)responseWithJSONObject:(id)jsonObject
statusCode:(int)statusCode
headers:(nullable NSDictionary *)httpHeaders;
- 直接返回一個(gè)網(wǎng)絡(luò)錯(cuò)誤:
+(instancetype)responseWithError:(NSError*)error;
- 網(wǎng)絡(luò)狀況的模擬
網(wǎng)絡(luò)狀況的模擬主要是通過(guò)OHHTTPStubsResponse
對(duì)象的requestTime
和responseTime
兩個(gè)屬性來(lái)控制服爷。
-
requestTime
:
請(qǐng)求在發(fā)送前必須等待的時(shí)間 -
responseTime
:
正數(shù)時(shí)杜恰,responseTime
就是從發(fā)送請(qǐng)求到接收到完整響應(yīng)數(shù)據(jù)的時(shí)間。
負(fù)數(shù)時(shí)仍源,responseTime
就是下載速度心褐,會(huì)根據(jù)需要下載的數(shù)據(jù)的大小,動(dòng)態(tài)的計(jì)算下載完成所需要的時(shí)間笼踩。
對(duì)于responseTime
逗爹,OHHTTPStubs已經(jīng)定義了一些常量值供我們使用
const double OHHTTPStubsDownloadSpeed1KBPS =- 8 / 8; // kbps -> KB/s
const double OHHTTPStubsDownloadSpeedSLOW =- 12 / 8; // kbps -> KB/s
const double OHHTTPStubsDownloadSpeedGPRS =- 56 / 8; // kbps -> KB/s
const double OHHTTPStubsDownloadSpeedEDGE =- 128 / 8; // kbps -> KB/s
const double OHHTTPStubsDownloadSpeed3G =- 3200 / 8; // kbps -> KB/s
const double OHHTTPStubsDownloadSpeed3GPlus =- 7200 / 8; // kbps -> KB/s
const double OHHTTPStubsDownloadSpeedWifi =- 12000 / 8; // kbps -> KB/s
可以通過(guò)如下代碼設(shè)置requestTime
和responseTime
:
[[OHHTTPStubsResponse responseWithData:data statusCode:200 headers:@{@"Content-Type":@"application/json"}]
requestTime:1.0f responseTime:OHHTTPStubsDownloadSpeed3G];
- 移除注冊(cè)的stub
查看stubRequestsPassingTest:withStubResponse:
的源碼
+(id<OHHTTPStubsDescriptor>)stubRequestsPassingTest:(OHHTTPStubsTestBlock)testBlock
withStubResponse:(OHHTTPStubsResponseBlock)responseBlock
{
OHHTTPStubsDescriptor* stub = [OHHTTPStubsDescriptor stubDescriptorWithTestBlock:testBlock
responseBlock:responseBlock];
[OHHTTPStubs.sharedInstance addStub:stub];
return stub;
}
+ (instancetype)sharedInstance
{
static OHHTTPStubs *sharedInstance = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
我們可以看到,每次添加一個(gè)stub之后嚎于,會(huì)創(chuàng)建一個(gè)OHHTTPStubsDescriptor
實(shí)例由單例對(duì)象OHHTTPStubs
持有掘而。如果我們不手動(dòng)將添加的stub移除挟冠,勢(shì)必會(huì)造成內(nèi)存常駐。我們可以調(diào)用以下方法移除添加的stub镣屹。
+(BOOL)removeStub:(id<OHHTTPStubsDescriptor>)stubDesc;
+(void)removeAllStubs;
- 啟用和禁用stubs
所有的stubs默認(rèn)是啟用的圃郊,即默認(rèn)會(huì)對(duì)需要攔截的請(qǐng)求進(jìn)行攔截价涝∨冢可以通過(guò)以下方法啟用或禁用攔截:
+(void)setEnabled:(BOOL)enabled;
+ (void)setEnabled:(BOOL)enabled forSessionConfiguration:(NSURLSessionConfiguration *)sessionConfig;
- 添加監(jiān)聽(tīng)事件
/**
添加一個(gè)在每個(gè)stub被觸發(fā)都執(zhí)行一次的block
@param block 每個(gè)stub被觸發(fā)都執(zhí)行一次的block
*/
+(void)onStubActivation:( nullable void(^)(NSURLRequest* request, id<OHHTTPStubsDescriptor> stub, OHHTTPStubsResponse* responseStub) )block;
/**
添加一個(gè)在OHHTTPStubs發(fā)生請(qǐng)求重定向的時(shí)候被執(zhí)行的block
@param block 在OHHTTPStubs發(fā)生請(qǐng)求重定向的時(shí)候被執(zhí)行的block
*/
+(void)onStubRedirectResponse:( nullable void(^)(NSURLRequest* request, NSURLRequest* redirectRequest, id<OHHTTPStubsDescriptor> stub, OHHTTPStubsResponse* responseStub) )block;
/**
添加一個(gè)在stub完成之后會(huì)被執(zhí)行的block
@param block 在stub完成之后會(huì)被執(zhí)行的block
*/
+(void)afterStubFinish:( nullable void(^)(NSURLRequest* request, id<OHHTTPStubsDescriptor> stub, OHHTTPStubsResponse* responseStub, NSError *error) )block;
/**
添加一個(gè)在OHHTTPStubs遇到無(wú)法攔截的請(qǐng)求時(shí)調(diào)用的block
@param block 在OHHTTPStubs遇到無(wú)法攔截的請(qǐng)求時(shí)調(diào)用的block
*/
+(void)onStubMissing:( nullable void(^)(NSURLRequest* request) )block;
- POST請(qǐng)求時(shí)的HTTPBody
當(dāng)使用NSURLSession
發(fā)送POST請(qǐng)求時(shí),請(qǐng)求的body會(huì)在到達(dá)OHHTTPStubs
時(shí)被置為nil
色瘩。也就是說(shuō)在testBlock
和responseBlock
中伪窖,直接獲取NSURLRequest
的HTTPBody
會(huì)得到nil
。
OHHTTPStubs
通過(guò)方法置換在調(diào)用NSURLRequest
的setHTTPBody:
時(shí)居兆,將HTTPBody
的內(nèi)容做了一個(gè)備份覆山。提供了方法OHHTTPStubs_HTTPBody
來(lái)獲取備份的HTTPBody
。
具體代碼如下:
NSString * const OHHTTPStubs_HTTPBodyKey = @"HTTPBody";
@implementation NSURLRequest (HTTPBodyTesting)
- (NSData*)OHHTTPStubs_HTTPBody
{
return [NSURLProtocol propertyForKey:OHHTTPStubs_HTTPBodyKey inRequest:self];
}
@end
#pragma mark - NSMutableURLRequest+HTTPBodyTesting
typedef void(*OHHHTTPStubsSetterIMP)(id, SEL, id);
static OHHHTTPStubsSetterIMP orig_setHTTPBody;
static void OHHTTPStubs_setHTTPBody(id self, SEL _cmd, NSData* HTTPBody)
{
// store the http body via NSURLProtocol
if (HTTPBody) {
[NSURLProtocol setProperty:HTTPBody forKey:OHHTTPStubs_HTTPBodyKey inRequest:self];
} else {
// unfortunately resetting does not work properly as the NSURLSession also uses this to reset the property
}
orig_setHTTPBody(self, _cmd, HTTPBody);
}
/**
* Swizzles setHTTPBody: in order to maintain a copy of the http body for later
* reference and calls the original implementation.
*
* @warning Should not be used in production, testing only.
*/
@interface NSMutableURLRequest (HTTPBodyTesting) @end
@implementation NSMutableURLRequest (HTTPBodyTesting)
+ (void)load
{
orig_setHTTPBody = (OHHHTTPStubsSetterIMP)OHHTTPStubsReplaceMethod(@selector(setHTTPBody:),
(IMP)OHHTTPStubs_setHTTPBody,
[NSMutableURLRequest class],
NO);
}
@end
注意事項(xiàng)
-
OHHTTPStubs
不能用于后臺(tái)會(huì)話(由[NSURLSessionConfiguration backgroundSessionConfiguration]
創(chuàng)建的會(huì)話)泥栖,因?yàn)楹笈_(tái)會(huì)話由iOS系統(tǒng)自己維護(hù)簇宽,并且不允許使用NSURLProtocols
。 -
OHHTTPStubs
不能模擬數(shù)據(jù)上傳吧享。NSURLProtocolClient
協(xié)議并沒(méi)有提供任何方式通知delegate數(shù)據(jù)已經(jīng)發(fā)送魏割,所以一個(gè)NSURLRequest
的HTTPBody
、HTTPBodyStream
或是由-[NSURLSession uploadTaskWithRequest:fromData:]
提供的數(shù)據(jù)都會(huì)被忽略钢颂。最重要的是使用OHHTTPStubs
來(lái)stub一個(gè)請(qǐng)求后钞它,代理方法-URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:
永遠(yuǎn)也不會(huì)被調(diào)用。 -
OHHTTPStubs
還有一個(gè)重定向的問(wèn)題殊鞭,具有零延遲的重定向會(huì)以一個(gè)空響應(yīng)結(jié)束遭垛。
提交到App Store
OHHTTPStubs
中沒(méi)有使用任何私有的API,它是可以被提交到App Store的操灿。但是我們基本上只會(huì)在開(kāi)發(fā)階段使用stubs锯仪,所以是沒(méi)有必要把stubs提交到App Store的。
我們可以通過(guò)#if DEBUG
塊的方式來(lái)避免提交相關(guān)代碼到App Store趾盐。
為了避免在發(fā)布前忘記移除對(duì)OHHTTPStubs
的導(dǎo)入卵酪,可以使用下面的方式對(duì)pod進(jìn)行配置:
pod 'OHHTTPStubs', :configurations => ['Debug', 'Development']
或以下代碼:
pod 'OHHTTPStubs', :configurations => 'Debug'
如果配置為僅在Debug
模式下導(dǎo)入OHHTTPStubs
,那么在其它模式下使用OHHTTPStubs
就會(huì)報(bào)錯(cuò)谤碳。所以一定要記得使用#if DEBUG
塊或者刪除相關(guān)的代碼溃卡。
最后附上github地址:https://github.com/AliSoftware/OHHTTPStubs