Merge pull request #379 from svgol/refactor_NSURLConnection_tests

Refactor NSURLConnection tests
This commit is contained in:
rfm 2024-04-04 13:39:07 +01:00 committed by GitHub
commit 9be55cc80a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 443 additions and 744 deletions

View file

@ -6,7 +6,7 @@ NEEDS_GUI=NO
TestConnection_OBJC_FILES = TestCase.m SimpleWebServer.m TestWebServer.m NSURLConnectionTest.m RequestHandler.m
#TestConnection_OBJC_LIBS += -lWebServer -lPerformance
TestConnection_RESOURCE_FILES += testKey.pem testCert.pem
TestConnection_RESOURCE_FILES += key.pem certificate.pem
TestConnection_PRINCIPAL_CLASS = NSURLConnectionTest
TOOL_NAME = testTestWebServer

View file

@ -1,6 +1,6 @@
/** -*- objc -*-
*
* Author: Sergei Golovin <Golovin.SV@gmail.com>
* Author: Sergei Golovin <svgdev@mail.ru>
*
* The class is intended to test the class NSURLConnection. It is designed to start
* a TestWebServer instance and make NSURLConnection to it getting TestWebServerDelegate's

View file

@ -1,5 +1,5 @@
/*
* Author: Sergei Golovin <Golovin.SV@gmail.com>
* Author: Sergei Golovin <svgdev@mail.ru>
*/
#import "NSURLConnectionTest.h"
@ -163,7 +163,6 @@ static NSMapTable *_flagMap = nil;
DESTROY(_request);
DESTROY(_redirectRequest);
DESTROY(_expectedContent);
[_conn cancel];
DESTROY(_conn);
DESTROY(_error);
@ -246,10 +245,7 @@ static NSMapTable *_flagMap = nil;
NSData *content = [request convertToData];
[self setFlags: SENTREQUEST];
if (YES == _debug)
{
NSLog(@"%@: set SENTREQUEST (-[%@])", self, NSStringFromSelector(_cmd));
}
NSDebugLog(@"%@: set SENTREQUEST (-[%@])", self, NSStringFromSelector(_cmd));
// TODO: more comparisons of _request and request
if ([method isEqualToString: @"POST"] ||
@ -258,19 +254,13 @@ static NSMapTable *_flagMap = nil;
if ([content isEqualToData: [_request HTTPBody]])
{
[self setFlags: GOTREQUEST];
if (YES == _debug)
{
NSLog(@"%@: set GOTREQUEST (-[%@])", self, NSStringFromSelector(_cmd));
}
NSDebugLog(@"%@: set GOTREQUEST (-[%@])", self, NSStringFromSelector(_cmd));
}
}
else
{
[self setFlags: GOTREQUEST];
if (YES == _debug)
{
NSLog(@"%@: set GOTREQUEST (-[%@])", self, NSStringFromSelector(_cmd));
}
NSDebugLog(@"%@: set GOTREQUEST (-[%@])", self, NSStringFromSelector(_cmd));
}
}
@ -279,10 +269,7 @@ willSendUnauthorized:(GSMimeDocument *)response
with:(TestWebServer *)server
{
[self setFlags: NOTAUTHORIZED];
if (YES == _debug)
{
NSLog(@"%@: set NOTAUTHORIZED (-[%@])", self, NSStringFromSelector(_cmd));
}
NSDebugLog(@"%@: set NOTAUTHORIZED (-[%@])", self, NSStringFromSelector(_cmd));
}
- (void)handler:(id)handler
@ -290,10 +277,7 @@ willSendUnauthorized:(GSMimeDocument *)response
with:(TestWebServer *)server
{
[self setFlags: AUTHORIZED];
if (YES == _debug)
{
NSLog(@"%@: set AUTHORIZED (-[%@])", self, NSStringFromSelector(_cmd));
}
NSDebugLog(@"%@: set AUTHORIZED (-[%@])", self, NSStringFromSelector(_cmd));
}
- (void)handler:(id)handler
@ -301,10 +285,7 @@ willSendUnauthorized:(GSMimeDocument *)response
with:(TestWebServer *)server
{
[self setFlags: SENTRESPONSE];
if (YES == _debug)
{
NSLog(@"%@: set SENTRESPONSE (-[%@])", self, NSStringFromSelector(_cmd));
}
NSDebugLog(@"%@: set SENTRESPONSE (-[%@])", self, NSStringFromSelector(_cmd));
}
- (void)timeoutExceededByHandler:(id)handler
@ -323,19 +304,13 @@ willSendUnauthorized:(GSMimeDocument *)response
if ([(NSHTTPURLResponse *)redirectResponse statusCode] == _redirectStatusCode)
{
[self setFlags: GOTREDIRECT];
if (YES == _debug)
{
NSLog(@"%@: set GOTREDIRECT (-[%@])", self, NSStringFromSelector(_cmd));
}
NSDebugLog(@"%@: set GOTREDIRECT (-[%@])", self, NSStringFromSelector(_cmd));
}
}
else
{
[self setFlags: GOTREDIRECT];
if (YES == _debug)
{
NSLog(@"%@: set GOTREDIRECT (-[%@])", self, NSStringFromSelector(_cmd));
}
NSDebugLog(@"%@: set GOTREDIRECT (-[%@])", self, NSStringFromSelector(_cmd));
}
return _redirectRequest;
@ -345,10 +320,7 @@ willSendUnauthorized:(GSMimeDocument *)response
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
[self setFlags: GOTUNAUTHORIZED];
if (YES == _debug)
{
NSLog(@"%@: set GOTUNAUTHORIZED (-[%@])", self, NSStringFromSelector(_cmd));
}
NSDebugLog(@"%@: set GOTUNAUTHORIZED (-[%@])", self, NSStringFromSelector(_cmd));
if ([challenge previousFailureCount] == 0)
{
@ -401,19 +373,13 @@ didReceiveResponse:(NSURLResponse *)response
if ([(NSHTTPURLResponse *)response statusCode] == _expectedStatusCode)
{
[self setFlags: GOTRESPONSE];
if (YES == _debug)
{
NSLog(@"%@: set GOTRESPONSE (-[%@])", self, NSStringFromSelector(_cmd));
}
NSDebugLog(@"%@: set GOTRESPONSE (-[%@])", self, NSStringFromSelector(_cmd));
}
}
else
{
[self setFlags: GOTRESPONSE];
if (YES == _debug)
{
NSLog(@"%@: set GOTRESPONSE (-[%@])", self, NSStringFromSelector(_cmd));
}
NSDebugLog(@"%@: set GOTRESPONSE (-[%@])", self, NSStringFromSelector(_cmd));
}
}
@ -421,10 +387,7 @@ didReceiveResponse:(NSURLResponse *)response
didReceiveData:(NSData *)data
{
[_received appendData: data];
if (YES == _debug)
{
NSLog(@"%@: received data '%@' (-[%@])", self, data, NSStringFromSelector(_cmd));
}
NSDebugLog(@"%@: received data '%@' (-[%@])", self, data, NSStringFromSelector(_cmd));
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
@ -433,17 +396,11 @@ didReceiveResponse:(NSURLResponse *)response
[_received isEqualToData: _expectedContent])
{
[self setFlags: GOTCONTENT];
if (YES == _debug)
{
NSLog(@"%@: set GOTCONTENT (-[%@])", self, NSStringFromSelector(_cmd));
}
NSDebugLog(@"%@: set GOTCONTENT (-[%@])", self, NSStringFromSelector(_cmd));
}
[self setFlags: GOTFINISH];
if (YES == _debug)
{
NSLog(@"%@: set GOTFINISH (-[%@])", self, NSStringFromSelector(_cmd));
}
NSDebugLog(@"%@: set GOTFINISH (-[%@])", self, NSStringFromSelector(_cmd));
_done = YES;
}
@ -461,11 +418,8 @@ didReceiveResponse:(NSURLResponse *)response
*/
[self setFlags: GOTFAIL];
if (YES == _debug)
{
NSLog(@"%@: set GOTFAIL (-[%@])", self, NSStringFromSelector(_cmd));
NSLog(@"%@: error %@", self, error);
}
NSDebugLog(@"%@: set GOTFAIL (-[%@])", self, NSStringFromSelector(_cmd));
NSDebugLog(@"%@: error %@", self, error);
_done = YES;
}
@ -515,7 +469,7 @@ didReceiveResponse:(NSURLResponse *)response
{
d = [d mutableCopy];
[(NSMutableDictionary *)d setObject: protocol forKey: @"Protocol"];
[d autorelease];
AUTORELEASE(d);
}
address = [instance address];

