AliSoftware/OHHTTPStubs — pull request #28 by kcharwood (2013-07-24 20:42:25+00:00)
Title: Support for NSInputStream

Hey guys,

First off, thanks for the great library. It has been super useful for us across many different projects.

We've been pushing the limits a bit with this library, and have ran into a few short comings with current implementation, including:

* No support for "progress". You get all the data in one call back.
* No support for streaming a file from disk, meaning the entire file must loaded into memory before it can be returned.
* No built in support for files not found in the main bundle. You can load them in as NSData, but large files must be loaded into memory.

We have a few different applications where we have been testing large files with different formats for a file browser. We've built out a protocol that allows QA engineers to load the data into the application using iTunes File Sharing, and the protocol pulls those files in and returns them using Stub Response. The problem is several of these files are quite large, and we can't fit them into memory at once. In addition, our UI supports progress, so we'd like to see progress for the stub as well to fully test the UI without actually needing a network connection.

With that being said, I wanted to take a stab at implementing this library with an NSInputStream, rather than relying on just a plain old NSData object. There are quite a few changes here, so it may be best to think of this as version 3.0. I'll try and highlight a few below:

**OHHTTPStubsResponse**
Property Updates:
* *REMOVED* `NSData * responseData` - No longer needed since we funnel things through a NSInputStream
* *ADDED* `NSInputStream* inputStream` - New property to represent the input stream
* *ADDED* `NSTimeInterval requestTime` - The amount of time to wait before we begin to send the response back. Defaults to 1.0. In the 2.0 implementation, this was calculated as 10% of the total response time. However, with large files this time was too large, so we've moved into a variable for finer tuned control. This value must be greater than or equal to zero.
* *ADDED* `unsigned long long dataSize` - Automatically calculated based on the content provided. The Content-Length header is also automatically applied for you with this value.

Method Updates:
* Several methods now include the `requestTime` parameter.
* Added new instance method to init with a file path
* Now funneling the `initWithData:..` and `initWithFilePath:...` instance methods through a new method named `initWithInputStream:...`, making `initWithInputStream:...` the new designated initializer.
* Changed the naming convention of the methods that took in a file name to more clearly document that it was a file name in the main bundle.

**OHHTTPStubs**
* Modified the start loading method to recursively call a new method that reads data out of the input stream, and forwards it to NSURLProtocolClient
* Chunked data is sent every 1/4 of a second at the specified bandwidth of the stub.
* Modified how the timing for the request is handled, using the new requestTime property on the stub.

**Demo App**
* The delay switch now controls the request time, and the response time has been set to wifi.

**Wrap-up**
I've tried to follow the syntax in the project, but I may have missed a few places... (I'm not a newline leading bracket kind of guy...)

To highlight, here are the benefits of this pull request:
* NSInputStream allows us to send back data in chunks, which allow for proper progress reporting during the stubbed request.
* NSInputStream does not require the entire file be loaded in memory before returning it, allowing larger files to be stubbed.
* Added support for file paths not in the main bundle, enabling more advanced stubbing approaches
* Finer control of the timing of the request going up

I have all the unit tests up to date and running as well.

Would love to get your feedback on this implementation, and if you prefer me to change anything. Looking forward to the discussion around this.