Hi Richard,

I would like to know if you would accept something like the attached
patch to the test suite. I did go the Python route but can switch to a
WebServer based helper if needed.

The code doesn't try to locate the python executable, it will just
return if python is not in the PATH. If that's a problem on OSX I can
and will change that as the purpose of all this is to compare the two
implementations.

Let me know if you find this useful and what are the results on OSX if
by chance the tests run.

Thanks,
Philippe
Index: base/NSURLConnection/Helpers/IGNORE
===================================================================
Index: base/NSURLConnection/Helpers/server.py
===================================================================
--- base/NSURLConnection/Helpers/server.py	(révision 0)
+++ base/NSURLConnection/Helpers/server.py	(révision 0)
@@ -0,0 +1,23 @@
+import BaseHTTPServer
+
+HOST_NAME = '127.0.0.1'
+PORT_NUMBER = 54321
+
+class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
+    def do_HEAD(s):
+        s.send_response(200)
+        s.send_header("Content-type", "text/html")
+        s.end_headers()
+
+    def do_GET(s):
+        s.send_response(200)
+        s.send_header("Content-type", "text/html")
+        s.end_headers()
+        s.wfile.write("<html><head><title>Title goes here.</title></head>")
+        s.wfile.write("<body><p>This is a test.</p></body></html>")
+
+if __name__ == '__main__':
+    server_class = BaseHTTPServer.HTTPServer
+    httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
+    httpd.serve_forever()
+    httpd.server_close()
Index: base/NSURLConnection/test00.m
===================================================================
--- base/NSURLConnection/test00.m	(révision 0)
+++ base/NSURLConnection/test00.m	(révision 0)
@@ -0,0 +1,169 @@
+#import <Foundation/Foundation.h>
+#import "Testing.h"
+#import "ObjectTesting.h"
+
+@interface TestClass : NSObject
+{
+  BOOL done;
+  NSMutableData *data;
+  NSURLResponse *response;
+  int status;
+}
+- (int) runTests;
+@end
+
+@implementation TestClass
+- (void)dealloc
+{
+  [response release];
+  [data release];
+  [super dealloc];
+}
+
+- (id) init
+{
+  data = [[NSMutableData alloc] initWithCapacity:512];
+  return self;
+}
+
+- (void) runTest
+{
+  NSRunLoop *loop = [NSRunLoop currentRunLoop];
+  int count;
+
+  DESTROY(response);
+  done = NO;
+  count = 0;
+  status = 0;
+  while (!done && count < 10) {
+    [loop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.2]];
+    count++;
+  }
+}
+
+- (int) runTests
+{
+  NSURL *url;
+  NSMutableURLRequest *request;
+  NSURLConnection *connection;
+
+  url = [NSURL URLWithString: @"http://localhost:54321/";];
+  if (!url) {
+    NSLog(@"Couldn't build an NSURL from http://localhost:54321/";);
+    return 1;
+  }
+  request = [NSMutableURLRequest requestWithURL:url];
+  if (!request) {
+    NSLog(@"Couldn't build an NSMutableURLRequest from NSURL http://localhost:54321/";);
+    return 1;
+  }
+
+  connection = [NSURLConnection connectionWithRequest:request delegate:self];
+  pass(connection != nil,
+       "NSURLConnection connectionWithRequest:delegate: with a valid GET HTTP "
+       "request and a delegate returns an instance");
+  [self runTest];
+  pass(status == 200, "HTTP GET gets a 200 response");
+  pass([data length] > 0, "HTTP GET returns data");
+  passeq([response class], [NSHTTPURLResponse class], "HTTP GET returns a NSHTTPURLResponse");
+
+  [request setHTTPMethod:@"DELETE"];
+  connection = [NSURLConnection connectionWithRequest:request delegate:self];
+  pass(connection != nil,
+       "NSURLConnection connectionWithRequest:delegate: with a valid DELETE HTTP "
+       "request and a delegate returns an instance");
+  [self runTest];
+  pass(response != nil, "HTTP DELETE gets a NSURLResponse");
+  pass(status == 501, "HTTP DELETE gets a 501 response as the server does not handle this method");
+
+  [request setHTTPMethod:@"WRONGMETHOD"];
+  connection = [NSURLConnection connectionWithRequest:request delegate:self];
+  pass(connection != nil,
+       "NSURLConnection connectionWithRequest:delegate: "
+       "with a invalid WRONGMETHOD HTTP request and a delegate returns an instance");
+  [self runTest];
+  pass(response == nil, "HTTP WRONGMETHOD doesn't get a NSURLResponse");
+  pass(status == 0, 
+       "HTTP WRONGMETHOD fails without an HTTP error code as the NSURLConnection doesn't allow it");
+
+  return 0;
+}
+
+- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
+{
+  NSLog(@"%s : %@ %d", __PRETTY_FUNCTION__, [error domain], [error code]);
+}
+
+- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
+{
+  [[challenge sender] cancelAuthenticationChallenge:challenge];
+}
+
+- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)newData
+{
+  NSLog(@"%s : %d octets", __PRETTY_FUNCTION__, [newData length]);
+  [data appendData:newData];
+}
+
+- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse
+{
+  NSLog(@"%s : %@", __PRETTY_FUNCTION__, [aResponse description]);
+  ASSIGN(response, aResponse);
+  if ([aResponse respondsToSelector:@selector(statusCode)])
+    status = [(NSHTTPURLResponse *)aResponse statusCode];
+  [data setLength:0];
+}
+
+- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)newRequest redirectResponse:(NSURLResponse *)response
+{
+  NSLog(@"%s", __PRETTY_FUNCTION__);
+  return newRequest;
+}
+
+- (void)connectionDidFinishLoading:(NSURLConnection *)connection
+{
+  NSLog(@"%s", __PRETTY_FUNCTION__);
+  done = YES;
+}
+@end
+
+int main()
+{
+  NSAutoreleasePool *arp = [NSAutoreleasePool new];
+  NSTask *t;
+  NSString *server;
+  TestClass *test;
+  int status = 0;
+  
+  server = [[NSFileManager defaultManager] currentDirectoryPath];
+  server = [server stringByAppendingPathComponent: @"Helpers"];
+  server = [server stringByAppendingPathComponent: @"server.py"];
+
+  if ([[NSFileManager defaultManager] fileExistsAtPath:server]) {
+    NS_DURING
+      {
+	t = [NSTask launchedTaskWithLaunchPath:@"python" arguments:[NSArray arrayWithObject:server]];
+	if (t != nil) {
+	  /* Let the server start up */
+	  [NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1]];
+	  test = [TestClass new];
+	  status = [test runTests];
+	  [test release];
+	  [t terminate];
+	  [t waitUntilExit];
+	} else {
+	  NSLog(@"Could run Helpers/server.py script");
+	}
+      }
+    NS_HANDLER
+      {
+	NSLog(@"Exception while trying to run %@", server);
+      }
+    NS_ENDHANDLER
+      
+  } else {
+    NSLog(@"Can't find helper at path %@", server);
+  }
+  [arp release]; arp = nil;
+  return status;
+}
_______________________________________________
Gnustep-dev mailing list
Gnustep-dev@gnu.org
http://lists.gnu.org/mailman/listinfo/gnustep-dev

Reply via email to