1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
/** -*- objc -*-
*
* 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
* and NSURLConnection's callbacks. As TestCase's child it has two flag sets, the actual
* one and the reference one (See TestCase.h). It sets/unsets flags of the actual set on
* the execution path at the 'checkpoints' listed below as macros.
*
* The method -[isSuccess] is called when the NSURLConnection finishes (fails). It makes
* a comparison between the two flag sets. The result signifies a success/failure of the
* test.
*
* The test case which the NSURLConnectionTest implements by default is connecting
* 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:
* --------------------------------------------------------------------------
* #import "NSURLConnectionTest.h"
* #import "Testing.h"
*
* NSURLConnectionTest *testCase = [NSURLConnectionTest new];
* // the extra dictionary with test cases's parameters
* NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:
* @"/somepath", @"Path",
* @"POST", @"Method",
* @"https", @"Protocol",
* nil];
* [testCase setUpTest: d];
* [testCase startTest: d];
* PASS([testCase isSuccess], "a diagnostic message about the test");
* [testCase tearDownTest: d];
* DESTROY(testCase);
* --------------------------------------------------------------------------
*
* The method -[setUpTest:] recognises the following variable parts (supplied as key-value
* pairs):
*
* 'Instance'
* 'AuxInstance' - holds an already running main/auxilliary TestWebServer instance.
* If nil the class will run one main instance of TestWebServer
* and no auxilliary one. The key 'IsAuxilliary' with the value 'YES'
* should be supplied to run an own auxilliary instance.
* Useful to run several tests consequently. The test won't stop
* on its own the supplied instance(s). It leaves the calling code
* to make the decision. The auxilliary instance 'AuxInstance' is
* used in tests on redirecting where two web servers are needed.
* Default: nil
* 'IsAuxilliary' - whether the NSURLConnectionTest should run it's own auxilliary
* TestWebServer instance. You must supply YES as a value to run it.
* It will be run with the same parameters (address/protocol/detached)
* as the main one except of the port which is assigned from the value
* of the key 'AuxPort'.
* Default: NO
* 'IsDetached' - whether to run the own(and auxilliary) instance in the detached mode
* (the instance will be run within a detached thread).
* Default: NO
* 'Address' - the address of the remote side.
* Default: localhost
* 'Port' - the port of the remote side.
* Default: 1234
* 'AuxPort' - the port of the auxilliary remote side (where the connection
* to be redirected).
* Default: 1235
* 'Protocol' - the network protocol (supports currently http, https).
* Default: http
* 'Path' - the path of the URL.
* Default: '/'
* 'RedirectPath' - the path where request should be redirected in corresponding tests.
* Default: '/'
* 'StatusCode' - the status code expected from the remote side if the test
* is successful.
* Default: 204
* 'RedirectCode' - the status code expected from the remote side on the connection's first
* stage in a test with redirect.
* Default: 301
* 'Method' - the request's method.
* Default: GET
* 'Payload' - the request's payload. It can be of NSString or of NSData class.
* The class produces NSData from NSString using NSUTF8StringEncoding.
* Default: nil
* 'Content' - the expected content. It can be of NSString or of NSData class.
* The class produces NSData from NSString using NSUTF8StringEncoding.
* Default: nil
* 'ReferenceFlags' - the dictionary to correct the guessed reference flag set.
* Default: nil
* The class tries to infer the reference flag set from
* the test request. If that guessed (default) set is wrong then this
* dictionary allows to correct it.
* The class recognises the following keys:
*
* 'SENTREQUEST'
* 'AUTHORIZED'
* 'NOTAUTHORIZED'
* 'GOTUNAUTHORIZED'
* 'GOTREQUEST'
* 'SENTRESPONSE'
* 'GOTRESPONSE'
* 'GOTCONTENT'
* 'GOTFINISH'
* 'GOTFAIL'
* 'GOTREDIRECT'
*
* the 'YES' as a value means the corresponding reference flag
* must be set, otherwise 'NO' means it must not be set.
*
* The NSURLConnectionTest can raise it's verbosity level by the method call -[setDebug: YES].
* In this case whole connection session will be showed in the log.
*
*/
#import "TestWebServer.h"
#import "TestCase.h"
/* the test's checkpoint list */
/* the request has been sent by the client and reaches the server */
#define SENTREQUEST 1
/* the client's request has been authorized */
#define AUTHORIZED 2
/* the client's request hasn't been authorized */
#define NOTAUTHORIZED 4
/* the client has got Unauthorized response */
#define GOTUNAUTHORIZED 8
/* the server has got a right request */
#define GOTREQUEST 16
/* the server is ready to send the response */
#define SENTRESPONSE 32
/* the client has got the response from the server with valid headers/properties */
#define GOTRESPONSE 64
/* the client has got the expected content from the server's response */
#define GOTCONTENT 128
/* the test's execution has reached client's -[connectionDidFinishLoading:] */
#define GOTFINISH 256
/* the test's execution has reached client's -[connection:didFailWithError:] */
#define GOTFAIL 512
/* the test's execution has reached client's -[connection:willSendRequest:redirectResponse:]*/
#define GOTREDIRECT 1024
/* the end of the test's checkpoint list */
@interface NSURLConnectionTest : TestCase <TestWebServerDelegate>
{
/* the main TestWebServer instance */
TestWebServer *_server;
/* tha auxilliary TestWebServer instance needed in tests on redirecting */
TestWebServer *_auxServer;
/* the custom request (made by the instance or supplied externally) */
NSURLRequest *_request;
/* the redirect request (made by the instance) */
NSURLRequest *_redirectRequest;
/* the data accumulator for the received response's content */
NSMutableData *_received;
/* the expected status code */
NSUInteger _expectedStatusCode;
/* the expected redirect status code of the connection's first stage
* in tests with redirect... the resulting (on the second stage)
* expected status code is stored in the ivar _expectedStatusCode */
NSUInteger _redirectStatusCode;
/* the expected response's content */
NSData *_expectedContent;
/* the connection */
NSURLConnection *_conn;
/* to store the error supplied with the -[connection:didFailWithError:] */
NSError *_error;
}
/**
* Returns a TestWebServer-like class used by this test case class. A descendant can
* return more advanced implementation of TestWebServer's functionality.
*/
+ (Class)testWebServerClass;
+ (void)initialize;
- (id)init;
/* See the super's description */
- (void)setUpTest:(id)extra;
- (void)startTest:(id)extra;
- (void)tearDownTest:(id)extra;
/**
* The only difference from the super's one is issuing of a diagnostic message
* if the test is failed.
*/
- (BOOL)isSuccess;
/**
* Issues log messages describing the actual and reference flag sets' states.
*/
- (void)logFlags;
/**
* Returns the error stored by the -[connection:didFailWithError:].
*/
- (NSError *)error;
/* NSURLConnectionDelegate */
- (NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)redirectResponse;
- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
/* end of NSURLConnectionDelegate */
@end /* NSURLConnectionTest */
|