View file

@ -1,6 +1,6 @@
/** -*- objc -*-
*
* Author: Sergei Golovin <Golovin.SV@gmail.com>
* Author: Sergei Golovin <svgdev@mail.ru>
*
*/

View file

@ -1,6 +1,6 @@
/** -*- objc -*-
*
* Author: Sergei Golovin <Golovin.SV@gmail.com>
* Author: Sergei Golovin <svgdev@mail.ru>
*
*/
@ -27,6 +27,8 @@
- (void)dealloc
{
_delegate = nil;
DESTROY(_login);
DESTROY(_password);
[super dealloc];
}
@ -76,12 +78,7 @@
NSString *path;
BOOL ret = NO;
if(YES == _debug)
{
NSLog(@"%@: BEGIN\n%@", self, request);
}
NSDebugLog(@"%@: BEGIN\n%@", self, request);
if(nil != _delegate && [_delegate respondsToSelector: @selector(handler:gotRequest:with:)])
{
@ -105,10 +102,7 @@
[_delegate handler: self willSendUnauthorized: response with: server];
}
if(YES == _debug)
{
NSLog(@"%@: about to send Unauthorized\n%@", self, response);
}
NSDebugLog(@"%@: about to send Unauthorized\n%@", self, response);
return YES;
}
@ -124,10 +118,7 @@
if([[credentials objectAtIndex:0] isEqualToString: _login] &&
[[credentials objectAtIndex:1] isEqualToString: _password])
{
if(YES == _debug)
{
NSLog(@"%@: got valid credentials", self);
}
NSDebugLog(@"%@: got valid credentials", self);
if(nil != _delegate && [_delegate respondsToSelector: @selector(handler:gotAuthorized:with:)])
{
@ -149,10 +140,7 @@
[_delegate handler: self willSendUnauthorized: response with: server];
}
if(YES == _debug)
{
NSLog(@"%@: about to send Unauthorized\n%@", self, response);
}
NSDebugLog(@"%@: about to send Unauthorized\n%@", self, response);
return YES;
}
@ -183,10 +171,7 @@
}
// TODO: may be to move at another place?
if(YES == _debug)
{
NSLog(@"%@: about to send\n%@", self, response);
}
NSDebugLog(@"%@: about to send\n%@", self, response);
return ret;
}

View file

@ -1,6 +1,6 @@
/** -*- objc -*-
*
* Author: Sergei Golovin <Golovin.SV@gmail.com>
* Author: Sergei Golovin <svgdev@mail.ru>
*
*/
@ -28,8 +28,14 @@
*/
@interface SimpleWebServer : NSObject
{
/* holds the GSServerStream accepting incoming connections */
GSServerStream *_serverStream;
/* holds the file handler of connection */
NSFileHandle *_fh;
/* holds the 'near' file handler of connection...
see "Background Inter-Process Communication Using Sockets"
of Low-Level File Management Programming Topics
*/
NSFileHandle *_cfh;
/* the delegate ... NOT RETAINED...
* see below the protocol SimpleWebServerDelegate */
id _delegate;
@ -41,36 +47,18 @@
NSString *_port;
/* SSL configuration and options */
NSDictionary *_secure;
/* The following web-server code is derived/stolen from NSURL/Helpers/capture.m */
/* the stream to send */
NSOutputStream *_op;
/* the stream to receive */
NSInputStream *_ip;
/* the collector of received bytes from a client */
NSMutableData *_capture;
/* the number of sent bytes to a client */
unsigned _written;
/* the flag indicating the instance is collecting bytes from a client */
BOOL _readable;
/* the flag indicating the instance is sending bytes to a client */
BOOL _writable;
/* whether to use a secure TLS/SSL connection */
BOOL _isSecure;
/* the request is read */
BOOL _doRespond;
/* the response is written */
BOOL _done;
/* end of the stolen */
/* wether the output stream is ready to write */
BOOL _canRespond;
/* the collector of received bytes from a client */
NSMutableData *_capture;
/* holds the current request */
GSMimeDocument *_request;
/* holds the current response */
GSMimeDocument *_response;
/* the flag the server wants to operate */
BOOL _isRunning;
/* to close the connection after sending the response */
BOOL _isClose;
}
- (void)dealloc;
@ -111,9 +99,6 @@
*/
- (void)stop;
/* The method is derived/stolen from NSURL/Helpers/capture.m */
- (void) stream: (NSStream *)theStream handleEvent: (NSStreamEvent)streamEvent;
@end /* SimpleWebServer */
@protocol SimpleWebServerDelegate

View file

