我想一个GHUnit测试用例添加到这个SimpleHTTPServer example。这个例子包括一个适合我的Cocoa应用程序。但是我不能在测试用例中复制行为。HTTP服务器在Cocoa应用程序中工作,但不是测试用例 - 运行循环问题?
下面是测试类:通过网络浏览器
#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确实得到通知。
谢谢。我在我的代码中解决了这个问题,但是这对我所问的问题没有帮助。现在数据总是零,错误“请求超时”。 – jlstrecker 2011-04-01 03:11:39
您是否尝试过使用更长的timeoutInterval? – 2011-04-01 10:47:18
是的,最多10秒。 – jlstrecker 2011-04-01 15:19:11