File: test06.m

package info (click to toggle)
gnustep-base 1.31.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,580 kB
  • sloc: objc: 239,446; ansic: 36,519; cpp: 122; sh: 112; makefile: 100; xml: 32
file content (75 lines) | stat: -rw-r--r-- 2,141 bytes parent folder | download | duplicates (2)
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
/**
 *  Tests for HTTP synchronous requests.
 */
#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;
  
  // 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)
    {
      Class testClass;
      TestWebServer *server;
      BOOL debug = GSDebugSet(@"dflt");
      NSURL *url;
      NSError *error = nil;
      NSURLRequest *request;
      NSURLResponse *response = nil;
      NSData *data;

      testClass = [bundle principalClass]; // NSURLConnectionTest

      // create a shared TestWebServer instance for performance
      // by default it requires the basic authentication with the pair
      // login:password
      server = [[[testClass testWebServerClass] alloc]
        initWithAddress: @"localhost"
                   port: @"1230"
                   mode: NO
                  extra: nil];
      [server setDebug: debug];
      [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:1230/index"];
      request = [NSURLRequest requestWithURL: url];
      data = [NSURLConnection sendSynchronousRequest: request
				   returningResponse: &response
					       error: &error];
      PASS(nil != data && [(NSHTTPURLResponse*)response statusCode] == 200,
        "NSURLConnection synchronous load with authentication returns a response");

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

  DESTROY(arp);

  return 0;
}