Skip to content

Commit 099b2a0

Browse files
committed
Updated run APIs to use options
1 parent f0c63f4 commit 099b2a0

3 files changed

Lines changed: 20 additions & 9 deletions

File tree

GCDWebServer/Core/GCDWebServer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ extern NSString* const GCDWebServerOption_AutomaticallySuspendInBackground; //
8383
@property(nonatomic, readonly) NSURL* serverURL; // Only non-nil if server is running
8484
@property(nonatomic, readonly) NSURL* bonjourServerURL; // Only non-nil if server is running and Bonjour registration is active
8585
#if !TARGET_OS_IPHONE
86-
- (BOOL)runWithPort:(NSUInteger)port; // Starts then automatically stops on SIGINT i.e. Ctrl-C (use on main thread only)
86+
- (BOOL)runWithPort:(NSUInteger)port bonjourName:(NSString*)name;
87+
- (BOOL)runWithOptions:(NSDictionary*)options; // Starts then automatically stops on SIGINT i.e. Ctrl-C (use on main thread only)
8788
#endif
8889
@end
8990

@@ -113,7 +114,7 @@ extern NSString* const GCDWebServerOption_AutomaticallySuspendInBackground; //
113114

114115
@interface GCDWebServer (Testing)
115116
@property(nonatomic, getter=isRecordingEnabled) BOOL recordingEnabled; // Creates files in the current directory containing the raw data for all requests and responses (directory most NOT contain prior recordings)
116-
- (NSInteger)runTestsInDirectory:(NSString*)path withPort:(NSUInteger)port; // Returns number of failed tests or -1 if server failed to start
117+
- (NSInteger)runTestsWithOptions:(NSDictionary*)options inDirectory:(NSString*)path; // Returns number of failed tests or -1 if server failed to start
117118
@end
118119

119120
#endif

GCDWebServer/Core/GCDWebServer.m

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -596,13 +596,20 @@ - (NSURL*)bonjourServerURL {
596596

597597
#if !TARGET_OS_IPHONE
598598

599-
- (BOOL)runWithPort:(NSUInteger)port {
599+
- (BOOL)runWithPort:(NSUInteger)port bonjourName:(NSString*)name {
600+
NSMutableDictionary* options = [NSMutableDictionary dictionary];
601+
[options setObject:[NSNumber numberWithInteger:port] forKey:GCDWebServerOption_Port];
602+
[options setValue:name forKey:GCDWebServerOption_BonjourName];
603+
return [self runWithOptions:options];
604+
}
605+
606+
- (BOOL)runWithOptions:(NSDictionary*)options {
600607
DCHECK([NSThread isMainThread]);
601608
BOOL success = NO;
602609
_run = YES;
603610
void (*handler)(int) = signal(SIGINT, _SignalHandler);
604611
if (handler != SIG_ERR) {
605-
if ([self startWithPort:port bonjourName:@""]) {
612+
if ([self startWithOptions:options]) {
606613
while (_run) {
607614
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0, true);
608615
}
@@ -899,10 +906,10 @@ static void _LogResult(NSString* format, ...) {
899906
ARC_RELEASE(message);
900907
}
901908

902-
- (NSInteger)runTestsInDirectory:(NSString*)path withPort:(NSUInteger)port {
909+
- (NSInteger)runTestsWithOptions:(NSDictionary*)options inDirectory:(NSString*)path {
903910
NSArray* ignoredHeaders = @[@"Date", @"Etag"]; // Dates are always different by definition and ETags depend on file system node IDs
904911
NSInteger result = -1;
905-
if ([self startWithPort:port bonjourName:nil]) {
912+
if ([self startWithOptions:options]) {
906913

907914
result = 0;
908915
NSArray* files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
@@ -927,7 +934,7 @@ - (NSInteger)runTestsInDirectory:(NSString*)path withPort:(NSUInteger)port {
927934
if (responseData) {
928935
CFHTTPMessageRef expectedResponse = _CreateHTTPMessageFromData(responseData, NO);
929936
if (expectedResponse) {
930-
CFHTTPMessageRef actualResponse = _CreateHTTPMessageFromPerformingRequest(requestData, port);
937+
CFHTTPMessageRef actualResponse = _CreateHTTPMessageFromPerformingRequest(requestData, self.port);
931938
if (actualResponse) {
932939
success = YES;
933940

Mac/main.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,17 @@ int main(int argc, const char* argv[]) {
274274
webServer.delegate = delegate;
275275
if (testDirectory) {
276276
fprintf(stdout, "<RUNNING TESTS FROM \"%s\">\n\n", [testDirectory UTF8String]);
277-
result = (int)[webServer runTestsInDirectory:testDirectory withPort:8080];
277+
result = (int)[webServer runTestsWithOptions:@{GCDWebServerOption_Port: @8080} inDirectory:testDirectory];
278278
} else {
279279
if (recording) {
280280
fprintf(stdout, "<RECORDING ENABLED>\n");
281281
webServer.recordingEnabled = YES;
282282
}
283283
fprintf(stdout, "\n");
284-
if ([webServer runWithPort:8080]) {
284+
NSMutableDictionary* options = [NSMutableDictionary dictionary];
285+
[options setObject:@8080 forKey:GCDWebServerOption_Port];
286+
[options setObject:@"" forKey:GCDWebServerOption_BonjourName];
287+
if ([webServer runWithOptions:options]) {
285288
result = 0;
286289
}
287290
}

0 commit comments

Comments
 (0)