1

I'm wondering how to test a synchronous request to assert the behavior of an API client depending on the server response. As it's a good practice to be independent of the server (so the test run fast and don't rely on the internet connection), I'd like to return my own response. I don't know how to do this, since the request is synchronous :

NSURL *url = [self URL];
NSData *postData = [self postData];

NSMutableURLRequest *downloadRequest = [NSMutableURLRequest requestWithURL:url];

[downloadRequest setHTTPMethod:@"POST"];
[downloadRequest setHTTPBody:postData];
[downloadRequest setTimeoutInterval:10.0];  
return downloadRequest;

NSURLResponse *response;
NSError *error = nil;
NSData *urlData = [NSURLConnection sendSynchronousRequest:downloadRequest
                    returningResponse:&response
                    error:&error];

Do you have any suggestion on how to do this ? I don't know if it's possible to override NSURLConnection for example, or if I should change my code, just for testing purposes.

razlebe
  • 7,134
  • 6
  • 42
  • 57
Julien
  • 9,312
  • 10
  • 63
  • 86
  • I guess you mean "crap spewed out by Apple". Seems interesting, I'll have a look. Not sure it will help testing though. – Julien Mar 30 '11 at 07:12

1 Answers1

0

I don't completely understand the first sentence of the question. I can say that you almost certainly want to make it asynchronous. A synchronous request will block the UI and if it takes too long iOS will force-quit your app. Your asynchronous request callbacks can then review the success/failure and then either call a delegate with the same response or a modified one, or post a notification that you've previously installed handlers for, again swapping the response or not as you wish.

Dad
  • 6,388
  • 2
  • 28
  • 34
  • All the API part of the app is ran in a separate thread already, so it's not very useful run a new one. – Julien Mar 30 '11 at 07:25