iOS模擬網(wǎng)絡(luò)之OHHTTPStubs庫的介紹與使用

在你的服務(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ù)編寫單元測試。

功能列表:

  1. 模擬慢網(wǎng)絡(luò),檢查你的應(yīng)用在差網(wǎng)絡(luò)情況下的行為
  2. 寫單元測試,模擬返回數(shù)據(jù)

基本用法

  1. 攔截域名為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 NSURLConnection or [NSURLSession sharedSession];
  • requests made using a NSURLSession created 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ù)上傳
    The NSURLProtocolClient @protocol does not provide a way to signal the delegate that data has been sent (only that some has been loaded), so any data in the HTTPBody or HTTPBodyStream of an NSURLRequest, 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 using OHHTTPStubs.

提交至蘋果商店

使用OHHTTPStubs的應(yīng)用可以提交至蘋果商店,因?yàn)樗话接械腁PI。
但是一般情況下你使用它都是在開發(fā)階段,而且當(dāng)你提交蘋果商店的時候并不需要他模擬網(wǎng)絡(luò),所以當(dāng)你的工程包含OHHTTPStubs類庫的時候你要小心,你可以在提交商店的時候移除該類庫或者使用#if DEBUG用來區(qū)分在正式環(huán)境下關(guān)閉它。

https://github.com/AliSoftware/OHHTTPStubs

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容