TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | May 2015 |
SPMSupports SPM | ✗ |
Maintained by Esteban Torres.
Helper library to record URL requests and save them locally (great for fixtures in HTTP stubbing)
Why? • OHHTTPStubs • Rec • Error Codes • TODO •
@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" JSON
s.
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?
This is where Rec
comes to play; it's an NSURLProtocol
that intercepts each NSURL
request made from NSURLSession
s (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).
Here's the list of «internal» error codes:
-666
: Unable to start loading the request-667
: Failed to connect to URL-668
: Unable to correctly parse as a JSON
response-669
: Unable to get the Document
's directory path-670
: Unable to extract the request's last path component for file name generation.-671
: Unable to save file to path-672
: Unable to get a successful response from URL.