在你的服務(wù)器沒有準(zhǔn)備妥當(dāng)或者在你需要模擬數(shù)據(jù)進(jìn)行本地開發(fā)時,OHHTTPStubs是一個很好的可以加速測試和開發(fā)的工具。OHHTTPStubs可使用偽造的網(wǎng)絡(luò)數(shù)據(jù)和模擬的緩慢網(wǎng)絡(luò)來測試你的應(yīng)用程序,從而檢測你的應(yīng)用程序在不佳的網(wǎng)絡(luò)環(huán)境中的行為,并使用偽造的網(wǎng)絡(luò)數(shù)據(jù)編寫單元測試。
功能列表:
- 模擬慢網(wǎng)絡(luò),檢查你的應(yīng)用在差網(wǎng)絡(luò)情況下的行為
- 寫單元測試,模擬返回數(shù)據(jù)
基本用法
-
攔截域名為mywebservice.com的http請求,并返回wsresponse.json文件里面的數(shù)據(jù),可以設(shè)置網(wǎng)絡(luò)狀態(tài)嗎,請求頭格式,以及請求時間和響應(yīng)時間
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) { return [request.URL.host isEqualToString:@"mywebservice.com"]; } withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) { // Stub it with our "wsresponse.json" stub file (which is in same bundle as self) NSString* fixture = OHPathForFile(@"wsresponse.json", self.class); return [OHHTTPStubsResponse responseWithFileAtPath:fixture statusCode:200 headers:@{@"Content-Type":@"application/json"}]; }];OHHTTPStubsResponse可以指定為文件,圖片,data數(shù)據(jù),或者json對象。比較靈活
-
可以模擬慢網(wǎng)絡(luò)情況,給OHHTTPStubsResponse設(shè)置requestTime或者responseTime
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) { return [request.URL.host isEqualToString:@"mywebservice.com"]; } withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) { return [[OHHTTPStubsResponse responseWithJSONObject:someDict statusCode:200 headers:nil] requestTime:1.0 responseTime:3.0]; }]; -
你也可以用枚舉值來定義responseTime
OHHTTPStubsDownloadSpeedGPRS = -7 = 7 KB/s = 56 kbps OHHTTPStubsDownloadSpeedEDGE = -16 = 16 KB/s = 128 kbps OHHTTPStubsDownloadSpeed3G = -400 = 400 KB/s = 3200 kbps OHHTTPStubsDownloadSpeed3GPlus = -900 = 900 KB/s = 7200 kbps OHHTTPStubsDownloadSpeedWifi = -1500 = 1500 KB/s = 12000 kbps然后根據(jù)data數(shù)據(jù)量來間接的計算下載/加載時間
return [[OHHTTPStubsResponse responseWithData:[NSData data] statusCode:400 headers:nil] responseTime:OHHTTPStubsDownloadSpeed3G]; -
同樣,你也可以模擬網(wǎng)絡(luò)錯誤情況
NSError* notConnectedError = [NSError errorWithDomain:NSURLErrorDomain code:kCFURLErrorNotConnectedToInternet userInfo:nil]; return [OHHTTPStubsResponse responseWithError:notConnectedError];
自動加載
Thanks to method swizzling, OHHTTPStubs is automatically loaded and installed both for:
- requests made using
NSURLConnectionor[NSURLSession sharedSession]; - requests made using a
NSURLSessioncreated using a[NSURLSessionConfiguration defaultSessionConfiguration]or[NSURLSessionConfiguration ephemeralSessionConfiguration]configuration (using[NSURLSession sessionWithConfiguration:…]-like methods).
If you need to disable (and re-enable) OHHTTPStubs — globally or
per NSURLSession — you can use [OHHTTPStubs setEnabled:] / [OHHTTPStubs setEnabled:forSessionConfiguration:].
注意事項(xiàng)
- OHHTTPStubs不可用于后臺請求
(sessions created using[NSURLSessionConfiguration backgroundSessionConfiguration]) 因?yàn)楹笈_請求不允許用自定義的NSURLProtocols它是由iOS系統(tǒng)自己進(jìn)行管理的 - OHHTTPStubs不能模擬數(shù)據(jù)上傳
TheNSURLProtocolClient@protocoldoes not provide a way to signal the delegate that data has been sent (only that some has been loaded), so any data in theHTTPBodyorHTTPBodyStreamof anNSURLRequest, or data provided to-[NSURLSession uploadTaskWithRequest:fromData:];will be ignored, and more importantly, the-URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:delegate method will never be called when you stub the request usingOHHTTPStubs.
提交至蘋果商店
使用OHHTTPStubs的應(yīng)用可以提交至蘋果商店,因?yàn)樗话接械腁PI。
但是一般情況下你使用它都是在開發(fā)階段,而且當(dāng)你提交蘋果商店的時候并不需要他模擬網(wǎng)絡(luò),所以當(dāng)你的工程包含OHHTTPStubs類庫的時候你要小心,你可以在提交商店的時候移除該類庫或者使用#if DEBUG用來區(qū)分在正式環(huán)境下關(guān)閉它。