NewLLDebugTool旨在通过向代码里集成一个 SDK 就可以快速便捷的进行 ios 自动化测试:NewLLDebugTool——一款可以快速进行 ios 自动化的工具

当前已经集成OHHTTPSTUBS的 http/https 的 mock 能力,在保留其原有功能的基础上进行修改,可以支持 http/https 在线 mock(返回数据是 json 格式),mock 规则是置空和置 NULL.可以结合 monkey 进行 fuzzy 测试。如果没有打开随机 mock 功能,返回的请求包如下图所示:

如果开启 mock 功能,就会随机将返回的结果 (json 格式) 置空或者置 NULL,效果如下所示

一、原理:

可以模拟 http/https 的请求主要是依赖于 ios 中一个强大的类 NSURLProtocol。NSURLProtocol 是 iOS 中 URL Loading System 的一部分,开发者可以自定义一个 NSURLProtocol 并注册到 app 中,在这个自定义的 NSURLProtocol 中我们可以拦截 UIWebView,基于系统的 NSURLConnection 或者 NSURLSession 进行封装的网络请求,从而做到自定义的 response 返回。它在 ios 系统中处于这样一个位置:

通过 NSURLProtocol 的这个功能,拦截到请求包以后,为了 mock 返回包,会再次发出网络请求,并修改返回的 response 数据。具体 mock 代码如下图所示:

if(responseStub.isOnlineMock)
   {

       //这次请求不进行mock
       self.stub.isMock = NO ;
       //请求数据
       NSOperationQueue *queue = [[NSOperationQueue alloc]init];
       //发送异步请求
       [NSURLConnection sendAsynchronousRequest:request
                                          queue:queue
                              completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){

                                  if(error){
                                      NSLog(@"Httperror:%@%@", error.localizedDescription,@(error.code));
                                      [self handleError:[NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil] request:request responseStub:responseStub client:client] ;
                                  }else{
                                      //NSData convert to NSString
                                      NSString* body = [self convertJSONStringFromData:data];
                                      NSString *mock_body = [self mockHTTP:body] ;

                                      NSData *mock_data = [mock_body dataUsingEncoding:NSUTF8StringEncoding] ;

                                      NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
                                      int statusCode = (int)httpResponse.statusCode;
                                      NSDictionary *headers = httpResponse.allHeaderFields ;
                                      NSTimeInterval requestTime = responseStub.requestTime ;
                                      NSTimeInterval responseTime = responseStub.responseTime ;
                                      OHHTTPStubsResponse *ohHTTPStubsResponse =  [[OHHTTPStubsResponse responseWithData:mock_data
                                                                                                              statusCode:statusCode
                                                                                                                 headers:headers] requestTime:requestTime responseTime:responseTime];

                                      [self handleRequest:request responseStub:ohHTTPStubsResponse client:client] ;
                                  }
                                  self.stub.isMock = YES ;
                              }];


   }

通过增加 http/https 的在线 mock 能力 + 本身提供的 monkey 能力即可以快速实现 fuzzy 测试


↙↙↙阅读原文可查看相关链接,并与作者交流