Episode #123

STHTTPRequest

13 minutes
Published on June 13, 2014

This video is only available to subscribers. Get access to this video and 572 others.

In this episode I take a look at a lightweight network library called STHTTPRequst. Specifically I like two features it provides: easy curl logging of outgoing requests, and a test response queue for performing unit tests against canned responses. Whether or not you want to use this library, there are some good things to learn here.

Episode Links

Adding Curl Logging

⌥ Click the Run button in Xcode (or go to Schemes, Edit Current Scheme) in order to open the Scheme Editor. Under the "Run" Section, add a launch argument with the following:

-STHTTPRequestShowCurlDescription 1

This will enable logging of curl commands in your debugger.

Testing Network Calls

First step is to manually drag the test support classes from UnitTestAdditions over to your test target. Then add a reference to the headers:

#import "STHTTPRequest+UnitTests.h"
#import "STHTTPRequestTestResponse.h"
#import "STHTTPRequestTestResponseQueue.h"

Next you need to add a test response to the queue with the canned data:

STHTTPRequestTestResponseQueue *queue = [STHTTPRequestTestResponseQueue sharedInstance];
STHTTPRequestTestResponst *response = [STHTTPRequestTestResponse testResponseWithBlock: ^(STHTTPRequest *r) {
   // customize request here
}];
[queue enqueue:response];

Next you just make the call as normal and you'll get a synchronous test callback.

Ensuring that the callback block is run:

Even though the network call is supposed to be swizzled by the test classes to be synchronous, we still need to ensure that our block was called:

    __block BOOL blockCalled;
    [self.client search:@"Breaking" completion:^(NSArray *results, NSError *error) {
        XCTAssertEqual(50, results.count, @"Expected 50 objects");
        blockCalled = YES;
    }];

    XCTAssertTrue(blockCalled, @"block was never called");

Now we'll have a failure if the block wasn't called, and if it was called we're covered by the assertion in the block.