Я пытаюсь добавить тестовый пример GHUnit к этому SimpleHTTPServer примеру . Пример включает приложение Какао, которое отлично работает для меня. Но я не могу дублировать поведение в тестовом случае.
Вот тестовый класс:
#import <GHUnit/GHUnit.h>
#import "SimpleHTTPServer.h"
@interface ServerTest : GHTestCase
{
SimpleHTTPServer *server;
}
@end
@implementation ServerTest
-(void)setUpClass
{
[[NSRunLoop currentRunLoop] run];
}
- (NSString*)requestToURL:(NSString*)urlString error:(NSError**)error
{
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:1];
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error];
NSString *page = nil;
if (error == nil)
{
NSStringEncoding responseEncoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef)[response textEncodingName]));
page = [[NSString alloc] initWithData:data encoding:responseEncoding];
[page autorelease];
}
return page;
}
- (void)testPortReuse
{
unsigned int port = 50001;
NSError *error = nil;
NSString *path, *url;
server = [[SimpleHTTPServer alloc] initWithTCPPort:port delegate:self];
sleep(10);
path = @"/x/y/z";
url = [NSString stringWithFormat:@"http://localhost:%u%@", port, path];
[self requestToURL:url error:&error];
GHAssertNil(error, @"%@ : %@", url, error);
[server release];
}
- (void)processURL:(NSURL *)path connection:(SimpleHTTPConnection *)connection
{
NSLog(@"processURL");
}
- (void)stopProcessing
{
NSLog(@"stopProcessing");
}
@end
Я пытался отправить запросы через NSURLRequest, а также (во время sleep
) через веб-браузер. Методы делегатов -processURL
и -stopProcessing
никогда не вызываются. Кажется, проблема в том, что [fileHandle acceptConnectionInBackgroundAndNotify]
в SimpleHTTPServer -initWithTCPPort:delegate:
не приводит к тому, что NSFileHandleConnectionAcceptedNotifications не достигает NSNotificationCenter - поэтому я подозреваю, что проблема связана с циклами выполнения.
Проблема, похоже, связана с NSFileHandle, а не с NSNotificationCenter, потому что, когда [nc postNotificationName:NSFileHandleConnectionAcceptedNotification object:nil]
добавляется в конец initWithTCPPort:delegate:
, NSNotificationCenter действительно получает уведомление.