/**
 *  TODO: remove it... it is only for convenience
 *
 *  Tests POST via SSL with two-stage authorization.
 *  The gnustep base has currently a bug in this case.
 */

#import <Foundation/Foundation.h>
#import "Helpers/NSURLConnectionTest.h"
#import "Helpers/TestWebServer.h"
#import <Testing.h>

int main(int argc, char **argv, char **env)
{
  CREATE_AUTORELEASE_POOL(arp);
  NSFileManager *fm;
  NSBundle *bundle;
  BOOL loaded;
  NSString *helperPath;

  // TODO: i didn't find out how to link a test with a library from the directory Helpers
  //       using TestFramework
  //       so i decided to use NSBundle

  // load the test suite's classes
  fm = [NSFileManager defaultManager];
  helperPath = [[fm currentDirectoryPath]
		 stringByAppendingString: @"/Helpers/TestConnection.bundle"];
  bundle = [NSBundle bundleWithPath: helperPath];
  loaded = [bundle load];

  if(loaded)
    {
      NSDictionary *d;
      Class testClass;
      //      NSDictionary *refs;
      TestWebServer *server;
      NSURLConnectionTest *testCase;
      BOOL debug = YES;

      testClass = [bundle principalClass]; // NSURLConnectionTest
      // the extra dictionary commanding to use HTTPS
      d = [NSDictionary dictionaryWithObjectsAndKeys:
			  @"HTTPS", @"Protocol",
			nil];
      // create a shared TestWebServer instance for performance
      server = [[[testClass testWebServerClass] alloc] initWithAddress: @"localhost"
								  port: @"54321"
								  mode: NO
								 extra: d];
      [server start: d]; // 127.0.0.1:54321 HTTPS

      /*
       *  Simple POST via HTTPS with the response's status code 400 and
       *  non-empty response's body
       */
      testCase = [testClass new];
      [testCase setDebug: debug];
      // the extra dictionary with test case's parameters
      d = [NSDictionary dictionaryWithObjectsAndKeys:
			  server, @"Instance", // we use the shared TestWebServer instance
			@"400", @"Path",       // request the handler responding with 400
			@"400", @"StatusCode", // the expected status code
			@"You have issued a request with invalid data", @"Content", // the expected response's body
			@"Some payload", @"Payload", // the custom payload
			@"POST", @"Method", // use POST
			nil];
      [testCase setUpTest: d];
      [testCase startTest: d];
      PASS([testCase isSuccess], "payload... response 400 .... POST https://localhost:54321/400");
      [testCase tearDownTest: d];
      DESTROY(testCase);

      // cleaning
      [server stop];
      DESTROY(server);
    }
  else
    {
      // no classes no tests
      [NSException raise: NSInternalInconsistencyException
		  format: @"can't load bundle TestConnection"];
    }


  DESTROY(arp);

  return 0;
}
