Coder Social home page Coder Social logo

objc-mocktail's Introduction

Mocktail

A simple(r) way to stub out HTTP servers in your Objective-C app.

Features

  • Only a hundred lines of code
  • As simple as possible
  • Slow!
    • Performance is O(N) for serving N mock responses!
    • Loads entire—potentially very large—files in to memory!
  • Poorly tested
  • Might crash on malformed input
  • Kind of a hack

You shouldn't ship Mocktail with your code. It's a development tool.

Usage

No, really, it's cool, you can still use it! You can read the entire source code in about five minutes to see what's going on. The entire API is a single method:

+ (void)startWithContentsOfDirectoryAtURL:(NSURL *)url;

All you do is put a bunch of files in a particular format (more on that later) and a .tail file extension in a directory and pass the URL of that directory to Mocktail. Et voilà.

The directory could be inside your app bundle (if you're supporting a test suite, say) or you could be really lazy about it if you're running in the simulator and just set the directory URL to a folder on your desktop.

File format

I wish there were a standard for this, but alas, that's not to be the case. (HAR thought it was going to be up to the task, but it's for forensic analysis of what happened during requests, not what came back.)

So we made one up. It's newline-delimited.

Line 1 is a regular expression that matches the HTTP method. Something like GET or GET|POST or .* will work.

Line 2 is a regular expression that matches the full URL of the HTTP request. So something like http://yourserver.com:1234/very/specific/path\?param1=value1 or maybe just /partial/path/to/something/.* is fine too.

Line 3 is the HTTP status code of the response. Probably 200.

Line 4 is the HTTP/MIME content type of the content. application/json; charset=utf-8 is a nice value for this line, as is text/html. Various types of images might work, I haven't tried them.

Line 5 is blank.

Everything after the newline ending line 5 (that is to say, line 6 and on) is sent back as the response body, verbatim. It doesn't even matter what the filename is. You just use one of these files per mock response "endpoint" and Mocktail loads them all in.

Mad props

While NSURLProtocol has been around forever, its uses aren't obvious unless you read things like @mattt's awesome NSHipster blog post on the subject. If you don't read NSHipster, you're missing out. It's great.

Contributing

Your pull requests are welcome, but please don't take this too seriously. Remember, one of Mocktail's greatest features is its simplicity. Our lawyers want you to sign this form first, too.

objc-mocktail's People

Contributors

nsillik avatar puls avatar

Watchers

Kevin Griffin avatar James Cloos 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.