Merge changes for NSURLSession from EngageHub (formerly Brainstorm).

This commit is contained in:
Richard Frith-Macdonald 2020-11-29 06:57:47 -05:00
parent d015cebbf3
commit 1b7bf26bea
70 changed files with 7980 additions and 192 deletions

View file

@ -13,7 +13,7 @@
* test.
*
* The test case which the NSURLConnectionTest implements by default is connecting
* to the http://127.0.0.1:1234/ whith the HTTP method 'GET'. You can change variable
* to the http://localhost:1234/ whith the HTTP method 'GET'. You can change variable
* parts of process by supplying a custom dictionary to the method -[setUpTest:]
* before the test case is started by a call of the -[startTest:]. The use pattern:
* --------------------------------------------------------------------------
@ -57,7 +57,7 @@
* (the instance will be run within a detached thread).
* Default: NO
* 'Address' - the address of the remote side.
* Default: 127.0.0.1
* Default: localhost
* 'Port' - the port of the remote side.
* Default: 1234
* 'AuxPort' - the port of the auxilliary remote side (where the connection

View file

@ -590,7 +590,7 @@ didReceiveResponse:(NSURLResponse *)response
}
if (nil == address)
{
address = @"127.0.0.1";
address = @"localhost";
}
if (nil == port)
{

View file

@ -6,9 +6,9 @@
@implementation TestCase
- (id)init
- (id) init
{
if((self = [super init]) != nil)
if ((self = [super init]) != nil)
{
[self resetFlags];
[self setReferenceFlags: NORESULTS];
@ -19,7 +19,7 @@
return self;
}
- (void)dealloc
- (void) dealloc
{
[self tearDownTest: _extra];
@ -27,47 +27,47 @@
}
/* TestProgress */
- (void)resetFlags
- (void) resetFlags
{
_flags = NORESULTS;
}
- (void)setFlags:(NSUInteger)mask
- (void) setFlags:(NSUInteger)mask
{
_flags = _flags | mask;
}
- (void)unsetFlags:(NSUInteger)mask
- (void) unsetFlags:(NSUInteger)mask
{
_flags = _flags & ~mask;
}
- (BOOL)isFlagSet:(NSUInteger)mask
- (BOOL) isFlagSet:(NSUInteger)mask
{
return ((_flags & mask) == mask);
}
- (void)resetReferenceFlags
- (void) resetReferenceFlags
{
_refFlags = NORESULTS;
}
- (void)setReferenceFlags:(NSUInteger)mask
- (void) setReferenceFlags:(NSUInteger)mask
{
_refFlags = _refFlags | mask;
}
- (void)unsetReferenceFlags:(NSUInteger)mask
- (void) unsetReferenceFlags:(NSUInteger)mask
{
_refFlags = _refFlags & ~mask;
}
- (BOOL)isReferenceFlagSet:(NSUInteger)mask
- (BOOL) isReferenceFlagSet:(NSUInteger)mask
{
return ((_refFlags & mask) == mask);
}
- (BOOL)isSuccess
- (BOOL) isSuccess
{
if(!_failed && (_flags == _refFlags))
{

View file

@ -13,7 +13,7 @@
* It is designed to call the delegate's callbacks (if implemented) of
* the TestWebServerDelegate protocol for every request made to
* the SimpleWebServer's assigned address on the SimpleWebServer's assigned port
* (by default 127.0.0.1 and 1234 respectively). However it currently doesn't
* (by default localhost and 1234 respectively). However it currently doesn't
* handle any request on it's own. Instead the class uses a collection of
* handlers. Every handler makes a custom response. So the TestWebServer only
* has to dispatch a request to a corresponding handler and then to send back
@ -29,7 +29,7 @@
* NSDictionary *extra = [NSDictionary dictionaryWithObjectsAndKeys:
* @"https", @"Protocol",
* nil];
* TestWebServer *server = [[TestWebServer alloc] initWithAddress: @"127.0.0.1"
* TestWebServer *server = [[TestWebServer alloc] initWithAddress: @"localhost"
* port: @"1234"
* mode: NO
* extra: extra];
@ -72,7 +72,7 @@
* 301 "Redirect to <URL>"
* Returns in the header 'Location' a new <URL> which by default
* has the same protocol, address but the port is incremented by
* 1 (e.g. http://127.0.0.1:1235/ with other parameters set to
* 1 (e.g. http://localhost:1235/ with other parameters set to
* their default values).
* 400 "You have issued a request with invalid data"
* 401 "Invalid login or password"

View file

@ -9,7 +9,7 @@
/**
* Starts the detached thread. It is the entry point of the thread.
*/
*/
- (void)_startDetached:(id)extra;
/**
@ -23,10 +23,10 @@
- (void)_stopHTTPServer;
@end
@end
/* default 'constants' */
#define DEFAULTADDRESS @"127.0.0.1"
#define DEFAULTADDRESS @"localhost"
#define DEFAULTPORT @"1234"
#define DEFAULTMODE NO
#define DEFAULTLOGIN @"login"
@ -45,7 +45,7 @@
@implementation TestWebServer
- (id)init
- (id) init
{
return [self initWithAddress: DEFAULTADDRESS
port: DEFAULTPORT
@ -68,7 +68,7 @@
_password = nil;
_login = nil;
_isSecure = NO;
if ([extra isKindOfClass: [NSDictionary class]])
{
NSDictionary *d = extra;
@ -123,7 +123,7 @@
selector: @selector(_startDetached:)
object: extra];
}
else
else
{
_serverThread = nil;
}
@ -145,36 +145,40 @@
[super dealloc];
}
- (void)start:(id)extra
- (void) start: (id)extra
{
if ([_server port] != nil)
{
if (YES == _debug)
if ([_server port] != nil)
{
if (_debug)
{
NSWarnMLog(@"SimpleWebServer already started");
}
return;
return;
}
if (nil != _serverThread)
{
if (_debug)
NSLog(@"Waiting for startup");
if (![_serverThread isExecuting])
{
NSTimeInterval duration = 0.0;
[_serverThread start];
// wait for the SimpleWebServer is started
while(![_lock tryLockWhenCondition: STARTED] &&
duration < MAXDURATION)
while (![_lock tryLockWhenCondition: STARTED]
&& duration < MAXDURATION)
{
[[NSRunLoop currentRunLoop]
runUntilDate: [NSDate dateWithTimeIntervalSinceNow: TIMING]];
duration += TIMING;
}
[_lock unlock];
if (duration >= MAXDURATION &&
nil != _delegate &&
[_delegate respondsToSelector: @selector(timeoutExceededByHandler:)])
if (duration >= MAXDURATION
&& nil != _delegate
&& [_delegate respondsToSelector:
@selector(timeoutExceededByHandler:)])
{
[_delegate timeoutExceededByHandler: self];
[self stop];
@ -191,7 +195,7 @@
}
}
- (void)stop
- (void) stop
{
if ([_server port] == nil)
{
@ -283,18 +287,18 @@
_isSecure ? @"https" : @"http",
_address,
_port];
[(HandlerIndex *)handler setURLString: urlString];
}
else if ([handler isKindOfClass: [Handler301 class]])
{
// by default http://127.0.0.1:1235/
// by default http://localhost:1235/
NSString *port = [NSString stringWithFormat: @"%u", [_port intValue] + 1]; // the TestWebServer's port + 1
NSString *urlString = [NSString stringWithFormat: @"%@://%@:%@/",
_isSecure ? @"https" : @"http",
_address,
port];
[(Handler301 *)handler setURLString: urlString];
}
}
@ -314,7 +318,7 @@
[handler posthandleRequest: request
response: response
for: self];
return ret;
}
@ -369,7 +373,7 @@
@implementation TestWebServer (Private)
- (void)_startHTTPServer:(id)extra
- (void) _startHTTPServer: (id)extra
{
NSString *certPath;
NSString *keyPath;
@ -381,8 +385,10 @@
[_server setDelegate: self];
if (_isSecure)
{
if ([_address isEqualToString: @"127.0.0.1"] ||
[_address isEqualToString: @"localhost"])
NSHost *h = [NSHost hostWithAddress: _address];
NSHost *l = [NSHost hostWithName: @"localhost"];
if ([h isEqual: l])
{
certPath = [[NSBundle bundleForClass: [self class]]
pathForResource: @"testCert"
@ -397,22 +403,31 @@
}
else
{
[NSException raise: NSInternalInconsistencyException
format: @"The server hasn't run. Address %@ is not localhost (%@)",
_address, l];
// NOTE: generate corresponding certificates for any address differing
// from 127.0.0.1
// from localhost
}
}
status = [_server setAddress: _address port: _port secure: secure]; // run the server
if (!status)
if (_debug)
{
NSLog(@"Starting web server with address %@, port %@ %@",
_address, _port, secure ? @" with TLS" : @"");
}
status = [_server setAddress: _address port: _port secure: secure];
if (!status)
{
[NSException raise: NSInternalInconsistencyException
format: @"The server hasn't run. Perhaps the port %@ is invalid", DEFAULTPORT];
format: @"The server hasn't run. Perhaps the port %@ is invalid",
DEFAULTPORT];
}
}
- (void)_stopHTTPServer
{
if (nil != _server && [_server port] != nil)
if (nil != _server && [_server port] != nil)
{
[_server stop]; // shut down the server
if (YES == _debug)

View file

@ -3,15 +3,15 @@
* Author: Sergei Golovin <Golovin.SV@gmail.com>
*
* Runs two TestWebServer instances to check how the class TestWebServer
* behaves. Visit http://127.0.0.1:1234/index to see all supported resources.
* behaves. Visit http://localhost:1234/index to see all supported resources.
*
* If you visit the main TestWebServer instance with the following command:
*
* wget -O - --user=login --password=password http://127.0.0.1:1234/301 2>&1
* wget -O - --user=login --password=password http://localhost:1234/301 2>&1
*
* you should get a session log like this:
*
* --2014-08-13 12:08:01-- http://127.0.0.1:1234/301
* --2014-08-13 12:08:01-- http://localhost:1234/301
* Resolving 127.0.0.1 (localhost)... 127.0.0.1
* Connecting to 127.0.0.1 (localhost)|127.0.0.1|:1234... connected.
* HTTP request sent, awaiting response... 401 Unauthorized
@ -64,20 +64,20 @@ int main(int argc, char **argv, char **env)
// @"https", @"Protocol",
nil];
server1 = [[[testClass testWebServerClass] alloc]
initWithAddress: @"127.0.0.1"
initWithAddress: @"localhost"
port: @"1234"
mode: NO
extra: d];
[server1 setDebug: debug];
[server1 start: d]; // 127.0.0.1:1234 HTTP
[server1 start: d]; // localhost:1234 HTTP
server2 = [[[testClass testWebServerClass] alloc]
initWithAddress: @"127.0.0.1"
initWithAddress: @"localhost"
port: @"1235"
mode: NO
extra: d];
[server2 setDebug: debug];
[server2 start: d]; // 127.0.0.1:1235 HTTP
[server2 start: d]; // localhost:1235 HTTP
while (YES)
{

View file

@ -59,7 +59,7 @@ int main(int argc, char **argv, char **env)
duration = 0.0;
timing = 0.1;
urlString = @"http://127.0.0.1:19750";
urlString = @"http://localhost:19750";
req = [NSURLRequest requestWithURL: [NSURL URLWithString: urlString]];
del = [[Delegate new] autorelease];
[NSURLConnection connectionWithRequest: req
@ -76,7 +76,7 @@ int main(int argc, char **argv, char **env)
[del reset];
duration = 0.0;
urlString = @"https://127.0.0.1:19750";
urlString = @"https://localhost:19750";
req = [NSURLRequest requestWithURL: [NSURL URLWithString: urlString]];
[NSURLConnection connectionWithRequest: req
delegate: del];

View file

@ -35,7 +35,7 @@ int main(int argc, char **argv, char **env)
// create a shared TestWebServer instance for performance
server = [[testClass testWebServerClass] new];
[server setDebug: debug];
[server start: nil]; // 127.0.0.1:1234 HTTP
[server start: nil]; // localhost:1234 HTTP
/*
* Simple GET via HTTP with empty response's body and

View file

@ -40,12 +40,12 @@ int main(int argc, char **argv, char **env)
nil];
// create a shared TestWebServer instance for performance
server = [[[testClass testWebServerClass] alloc]
initWithAddress: @"127.0.0.1"
initWithAddress: @"localhost"
port: @"1234"
mode: NO
extra: d];
[server setDebug: debug];
[server start: d]; // 127.0.0.1:1234 HTTPS
[server start: d]; // localhost:1234 HTTPS
/*
* Simple GET via HTTPS with empty response's body and
@ -59,7 +59,7 @@ int main(int argc, char **argv, char **env)
nil];
[testCase setUpTest: d];
[testCase startTest: d];
PASS([testCase isSuccess], "HTTPS... GET https://127.0.0.1:1234/");
PASS([testCase isSuccess], "HTTPS... GET https://localhost:1234/");
[testCase tearDownTest: d];
DESTROY(testCase);

View file

@ -35,7 +35,7 @@ int main(int argc, char **argv, char **env)
// create a shared TestWebServer instance for performance
server = [[testClass testWebServerClass] new];
[server setDebug: debug];
[server start: nil]; // 127.0.0.1:1234 HTTP
[server start: nil]; // localhost:1234 HTTP
/*
* Simple GET via HTTP without authorization with empty response's body and

View file

@ -44,7 +44,7 @@ int main(int argc, char **argv, char **env)
mode: NO
extra: d];
[server setDebug: debug];
[server start: d]; // 127.0.0.1:1234 HTTPS
[server start: d]; // localhost:1234 HTTPS
/* Simple GET via HTTPS without authorization with empty response's
* body and the response's status code 204 (by default)

View file

@ -39,7 +39,7 @@ int main(int argc, char **argv, char **env)
// login:password
server = [[testClass testWebServerClass] new];
[server setDebug: debug];
[server start: nil]; // 127.0.0.1:1234 HTTP
[server start: nil]; // localhost:1234 HTTP
/*
* Simple GET via HTTP with some response's body and

View file

@ -45,7 +45,7 @@ int main(int argc, char **argv, char **env)
mode: NO
extra: d];
[server setDebug: debug];
[server start: d]; // 127.0.0.1:1234 HTTPS
[server start: d]; // localhost:1234 HTTPS
/* Simple POST via HTTPS with the response's status code 400 and
* non-empty response's body