@ -1,39 +1,27 @@
/*
* Author: Sergei Golovin <Golovin.SV@gmail.com>
* Author: Sergei Golovin <svgdev@mail.ru>
*/
#import "SimpleWebServer.h"
/* the time step for the runloop */
#define TIMING 0.1
/* the time step for the runloop */
#define TIMING 0.1
@interface SimpleWebServer (Private)
/**
* Starts a listening (server) stream.
* Starts listening. Returns NO if the instance can't listen.
*/
- (void)_openServerStream;
- (BOOL) _startListening;
/**
* Stops the listening (server) stream.
* Receives NSFileHandleConnectionAcceptedNotification.
*/
- (void)_closeServerStream;
- (void) _accept:(NSNotification *)ntf;
/**
* Reset to prepare for the next request-response cycle.
* Receives NSFileHandleReadCompletionNotification.
*/
- (void)_resetCycle;
/**
* Opens the input _ip and output _op streams, schedules them
* on the current runloop. Requires the _ip and _op are not nil.
*/
- (void)_openIOStreams;
/**
* Closes the input _ip and output _op streams, unschedules them
* on the current runloop and clears that ivars.
*/
- (void)_closeIOStreams;
- (void) _read:(NSNotification *)ntf;
/**
* Tries to recognise if the request's bytes have been read (HTTP message's
@ -42,18 +30,34 @@
*/
- (BOOL)_tryCaptured;
/**
* Makes and sends response.
*/
- (void) _makeAndSendResponse;
/**
* Reset to prepare for the next request-response cycle.
*/
- (void)_resetCycle;
/**
* Closes IO
*/
- (void) _close;
@end /* SimpleWebServer (Private) */
@implementation SimpleWebServer
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver: self];
_delegate = nil;
DESTROY(_capture);
DESTROY(_request);
DESTROY(_response);
[self _closeIOStreams];
[self _closeServerStream];
DESTROY(_cfh);
DESTROY(_fh);
DESTROY(_address);
DESTROY(_port);
DESTROY(_secure);
@ -66,11 +70,9 @@
{
_debug = NO;
_delegate = nil;
_doRespond = NO;
_done = NO;
_canRespond = NO;
_ip = nil;
_op = nil;
_isSecure = NO;
_isRunning = NO;
_isClose = NO;
}
return self;
@ -79,13 +81,12 @@
/* getters */
- (NSString *)port
{
NSStream *s = _serverStream;
if (nil !=_fh)
{
return _port;
}
if (nil == s) s = _ip;
if (nil == s) s = _op;
if (nil == s) return nil;
return [s streamStatus] == NSStreamStatusOpen ? _port : nil;
return nil;
}
/* end of getters */
@ -94,26 +95,11 @@
port:(NSString *)port
secure:(NSDictionary *)dict
{
BOOL ret = NO;
ASSIGN(_address, address);
ASSIGN(_port, port);
ASSIGN(_secure, dict);
if (nil == _serverStream)
{
ASSIGN(_address, address);
ASSIGN(_port, port);
ASSIGN(_secure, dict);
[self _openServerStream];
if (nil != _serverStream)
{
ret = YES;
}
}
else
{
NSLog(@"%@: already started '%@' on '%@'",
self, _serverStream, [NSThread currentThread]);
}
return ret;
return [self _startListening];
}
- (void)setDebug:(BOOL)flag
@ -130,353 +116,131 @@
- (void)stop
{
NSRunLoop *rl = [NSRunLoop currentRunLoop];;
if (nil != _serverStream)
{
[self _closeServerStream];
DESTROY(_address);
DESTROY(_port);
DESTROY(_secure);
}
[self _closeIOStreams];
// give a time slice to free all resources
[rl runUntilDate: [NSDate dateWithTimeIntervalSinceNow: TIMING]];
[[NSNotificationCenter defaultCenter] removeObserver: self
name: NSFileHandleConnectionAcceptedNotification
object: _fh];
[self _close];
DESTROY(_fh);
DESTROY(_address);
DESTROY(_port);
DESTROY(_secure);
_isRunning = NO;
}
/* GSStream's delegate */
/* The method is derived/stolen from NSURL/Helpers/capture.m */
- (void) stream: (NSStream *)theStream handleEvent: (NSStreamEvent)streamEvent
{
NSRunLoop *rl = [NSRunLoop currentRunLoop];
// NSLog(@"Event %p %d", theStream, streamEvent);
switch (streamEvent)
{
case NSStreamEventHasBytesAvailable:
{
if (_ip == nil)
{
[self _resetCycle];
[(GSServerStream*)_serverStream acceptWithInputStream: &_ip
outputStream: &_op];
if (_ip) // it is ok to accept nothing
{
RETAIN(_ip);
RETAIN(_op);
[self _openIOStreams];
[self _closeServerStream];
}
}
if (theStream == _ip)
{
_readable = YES;
while (_readable == YES)
{
unsigned char buffer[BUFSIZ];
int readSize;
readSize = [_ip read: buffer maxLength: sizeof(buffer)];
if (readSize <= 0)
{
_readable = NO;
}
else
{
[_capture appendBytes: buffer length: readSize];
}
}
_doRespond = [self _tryCaptured];
if (_doRespond)
{
// reset the output stream to trigger polling
[_op write: NULL maxLength: 0];
if (YES == _debug)
{
NSLog(@"%@: about to send response\n%@", self, _response);
}
}
}
break;
}
case NSStreamEventHasSpaceAvailable:
{
if (_doRespond && _canRespond)
{
NSMutableData *data;
NSString *status;
NSData *statusData;
char *crlf = "\r\n";
id content;
NSData *contentData = nil;
NSUInteger cLength = 0; // content-length
NSString *connection;
BOOL close = YES;
NSAssert(theStream == _op, @"Wrong stream for writing");
_writable = YES;
// adding the 'Connection' to the response
connection = [[_request headerNamed: @"Connection"] value];
if (nil == connection)
{
connection = [[_request headerNamed: @"connection"] value];
}
// if the client didn't supply the header 'Connection' or
// explicitly stated to close the current connection
close = (nil == connection
|| [[connection lowercaseString] isEqualToString: @"close"]);
// adding the 'Content-Length' to the response
content = [_response content];
if ([content isKindOfClass: [NSString class]])
{
contentData = [(NSString *)content
dataUsingEncoding: NSUTF8StringEncoding];
}
else if ([content isKindOfClass: [NSData class]])
{
contentData = (NSData *)content;
}
else
{
// yet unsupported
}
if (nil != content)
{
cLength = [contentData length];
if (cLength > 0)
{
NSString *l;
l = [NSString stringWithFormat: @"%u", (unsigned)cLength];
[_response setHeader: @"Content-Length"
value: l
parameters: nil];
}
}
if (cLength == 0)
{
[_response setHeader: @"Content-Length"
value: @"0"
parameters: nil];
}
// adding the status line
status = [[_response headerNamed: @"http"] value];
if (nil == status)
{
status = [[_response headerNamed: @"HTTP"] value];
}
if (nil == status)
{
status = [[_response headerNamed: @"Http"] value];
}
statusData = [status dataUsingEncoding: NSUTF8StringEncoding];
data = [NSMutableData dataWithData: statusData];
[_response deleteHeaderNamed: @"http"];
[data appendBytes: crlf length: 2];
// actual sending
[data appendData: [_response rawMimeData]];
while (_writable == YES && _written < [data length])
{
int result = [_op write: [data bytes] + _written
maxLength: [data length] - _written];
if (result <= 0)
{
_writable = NO;
}
else
{
_written += result;
}
}
if (_written == [data length])
{
if (close)
{
// if the client didn't supply the header 'Connection' or explicitly stated
// to close the current connection
[self _closeIOStreams];
[self _openServerStream];
}
// ready for another request-response cycle
[self _resetCycle];
}
}
_canRespond = YES;
break;
}
case NSStreamEventEndEncountered:
{
if (theStream == _ip || theStream == _op)
{
[self _closeIOStreams];
[self _resetCycle];
[self _openServerStream];
}
else
{
[theStream close];
[theStream removeFromRunLoop: rl forMode: NSDefaultRunLoopMode];
}
break;
}
case NSStreamEventErrorOccurred:
{
int code = [[theStream streamError] code];
if (theStream == _ip || theStream == _op)
{
[self _closeIOStreams];
[self _resetCycle];
[self _openServerStream];
}
else
{
[theStream close];
[theStream removeFromRunLoop: rl forMode: NSDefaultRunLoopMode];
}
NSAssert1(1, @"Error! code is %d", code);
break;
}
default:
break;
}
}
/* end of GSStream's delegate */
@end /* SimpleWebServer */
@implementation SimpleWebServer (Private)
- (void)_openServerStream
- (BOOL) _startListening
{
NSString *certFile = nil;
NSString *keyFile = nil;
NSRunLoop *rl;
if (nil == _serverStream)
if (nil != _secure
&& [_secure objectForKey: @"CertificateFile"] != nil
&& [_secure objectForKey: @"KeyFile"] != nil)
{
rl = [NSRunLoop currentRunLoop];
Class sslClass = [NSFileHandle sslClass];
_serverStream = [GSServerStream serverStreamToAddr: _address port: [_port intValue]];
RETAIN(_serverStream);
if (_serverStream == nil)
_isSecure = YES;
_fh = [sslClass fileHandleAsServerAtAddress: _address
service: _port
protocol: @"tcp"];
[_fh sslSetOptions: _secure];
}
else
{
_fh = [NSFileHandle fileHandleAsServerAtAddress: _address
service: _port
protocol: @"tcp"];
}
RETAIN(_fh);
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(_accept:)
name: NSFileHandleConnectionAcceptedNotification
object: _fh];
[_fh acceptConnectionInBackgroundAndNotify];
_isRunning = YES;
return YES;
}
- (void) _accept:(NSNotification *)ntf
{
if (_isRunning)
{
NSDictionary *info = [ntf userInfo];
NSFileHandle *fh = [info objectForKey: NSFileHandleNotificationFileHandleItem];
if (_cfh != nil)
{
NSLog(@"Failed to create server stream (address: %@, port: %@)", _address, _port);
[self _close];
}
ASSIGN(_cfh, fh);
[_fh acceptConnectionInBackgroundAndNotify];
if (!_isSecure || (_isSecure && [_cfh sslAccept]))
{
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(_read:)
name: NSFileHandleReadCompletionNotification
object: _cfh];
[_cfh readInBackgroundAndNotify];
}
}
}
- (void) _read:(NSNotification *)ntf
{
if (_isRunning)
{
NSDictionary *info = [ntf userInfo];
if([info objectForKey: GSFileHandleNotificationError])
{
[self _close];
return;
}
if (nil != _secure &&
(certFile = [_secure objectForKey: @"CertificateFile"]) != nil &&
(keyFile = [_secure objectForKey: @"KeyFile"]) != nil)
if([[ntf name] isEqual: NSFileHandleReadCompletionNotification])
{
_isSecure = YES;
[_serverStream setProperty: NSStreamSocketSecurityLevelTLSv1 forKey: NSStreamSocketSecurityLevelKey];
[_serverStream setProperty: certFile forKey: GSTLSCertificateFile];
[_serverStream setProperty: keyFile forKey: GSTLSCertificateKeyFile];
}
NSData *hunk = [info objectForKey: NSFileHandleNotificationDataItem];
if (nil != hunk && [hunk length] > 0)
{
if (nil == _capture)
{
_capture = [NSMutableData new];
}
[_capture appendData: hunk];
if ([self _tryCaptured]) // <- the _request and _response are allocated
{
[self _makeAndSendResponse];
// ready for another request-response cycle
[self _resetCycle]; // <- the _request and _response are deallocated
_capture = [NSMutableData new];
[_serverStream setDelegate: self];
[_serverStream scheduleInRunLoop: rl forMode: NSDefaultRunLoopMode];
[_serverStream open];
if (YES == _debug)
{
NSLog(@"%@: started '%@' on '%@'", self, _serverStream, [NSThread currentThread]);
if (_isClose)
{
// if the client didn't supply the header 'Connection' or explicitly stated
// to close the current connection
[self _close];
return;
// BEWARE: it can left the socket busy for HTTP after server stopping (for HTTPS is OK)
// so consequent tests are failed bc their server can't bind
}
}
[_cfh readInBackgroundAndNotify];
}
else
{
[self _close];
return;
}
}
}
else
{
NSLog(@"%@: already started '%@' on '%@'", self, _serverStream, [NSThread currentThread]);
}
}
- (void)_closeServerStream
{
NSRunLoop *rl;
if (nil != _serverStream)
{
rl = [NSRunLoop currentRunLoop];
[_serverStream close];
[_serverStream removeFromRunLoop: rl forMode: NSDefaultRunLoopMode];
if (YES == _debug)
{
NSLog(@"%@: stopped server stream %@", self, _serverStream);
}
DESTROY(_serverStream);
}
}
- (void)_resetCycle
{
_written = 0;
_doRespond = NO;
_done = NO;
_canRespond = NO;
[_capture setLength: 0];
}
- (void)_openIOStreams
{
NSRunLoop *rl;
if (_ip != nil && _op != nil)
{
rl = [NSRunLoop currentRunLoop];
[_ip scheduleInRunLoop: rl forMode: NSDefaultRunLoopMode];
[_op scheduleInRunLoop: rl forMode: NSDefaultRunLoopMode];
[_ip setDelegate: self];
[_op setDelegate: self];
[_ip open];
[_op open];
}
else
{
[NSException raise: NSInternalInconsistencyException
format: @"%@: IO streams not properly initialized", self];
}
}
- (void)_closeIOStreams
{
NSRunLoop *rl;
if (nil != _ip || nil != _op)
{
rl = [NSRunLoop currentRunLoop];
[_ip close];
[_ip removeFromRunLoop: rl forMode: NSDefaultRunLoopMode];
[_op close];
[_op removeFromRunLoop: rl forMode: NSDefaultRunLoopMode];
DESTROY(_ip);
DESTROY(_op);
}
}
- (BOOL)_tryCaptured
- (BOOL) _tryCaptured
{
BOOL ret = NO;
NSRange r1;
@ -490,7 +254,7 @@
// when all request's bytes are read... it waits for full headers and
// reads the Content-Length's value then waits for the number of bytes
// equal to that value is read
tmp1 = [[NSString alloc] initWithData: _capture
tmp1 = [[NSString alloc] initWithData: _capture
encoding: NSUTF8StringEncoding];
// whether the headers are read
if ((r1 = [tmp1 rangeOfString: @"\r\n\r\n"]).location != NSNotFound)
@ -521,7 +285,7 @@
NSData *data;
// TODO: currently no checks
r2 = [headers rangeOfString: @"\r\n"];
r2 = [headers rangeOfString: @"\r\n"];
while (r2.location == 0)
{
// ignore an empty line before the request line
@ -539,11 +303,11 @@
tmp2 = [tmp2 stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
// find the method
r2 = [tmp2 rangeOfString: @" "];
r2 = [tmp2 rangeOfString: @" "];
method = [[tmp2 substringToIndex: r2.location] uppercaseString];
tmp2 = [[tmp2 substringFromIndex: r2.location + 1]
stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
r2 = [tmp2 rangeOfString: @"?"]; // path?query
if (r2.location != NSNotFound)
{
@ -568,15 +332,15 @@
[_request setHeader: @"x-http-method"
value: method
parameters: nil];
[_request setHeader: @"x-http-path"
value: path
parameters: nil];
[_request setHeader: @"x-http-query"
value: query
parameters: nil];
[_request setHeader: @"x-http-scheme"
value: scheme
parameters: nil];
@ -584,12 +348,11 @@
[_request setHeader: @"x-http-version"
value: version
parameters: nil];
if (YES == _debug)
{
NSLog(@"%@: got request\n%@", self, _request);
}
NSDebugLog(@"%@: got request\n%@", self, _request);
_response = [GSMimeDocument new];
if (nil != _delegate && [_delegate respondsToSelector: @selector(processRequest:response:for:)])
{
ret = [_delegate processRequest: _request response: _response for: self];
@ -609,4 +372,92 @@
return ret;
}
- (void) _makeAndSendResponse
{
NSMutableData *data;
NSString *status;
NSData *statusData;
char *crlf = "\r\n";
id content;
NSData *contentData = nil;
NSUInteger cLength = 0; // content-length
NSString *connection;
// adding the 'Connection' to the response
connection = [[_request headerNamed: @"connection"] value];
// if the client didn't supply the header 'Connection' or
// explicitly stated to close the current connection
_isClose = (nil == connection ||
[[connection lowercaseString] isEqualToString: @"close"]);
// adding the 'Content-Length' to the response
content = [_response content];
if ([content isKindOfClass: [NSString class]])
{
contentData = [(NSString *)content
dataUsingEncoding: NSUTF8StringEncoding];
}
else if ([content isKindOfClass: [NSData class]])
{
contentData = (NSData *)content;
}
else
{
// yet unsupported
}
if (nil != content)
{
cLength = [contentData length];
if (cLength > 0)
{
NSString *l;
l = [NSString stringWithFormat: @"%u", (unsigned)cLength];
[_response setHeader: @"Content-Length"
value: l
parameters: nil];
}
}
if (cLength == 0)
{
[_response setHeader: @"Content-Length"
value: @"0"
parameters: nil];
}
// adding the status line
status = [[_response headerNamed: @"http"] value];
statusData = [status dataUsingEncoding: NSUTF8StringEncoding];
data = [[NSMutableData alloc] initWithData: statusData];
[_response deleteHeaderNamed: @"http"];
[data appendBytes: crlf length: 2];
// actual sending
[data appendData: [_response rawMimeData]];
NSDebugLog(@"%@: about to send response\n%@", self, _response);
[_cfh writeData: data];
RELEASE(data);
}
- (void)_resetCycle
{
DESTROY(_response);
DESTROY(_request);
DESTROY(_capture);
}
- (void) _close
{
[[NSNotificationCenter defaultCenter] removeObserver: self
name: NSFileHandleReadCompletionNotification
object: _cfh];
if(_isSecure)
{
[_cfh sslDisconnect];
}
DESTROY(_cfh);
}
@end /* SimpleWebServer (Private) */

View file

@ -1,6 +1,6 @@
/* -*- objc -*-
*
* Author: Sergei Golovin <Golovin.SV@gmail.com>
* Author: Sergei Golovin <svgdev@mail.ru>
*/
#import <Foundation/Foundation.h>

View file

@ -1,5 +1,5 @@
/*
* Author: Sergei Golovin <Golovin.SV@gmail.com>
* Author: Sergei Golovin <svgdev@mail.ru>
*/
#import "TestCase.h"

View file

@ -1,7 +1,7 @@
/** -*- objc -*-
*
* Author: Sergei Golovin <Golovin.SV@gmail.com>
* Author: Sergei Golovin <svgdev@mail.ru>
*
* TestWebServer is intended to be used in the tests where a web service is
* needed. The class's instance is instantiated with the method

View file

@ -1,5 +1,5 @@
/*
* Author: Sergei Golovin <Golovin.SV@gmail.com>
* Author: Sergei Golovin <svgdev@mail.ru>
*/
#import "TestWebServer.h"
@ -142,6 +142,7 @@
DESTROY(_password);
_delegate = nil;
DESTROY(_traversalMap);
DESTROY(_server);
[super dealloc];
}
@ -149,17 +150,13 @@
{
if ([_server port] != nil)
{
if (_debug)
{
NSWarnMLog(@"SimpleWebServer already started");
}
NSWarnMLog(@"SimpleWebServer already started");
return;
}
if (nil != _serverThread)
{
if (_debug)
NSLog(@"Waiting for startup");
NSDebugLog(@"Waiting for startup");
if (![_serverThread isExecuting])
{
NSTimeInterval duration = 0.0;
@ -199,10 +196,7 @@
{
if ([_server port] == nil)
{
if (YES == _debug)
{
NSWarnMLog(@"SimpleWebServer already stopped");
}
NSWarnMLog(@"SimpleWebServer already stopped");
return;
}
if (nil != _serverThread)
@ -273,6 +267,7 @@
Class cls = (Class)handler;
handler = [[cls alloc] init];
[_traversalMap setObject: handler forKey: component];
RELEASE(handler);
}
// set the handler
@ -363,6 +358,10 @@
- (void)setDebug:(BOOL)mode
{
NSProcessInfo *pi = [NSProcessInfo processInfo];
[pi setDebugLoggingEnabled: mode];
_debug = mode;
}
@ -391,10 +390,10 @@
if ([h isEqual: l])
{
certPath = [[NSBundle bundleForClass: [self class]]
pathForResource: @"testCert"
pathForResource: @"certificate"
ofType: @"pem"];
keyPath = [[NSBundle bundleForClass: [self class]]
pathForResource: @"testKey"
pathForResource: @"key"
ofType: @"pem"];
secure = [NSDictionary dictionaryWithObjectsAndKeys:
certPath, @"CertificateFile",
@ -411,11 +410,9 @@
}
}
if (_debug)
{
NSLog(@"Starting web server with address %@, port %@ %@",
_address, _port, secure ? @" with TLS" : @"");
}
NSDebugLog(@"Starting web server with address %@, port %@ %@",
_address, _port, secure ? @" with TLS" : @"");
status = [_server setAddress: _address port: _port secure: secure];
if (!status)
{
@ -430,10 +427,7 @@
if (nil != _server && [_server port] != nil)
{
[_server stop]; // shut down the server
if (YES == _debug)
{
NSLog(@"%@: stopped SimpleWebServer %@", self, _server);
}
NSDebugLog(@"%@: stopped SimpleWebServer %@", self, _server);
DESTROY(_server);
}
}
@ -447,10 +441,8 @@
[self _startHTTPServer: extra];
[_lock unlockWithCondition: STARTED];
if (YES == _debug)
{
NSLog(@"%@: enter into runloop in detached thread %@", self, [NSThread currentThread]);
}
NSDebugLog(@"%@: enter into runloop in detached thread %@", self, [NSThread currentThread]);
while(!_threadToQuit && duration < MAXDURATION)
{
@ -459,10 +451,7 @@
duration += TIMING;
}
if (YES == _debug)
{
NSLog(@"%@: exit from runloop in detached thread %@", self, [NSThread currentThread]);
}
NSDebugLog(@"%@: exit from runloop in detached thread %@", self, [NSThread currentThread]);
if (duration >= MAXDURATION &&
nil != _delegate &&

View file

@ -0,0 +1,24 @@
-----BEGIN CERTIFICATE-----
MIID9TCCAt2gAwIBAgIUTHRdikwMl8AkgbR96OlOvhfBajcwDQYJKoZIhvcNAQEL
BQAwgYgxCzAJBgNVBAYTAkdTMQ0wCwYDVQQIDARCQVNFMQ4wDAYDVQQHDAVURVNU
UzEQMA4GA1UECgwHR05Vc3RlcDEQMA4GA1UECwwHRGV2VGVhbTESMBAGA1UEAwwJ
bG9jYWxob3N0MSIwIAYJKoZIhvcNAQkBFhNnbnVzdGVwLWRldkBnbnUub3JnMCAX
DTI0MDMwNjE4MjUyM1oYDzIxMjQwMjExMTgyNTIzWjCBiDELMAkGA1UEBhMCR1Mx
DTALBgNVBAgMBEJBU0UxDjAMBgNVBAcMBVRFU1RTMRAwDgYDVQQKDAdHTlVzdGVw
MRAwDgYDVQQLDAdEZXZUZWFtMRIwEAYDVQQDDAlsb2NhbGhvc3QxIjAgBgkqhkiG
9w0BCQEWE2dudXN0ZXAtZGV2QGdudS5vcmcwggEiMA0GCSqGSIb3DQEBAQUAA4IB
DwAwggEKAoIBAQCaXAhoNy8LiYCZjKUDM6YBuVTmOEgFR8/kzQycxNOyw6GHR/xH
5sdGTVUWju43HIKYjGfDGVPYMIv7uc0OLKjWYPG2pth0SB0BW7Rf24Mslt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DF+6sKw+ggUcMgLM0z6HcYE975T/c4KNqb11Mu2+38CQvY
pvlzOgQxzbjq59reek3gRtj3u0UCaXzTnIvgLl+sC0/l4f4qEVwcEcnMiJyUAIsR
VR7Mw874clbF8TK8tLlcgCipmDmWxFlrgxOPs1ikcObvTOR/oxacUQ==
-----END RSA PRIVATE KEY-----

View file

@ -1,6 +1,6 @@
/**
*
* Author: Sergei Golovin <Golovin.SV@gmail.com>
* Author: Sergei Golovin <svgdev@mail.ru>
*
* Runs two TestWebServer instances to check how the class TestWebServer
* behaves. Visit http://localhost:1234/index to see all supported resources.
@ -84,11 +84,10 @@ int main(int argc, char **argv, char **env)
[[NSRunLoop currentRunLoop]
runUntilDate: [NSDate dateWithTimeIntervalSinceNow: TIMING]];
}
// [server1 stop];
// DESTROY(server1);
// [server2 stop];
// DESTROY(server2);
[server1 stop];
DESTROY(server1);
[server2 stop];
DESTROY(server2);
}
else
{

View file

@ -57,18 +57,6 @@ int main(int argc, char **argv, char **env)
NSURLRequest *req;
Delegate *del;
/* The following test cases depend on the GSInetServerStream
* class which is completely broken on Windows.
*
* See: https://github.com/gnustep/libs-base/issues/266
*
* We will mark the test cases as hopeful on Windows.
*/
#if defined(_WIN32)
NSLog(@"Marking local web server tests as hopeful because GSInetServerStream is broken on Windows");
testHopeful = YES;
#endif
duration = 0.0;
timing = 0.1;
urlString = @"http://localhost:19750";
@ -87,6 +75,10 @@ int main(int argc, char **argv, char **env)
"connection to dead(not-listening) HTTP service");
[del reset];
#if !defined(HAVE_GNUTLS)
testHopeful = YES;
#endif
duration = 0.0;
urlString = @"https://localhost:19750";
req = [NSURLRequest requestWithURL: [NSURL URLWithString: urlString]];
@ -103,11 +95,11 @@ int main(int argc, char **argv, char **env)
"connection to dead(not-listening) HTTPS service");
[del reset];
[arp release]; arp = nil;
#if defined(_WIN32)
testHopeful = NO;
#if !defined(HAVE_GNUTLS)
testHopeful = NO;
#endif
[arp release]; arp = nil;
return 0;
}

View file

@ -28,20 +28,8 @@ int main(int argc, char **argv, char **env)
NSDictionary *refs;
TestWebServer *server;
NSURLConnectionTest *testCase;
BOOL debug = NO;
BOOL debug = GSDebugSet(@"dflt");
/* The following test cases depend on the GSInetServerStream
* class which is completely broken on Windows.
*
* See: https://github.com/gnustep/libs-base/issues/266
*
* We will mark the test cases as hopeful on Windows.
*/
#if defined(_WIN32)
NSLog(@"Marking local web server tests as hopeful because GSInetServerStream is broken on Windows");
testHopeful = YES;
#endif
testClass = [bundle principalClass]; // NSURLConnectionTest
// create a shared TestWebServer instance for performance
@ -142,10 +130,6 @@ int main(int argc, char **argv, char **env)
format: @"can't load bundle TestConnection"];
}
#if defined(_WIN32)
testHopeful = NO;
#endif
DESTROY(arp);
return 0;

View file

@ -10,24 +10,17 @@
int main(int argc, char **argv, char **env)
{
#if !defined(HAVE_GNUTLS)
testHopeful = YES;
#endif
CREATE_AUTORELEASE_POOL(arp);
NSFileManager *fm;
NSBundle *bundle;
BOOL loaded;
NSString *helperPath;
/* The following test cases depend on the GSInetServerStream
* class which is completely broken on Windows.
*
* See: https://github.com/gnustep/libs-base/issues/266
*
* We will mark the test cases as hopeful on Windows.
*/
#if defined(_WIN32)
NSLog(@"Marking local web server tests as hopeful because GSInetServerStream is broken on Windows");
testHopeful = YES;
#endif
// load the test suite's classes
fm = [NSFileManager defaultManager];
helperPath = [[fm currentDirectoryPath]
@ -42,7 +35,7 @@ int main(int argc, char **argv, char **env)
NSDictionary *refs;
TestWebServer *server;
NSURLConnectionTest *testCase;
BOOL debug = NO;
BOOL debug = GSDebugSet(@"dflt");
testClass = [bundle principalClass]; // NSURLConnectionTest
@ -53,11 +46,11 @@ int main(int argc, char **argv, char **env)
// create a shared TestWebServer instance for performance
server = [[[testClass testWebServerClass] alloc]
initWithAddress: @"localhost"
port: @"1234"
port: @"1233"
mode: NO
extra: d];
[server setDebug: debug];
[server start: d]; // localhost:1234 HTTPS
[server start: d]; // localhost:1233 HTTPS
/*
* Simple GET via HTTPS with empty response's body and
@ -71,7 +64,7 @@ int main(int argc, char **argv, char **env)
nil];
[testCase setUpTest: d];
[testCase startTest: d];
PASS([testCase isSuccess], "HTTPS... GET https://localhost:1234/");
PASS([testCase isSuccess], "HTTPS... GET https://localhost:1233/");
[testCase tearDownTest: d];
DESTROY(testCase);
@ -90,7 +83,7 @@ int main(int argc, char **argv, char **env)
nil];
[testCase setUpTest: d];
[testCase startTest: d];
PASS([testCase isSuccess], "HTTPS... response 400 .... GET https://localhost:1234/400");
PASS([testCase isSuccess], "HTTPS... response 400 .... GET https://localhost:1233/400");
[testCase tearDownTest: d];
DESTROY(testCase);
@ -111,7 +104,7 @@ int main(int argc, char **argv, char **env)
nil];
[testCase setUpTest: d];
[testCase startTest: d];
PASS([testCase isSuccess], "HTTPS... payload... response 400 .... POST https://localhost:1234/400");
PASS([testCase isSuccess], "HTTPS... payload... response 400 .... POST https://localhost:1233/400");
[testCase tearDownTest: d];
DESTROY(testCase);
@ -133,11 +126,12 @@ int main(int argc, char **argv, char **env)
@"/301", @"Path", // request the handler responding with a redirect
@"/", @"RedirectPath", // the URL's path of redirecting
@"YES", @"IsAuxilliary", // start an auxilliary TestWebServer instance
@"1236", @"AuxPort", // the port of the auxilliary instance
refs, @"ReferenceFlags", // the expected reference set difference
nil];
[testCase setUpTest: d];
[testCase startTest: d];
PASS([testCase isSuccess], "HTTPS... redirecting... GET https://localhost:1234/301");
PASS([testCase isSuccess], "HTTPS... redirecting... GET https://localhost:1233/301");
[testCase tearDownTest: d];
DESTROY(testCase);
@ -154,9 +148,9 @@ int main(int argc, char **argv, char **env)
DESTROY(arp);
#if defined(_WIN32)
testHopeful = NO;
#if !defined(HAVE_GNUTLS)
testHopeful = NO;
#endif
return 0;
}

View file

@ -14,18 +14,6 @@ int main(int argc, char **argv, char **env)
BOOL loaded;
NSString *helperPath;
/* The following test cases depend on the GSInetServerStream
* class which is completely broken on Windows.
*
* See: https://github.com/gnustep/libs-base/issues/266
*
* We will mark the test cases as hopeful on Windows.
*/
#if defined(_WIN32)
NSLog(@"Marking local web server tests as hopeful because GSInetServerStream is broken on Windows");
testHopeful = YES;
#endif
// load the test suite's classes
fm = [NSFileManager defaultManager];
helperPath = [[fm currentDirectoryPath]
@ -40,14 +28,18 @@ int main(int argc, char **argv, char **env)
NSDictionary *refs;
TestWebServer *server;
NSURLConnectionTest *testCase;
BOOL debug = NO;
BOOL debug = GSDebugSet(@"dflt");
testClass = [bundle principalClass]; // NSURLConnectionTest
// create a shared TestWebServer instance for performance
server = [[testClass testWebServerClass] new];
server = [[[testClass testWebServerClass] alloc]
initWithAddress: @"localhost"
port: @"1232"
mode: NO
extra: nil];
[server setDebug: debug];
[server start: nil]; // localhost:1234 HTTP
[server start: nil]; // localhost:1232 HTTP
/*
* Simple GET via HTTP without authorization with empty response's body and
@ -71,7 +63,7 @@ int main(int argc, char **argv, char **env)
nil];
[testCase setUpTest: d];
[testCase startTest: d];
PASS([testCase isSuccess], "no auth... GET http://localhost:1234/withoutauth");
PASS([testCase isSuccess], "no auth... GET http://localhost:1232/withoutauth");
[testCase tearDownTest: d];
DESTROY(testCase);
@ -99,7 +91,7 @@ int main(int argc, char **argv, char **env)
nil];
[testCase setUpTest: d];
[testCase startTest: d];
PASS([testCase isSuccess], "no auth... response 400 .... GET http://localhost:1234/400/withoutauth");
PASS([testCase isSuccess], "no auth... response 400 .... GET http://localhost:1232/400/withoutauth");
[testCase tearDownTest: d];
DESTROY(testCase);
@ -129,7 +121,7 @@ int main(int argc, char **argv, char **env)
nil];
[testCase setUpTest: d];
[testCase startTest: d];
PASS([testCase isSuccess], "no auth... payload... response 400 .... POST http://localhost:1234/400/withoutauth");
PASS([testCase isSuccess], "no auth... payload... response 400 .... POST http://localhost:1232/400/withoutauth");
[testCase tearDownTest: d];
DESTROY(testCase);
@ -156,11 +148,12 @@ int main(int argc, char **argv, char **env)
@"/301/withoutauth", @"Path", // request the handler responding with a redirect
@"/withoutauth", @"RedirectPath", // the URL's path of redirecting
@"YES", @"IsAuxilliary", // start an auxilliary TestWebServer instance
@"1237", @"AuxPort", // the port of the auxilliary instance
refs, @"ReferenceFlags", // the expected reference set difference
nil];
[testCase setUpTest: d];
[testCase startTest: d];
PASS([testCase isSuccess], "no auth... redirecting... GET http://localhost:1234/301/withoutauth");
PASS([testCase isSuccess], "no auth... redirecting... GET http://localhost:1232/301/withoutauth");
[testCase tearDownTest: d];
DESTROY(testCase);
@ -175,10 +168,6 @@ int main(int argc, char **argv, char **env)
format: @"can't load bundle TestConnection"];
}
#if defined(_WIN32)
testHopeful = NO;
#endif
DESTROY(arp);
return 0;

View file

@ -9,24 +9,17 @@
int main(int argc, char **argv, char **env)
{
#if !defined(HAVE_GNUTLS)
testHopeful = YES;
#endif
CREATE_AUTORELEASE_POOL(arp);
NSFileManager *fm;
NSBundle *bundle;
BOOL loaded;
NSString *helperPath;
/* The following test cases depend on the GSInetServerStream
* class which is completely broken on Windows.
*
* See: https://github.com/gnustep/libs-base/issues/266
*
* We will mark the test cases as hopeful on Windows.
*/
#if defined(_WIN32)
NSLog(@"Marking local web server tests as hopeful because GSInetServerStream is broken on Windows");
testHopeful = YES;
#endif
// load the test suite's classes
fm = [NSFileManager defaultManager];
helperPath = [[fm currentDirectoryPath]
@ -41,7 +34,7 @@ int main(int argc, char **argv, char **env)
NSDictionary *refs;
TestWebServer *server;
NSURLConnectionTest *testCase;
BOOL debug = NO;
BOOL debug = GSDebugSet(@"dflt");
testClass = [bundle principalClass]; // NSURLConnectionTest
@ -52,11 +45,11 @@ int main(int argc, char **argv, char **env)
// create a shared TestWebServer instance for performance
server = [[[testClass testWebServerClass] alloc]
initWithAddress: @"localhost"
port: @"1234"
port: @"1231"
mode: NO
extra: d];
[server setDebug: debug];
[server start: d]; // localhost:1234 HTTPS
[server start: d]; // localhost:1231 HTTPS
/* Simple GET via HTTPS without authorization with empty response's
* body and the response's status code 204 (by default)
@ -82,7 +75,7 @@ int main(int argc, char **argv, char **env)
[testCase setUpTest: d];
[testCase startTest: d];
PASS([testCase isSuccess],
"HTTPS... no auth...GET https://localhost:1234/withoutauth");
"HTTPS... no auth...GET https://localhost:1231/withoutauth");
[testCase tearDownTest: d];
DESTROY(testCase);
@ -111,7 +104,7 @@ int main(int argc, char **argv, char **env)
nil];
[testCase setUpTest: d];
[testCase startTest: d];
PASS([testCase isSuccess], "HTTPS... no auth... response 400... GET https://localhost:1234/400/withoutauth");
PASS([testCase isSuccess], "HTTPS... no auth... response 400... GET https://localhost:1231/400/withoutauth");
[testCase tearDownTest: d];
DESTROY(testCase);
@ -143,7 +136,7 @@ int main(int argc, char **argv, char **env)
nil];
[testCase setUpTest: d];
[testCase startTest: d];
PASS([testCase isSuccess], "HTTPS... no auth... payload... response 400 .... POST https://localhost:1234/400/withoutauth");
PASS([testCase isSuccess], "HTTPS... no auth... payload... response 400 .... POST https://localhost:1231/400/withoutauth");
[testCase tearDownTest: d];
DESTROY(testCase);
@ -173,11 +166,12 @@ int main(int argc, char **argv, char **env)
@"/301/withoutauth", @"Path", // request a redirect
@"/withoutauth", @"RedirectPath", // the URL's path of redirecting
@"YES", @"IsAuxilliary", // start an auxilliary TestWebServer instance
@"1238", @"AuxPort", // the port of the auxilliary instance
refs, @"ReferenceFlags", // the expected reference set difference
nil];
[testCase setUpTest: d];
[testCase startTest: d];
PASS([testCase isSuccess], "HTTPS... no auth... redirecting... GET https://localhost:1234/301/withoutauth");
PASS([testCase isSuccess], "HTTPS... no auth... redirecting... GET https://localhost:1231/301/withoutauth");
[testCase tearDownTest: d];
DESTROY(testCase);
@ -192,11 +186,11 @@ int main(int argc, char **argv, char **env)
format: @"can't load bundle TestConnection"];
}
#if defined(_WIN32)
testHopeful = NO;
#endif
DESTROY(arp);
#if !defined(HAVE_GNUTLS)
testHopeful = NO;
#endif
return 0;
}

View file

@ -14,18 +14,6 @@ int main(int argc, char **argv, char **env)
BOOL loaded;
NSString *helperPath;
/* The following test cases depend on the GSInetServerStream
* class which is completely broken on Windows.
*
* See: https://github.com/gnustep/libs-base/issues/266
*
* We will mark the test cases as hopeful on Windows.
*/
#if defined(_WIN32)
NSLog(@"Marking local web server tests as hopeful because GSInetServerStream is broken on Windows");
testHopeful = YES;
#endif
// load the test suite's classes
fm = [NSFileManager defaultManager];
helperPath = [[fm currentDirectoryPath]
@ -37,7 +25,7 @@ int main(int argc, char **argv, char **env)
{
Class testClass;
TestWebServer *server;
BOOL debug = NO;
BOOL debug = GSDebugSet(@"dflt");
NSURL *url;
NSError *error = nil;
NSURLRequest *request;
@ -49,16 +37,20 @@ int main(int argc, char **argv, char **env)
// create a shared TestWebServer instance for performance
// by default it requires the basic authentication with the pair
// login:password
server = [[testClass testWebServerClass] new];
server = [[[testClass testWebServerClass] alloc]
initWithAddress: @"localhost"
port: @"1230"
mode: NO
extra: nil];
[server setDebug: debug];
[server start: nil]; // localhost:1234 HTTP
[server start: nil]; // localhost:1230 HTTP
/*
* Simple GET via HTTP with some response's body and
* the response's status code 200
*/
url = [NSURL
URLWithString: @"http://login:password@localhost:1234/index"];
URLWithString: @"http://login:password@localhost:1230/index"];
request = [NSURLRequest requestWithURL: url];
data = [NSURLConnection sendSynchronousRequest: request
returningResponse: &response
@ -77,10 +69,6 @@ int main(int argc, char **argv, char **env)
format: @"can't load bundle TestConnection"];
}
#if defined(_WIN32)
testHopeful = NO;
#endif
DESTROY(arp);
return 0;

View file

@ -8,24 +8,17 @@
int main(int argc, char **argv, char **env)
{
#if !defined(HAVE_GNUTLS)
testHopeful = YES;
#endif
CREATE_AUTORELEASE_POOL(arp);
NSFileManager *fm;
NSBundle *bundle;
BOOL loaded;
NSString *helperPath;
/* The following test cases depend on the GSInetServerStream
* class which is completely broken on Windows.
*
* See: https://github.com/gnustep/libs-base/issues/266
*
* We will mark the test cases as hopeful on Windows.
*/
#if defined(_WIN32)
NSLog(@"Marking local web server tests as hopeful because GSInetServerStream is broken on Windows");
testHopeful = YES;
#endif
// load the test suite's classes
fm = [NSFileManager defaultManager];
helperPath = [[fm currentDirectoryPath]
@ -41,7 +34,7 @@ int main(int argc, char **argv, char **env)
TestWebServer *server;
NSMutableString *payload;
NSURLConnectionTest *testCase;
BOOL debug = NO;
BOOL debug = GSDebugSet(@"dflt");
int i;
testClass = [bundle principalClass]; // NSURLConnectionTest
@ -53,11 +46,11 @@ int main(int argc, char **argv, char **env)
// create a shared TestWebServer instance for performance
server = [[[testClass testWebServerClass] alloc]
initWithAddress: @"localhost"
port: @"1234"
port: @"1229"
mode: NO
extra: d];
[server setDebug: debug];
[server start: d]; // localhost:1234 HTTPS
[server start: d]; // localhost:1229 HTTPS
/* Simple POST via HTTPS with the response's status code 400 and
* non-empty response's body
@ -94,7 +87,7 @@ int main(int argc, char **argv, char **env)
nil];
[testCase setUpTest: d];
[testCase startTest: d];
PASS([testCase isSuccess], "HTTPS... big payload... response 400 .... POST https://localhost:1234/400/withoutauth");
PASS([testCase isSuccess], "HTTPS... big payload... response 400 .... POST https://localhost:1229/400/withoutauth");
[testCase tearDownTest: d];
DESTROY(testCase);
@ -109,11 +102,11 @@ int main(int argc, char **argv, char **env)
format: @"can't load bundle TestConnection"];
}
#if defined(_WIN32)
testHopeful = NO;
#endif
DESTROY(arp);
#if !defined(HAVE_GNUTLS)
testHopeful = NO;
#endif
return 0;
}