0

I use GCDWebServer, run method -addHandlerForMethod:path:requestClass: twice with different parameter "path". then NSLog webServer.serverURL,

  • first time it success:192.168.0.121:8080,
  • but second time it fail:nil

why? please help me.

#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);
}];
}
soumya
  • 3,801
  • 9
  • 35
  • 69

1 Answers1

0

Check the Xcode console for errors. The problem is most likely that you are you not stopping the GCDWebServer instance after calling the block, so it's still running and holding on port 8080, preventing new servers to start.

Pol
  • 3,848
  • 1
  • 38
  • 55