Rec

Helper library to record URL requests and save them locally (great for fixtures in HTTP stubbing).


License
MIT
Install
pod try Rec

Documentation

Carthage compatible Version Circle CI CI Status Coverage Status License Platform

https://cldup.com/Q0g0iZrPlT.png

Helper library to record URL requests and save them locally (great for fixtures in HTTP stubbing)


Why? • OHHTTPStubs • Rec • Error Codes • TODO •


Why?

@Orta told me once over Skype how he has incorporated into their API teams some way to have a "sample" JSON for each call (I'm paraphrasing) so they can actually test with «real life» sample data and even support an «offline» mode for the app for when developing while riding the 🚋, etc.

Sadly for me I work at a company that does multiple projects a year and not always we control the API development part which kinds of "cripples" the addition of "sample" JSONs.

OHHTTPStubs

Enter OHHTTPStubs; I started adding HTTP Stubs to my projects for testing purposes and had to manually enter the URL for each request and save the resulting JSON in each file to add it to the fixtures folder for the test cases.

As an engineer (and more importantly an overly lazy person) I decided that this was a terribly tedious and slow process; so… why not automate it?

Rec

This is where Rec comes to play; it's an NSURLProtocol that intercepts each NSURL request made from NSURLSessions (with defaultSessionConfiguration & ephemeralSessionConfiguration) and adds itself as the delegate for the connection; once the request succeeds the framework will save it to the application's Documents folder (under «Fixtures» folder).

Rec follows in the steps of OHHTTPStubs and «automagically» adds itself as the listener (it does so by swizzling defaultSessionConfiguration & ephemeralSessionConfiguration and returning a "pre configured" session configuration object).

Error Codes

Here's the list of «internal» error codes:

public enum RecordingError: ErrorType {
    /// Unable to start loading the request
    case UnableToStartLoadingRequest
    /// Failed to connect with NSURLRequest
    case FailedToConnectToWithRequest(NSURLRequest)
    /// Unable to correctly parse as a `JSON` response
    case UnableToCorrectlyParseAsJSON(NSURLRequest)
    /// Unable to get the `Document`'s directory path
    case UnableToGetDocumentsDirectoryPath
    /// Unable to extract the request's last path component for file name generation.
    case UnableToExtractRequestsLastPath(NSURLRequest)
    /// Unable to save file to path
    case UnableToSaveFileToPath(String)
    /// Unable to get a successful response from URL.
    case UnableToGetSuccessfullResponseFromRequest(NSURLRequest)
}

TODO:

  • Support other responses (e.g.: xml, etc) • Issue #1
  • Support custom «save» paths • Issue #2
  • Support disabling automatic «injection» • Issue #3
  • Fix the race condition happening on the Example App (println() I'm 👀 at you 😒 ) • Issue #4
  • Add support for OS X • Issue #5