を使用し、異なるパラメーター「パス」でGCDWebServer
メソッドを2回実行します。-addHandlerForMethod:path:requestClass:
次に NSLog webServer.serverURL
、
- 初回成功:192.168.0.121:8080,
- しかし、2回目は失敗します: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);
}];
}