我使用不同的参数“路径”两次GCDWebServer
运行方法。-addHandlerForMethod:path:requestClass:
然后 NSLog webServer.serverURL
,
- 第一次成功:192.168.0.121:8080,
- 但第二次失败:nil
为什么?请帮我。
#import "ServerMock.h"
@implementation ServerMock
+ (void)mockWithMethod:(NSString *)method path:(NSString *)path timeoutInterval:(NSTimeInterval)timeoutInterval JSONObject:(NSDictionary *)JSONObject port:(NSUInteger)port serverURL:(void (^)(NSURL *serverURL))block
{
GCDWebServer *webServer = [GCDWebServer new];
[webServer addHandlerForMethod:method path:path requestClass:[GCDWebServerRequest class] asyncProcessBlock:^(GCDWebServerRequest *request, GCDWebServerCompletionBlock completionBlock) {
GCD_DELAY_AFTER(timeoutInterval, ^{
GCDWebServerDataResponse *response = [GCDWebServerDataResponse responseWithJSONObject:JSONObject];
completionBlock(response);
});
}];
[webServer startWithPort:port bonjourName:nil];
block(webServer.serverURL);
}
@end
//////////////////////////////////////
- (void)viewDidLoad
{
NSDictionary *dict = @{
@"11111":
@"22222222"
};
[ServerMock mockWithMethod:@"GET"
path:@"/123"
timeoutInterval:0
JSONObject:dict
port:8080 serverURL:^(NSURL *serverURL) {
NSLog(@"________%@", serverURL);
}];
NSDictionary *dict2 = @{ @"2222222": @"111111111"};
[ServerMock mockWithMethod:@"GET" path:@"/321" timeoutInterval:0 JSONObject:dict2 port:8080 serverURL:^(NSURL *serverURL) {
NSLog(@"________%@", serverURL);
}];
}