Find file
Fetching contributors…
Cannot retrieve contributors at this time
158 lines (124 sloc) 5.12 KB


Stunning HTTP stubbing for iOS and OS X. Testing HTTP requests has never been easier.

This library was inspired by WebMock and it's using this approach to stub the requests.


  • Stub HTTP and HTTPS requests in your unit tests.
  • Awesome DSL that will improve the readability and maintainability of your tests.
  • NEW! Match requests with regular expressions.
  • NEW! Stub requests with errors.
  • Tested.
  • Fast.
  • Extendable to support more HTTP libraries.


  • At this moment only works with requests made with NSURLConnection, but it's possible to extend Nocilla to support more HTTP libraries. Nocilla has been tested with AFNetworking and MKNetworkKit

As a CocoaPod

Just add this to your Podfile

pod 'Nocilla'

Other approaches

  • You should be able to add Nocilla to you source tree. If you are using git, consider using a git submodule


Yes, the following code is valid Objective-C, or at least, it should be

The following examples are described using Kiwi

Common parts

Until Nocilla can hook directly into Kiwi, you will have to include the following snippet in the specs you want to use Nocilla:

#import "Kiwi.h"
#import "Nocilla.h"
  [[LSNocilla sharedInstance] start];
  [[LSNocilla sharedInstance] stop];
  [[LSNocilla sharedInstance] clearStubs];

it(@"should do something", ^{
  // Stub here!

Stubbing requests

Stubbing a simple request

It will return the default response, which is a 200 and an empty body.

stubRequest(@"GET", @"");

NEW! Stubbing requests with regular expressions

stubRequest(@"GET", @"^http://(.*?)\.example\.com/v1/dogs\.json".regex);

Stubbing a request with a particular header

stubRequest(@"GET", @"").
withHeader(@"Accept", @"application/json");

Stubbing a request with multiple headers

Using the withHeaders method makes sense with the new Objective-C literals, but it accepts an NSDictionary.

stubRequest(@"GET", @"").
withHeaders(@{@"Accept": @"application/json", @"X-CUSTOM-HEADER": @"abcf2fbc6abgf"});

Stubbing a request with a particular body

stubRequest(@"POST", @"").
withHeaders(@{@"Accept": @"application/json", @"X-CUSTOM-HEADER": @"abcf2fbc6abgf"}).

Returning a specific status code

stubRequest(@"GET", @"").andReturn(404);

Returning a specific status code and header

The same approch here, you can use withHeader or withHeaders

stubRequest(@"POST", @"").
withHeaders(@{@"Content-Type": @"application/json"});

Returning a specific status code, headers and body

stubRequest(@"GET", @"").
withHeaders(@{@"Content-Type": @"application/json"}).

Returning raw responses recorded with curl -is

curl -is > /tmp/example_curl_-is_output.txt

stubRequest(@"GET", @"").
andReturnRawResponse([NSData dataWithContentsOfFile:"/tmp/example_curl_-is_output.txt"]);

All together

stubRequest(@"POST", @"").
withHeaders(@{@"Accept": @"application/json", @"X-CUSTOM-HEADER": @"abcf2fbc6abgf"}).
withHeaders(@{@"Content-Type": @"application/json"}).

NEW! Making a request fail

This will call the failure handler (callback, delegate... whatever your HTTP client uses) with the specified error.

stubRequest(@"POST", @"").
withHeaders(@{@"Accept": @"application/json", @"X-CUSTOM-HEADER": @"abcf2fbc6abgf"}).
andFailWithError([NSError errorWithDomain:@"foo" code:123 userInfo:nil]);

Unexpected requests

If some request is made but it wasn't stubbed, Nocilla won't let that request hit the real world. In that case your test should fail. At this moment Nocilla will return a response with a 500, the header X-Nocilla: Unexpected Request and a body with a meaningful message about the error and how to solve it, including a snippet of code on how to stub the unexpected request. I'm not particularly happy with returning a 500 and this will change. Check this issue for more details.

Other alternatives


  1. Fork it
  2. Create your feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create new Pull Request