GithubHelp home page GithubHelp logo

honood / nocilla Goto Github PK

View Code? Open in Web Editor NEW

This project forked from luisobo/nocilla

0.0 1.0 0.0 1.15 MB

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

License: MIT License

nocilla's Introduction

Nocilla

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.

Features

  • Stub HTTP and HTTPS requests in your unit tests.
  • Awesome DSL that will improve the readability and maintainability of your tests.
  • Tested.
  • Fast.
  • Extendable to support more HTTP libraries.
  • Huge community, we overflowed the number of Stars and Forks in GitHub (meh, not really).

Limitations

  • 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

Installation

WIP (please, read: You figure it out, and then you tell me)

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

Usage

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"
SPEC_BEGIN(ExampleSpec)
beforeAll(^{
  [[LSNocilla sharedInstance] start];
});
afterAll(^{
  [[LSNocilla sharedInstance] stop];
});
afterEach(^{
  [[LSNocilla sharedInstance] clearStubs];
});

it(@"should do something", ^{
  // Stub here!
});
SPEC_END

Stubbing requests

Stubbing a simple request

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

stubRequest(@"GET", @"http://www.google.com");

Stubbing a request with a particular header

stubRequest(@"GET", @"https://api.example.com").
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", @"https://api.example.com/dogs.json").
withHeaders(@{@"Accept": @"application/json", @"X-CUSTOM-HEADER": @"abcf2fbc6abgf"});

Stubbing a request with a particular body

stubRequest(@"POST", @"https://api.example.com/dogs.json").
withHeaders(@{@"Accept": @"application/json", @"X-CUSTOM-HEADER": @"abcf2fbc6abgf"}).
withBody(@"{\"name\":\"foo\"}");

Returning a specific status code

stubRequest(@"GET", @"http://www.google.com").andReturn(404);

Returning a specific status code and header

The same approch here, you can use withHeader or withHeaders

stubRequest(@"POST", @"https://api.example.com/dogs.json").
andReturn(201).
withHeaders(@{@"Content-Type": @"application/json"});

Returning a specific status code, headers and body

stubRequest(@"GET", @"https://api.example.com/dogs.json").
andReturn(201).
withHeaders(@{@"Content-Type": @"application/json"}).
withBody(@"{\"ok\":true}");

Returning raw responses recorded with curl -is

curl -is http://api.example.com/dogs.json > /tmp/example_curl_-is_output.txt

stubRequest(@"GET", @"https://api.example.com/dogs.json").
andReturnRawResponse([NSData dataWithContentsOfFile:"/tmp/example_curl_-is_output.txt"]);

All together

stubRequest(@"POST", @"https://api.example.com/dogs.json").
withHeaders(@{@"Accept": @"application/json", @"X-CUSTOM-HEADER": @"abcf2fbc6abgf"}).
withBody(@"{\"name\":\"foo\"}").
andReturn(201).
withHeaders(@{@"Content-Type": @"application/json"}).
withBody(@"{\"ok\":true}");

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

Contributing

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

nocilla's People

Contributors

bencevans avatar jeanregisser avatar luisobo avatar modocache avatar tonyarnold avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.