title: Testing use of NSURLConnection with HTTP response error statuses
body: <p>I'm writing an iPhone application that needs to get some data from a web server. I'm using <code>NSURLConnection</code> to do the HTTP request, which works well, but I'm having trouble unit testing my code in the case where the response has an HTTP error code (like 404 or 500).</p>
  
  <p>I'm using <a href="http://code.google.com/p/google-toolbox-for-mac/" rel="noreferrer">GTM</a> for unit testing and <a href="http://www.mulle-kybernetik.com/software/OCMock/" rel="noreferrer">OCMock</a> for mocking.</p>
  
  <p>When the server returns an error, the connection does not call <code>connection:didFailWithError:</code> on the delegate, but calls <code>connection:didReceiveResponse:</code>, <code>connection:didReceiveData:</code>, and <code>connectionDidFinishLoading:</code> instead. I'm currently checking the status code on the response in <code>connection:didReceiveResponse:</code> and calling <code>cancel</code> on the connection when the status code looks like an error to prevent <code>connectionDidFinishLoading:</code> from being called, where a successful response would be reported.</p>
  
  <p>Providing a static stubbed <code>NSURLConnection</code> is simple, but I want my test to change it's behaviour when one of the mock connection's methods is called. Specifically, I want the test to be able to tell when the code has called <code>cancel</code> on the mock connection, so the test can stop calling <code>connection:didReceiveData:</code> and <code>connectionDidFinishLoading:</code> on the delegate.</p>
  
  <p>Is there a way for tests to tell if <code>cancel</code> has been called on the mock object? Is there a better way to test code that uses <code>NSURLConnection</code>? Is there a better way to handle HTTP error statuses?</p>