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测试
参考文章
1、https://github.com/AliSoftware/OHHTTPStubs
2、https://www.jianshu.com/p/4091870ca3f0
3、https://www.jianshu.com/p/03ddcfe5ebd7
4、https://github.com/draveness/analyze/blob/master/contents/OHHTTPStubs/如何进行%20HTTP%20Mock(iOS).md
5、https://www.jianshu.com/p/0cfd2fa7a8df
6、http://wereadteam.github.io/2016/02/25/GYHttpMock/
7、https://github.com/hypoyao/GYHttpMock
8、