• Stars
    star
    1,490
  • Rank 31,531 (Top 0.7 %)
  • Language
    Swift
  • License
    BSD 3-Clause "New...
  • Created almost 10 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An elegant library for stubbing HTTP requests with ease in Swift

Mockingjay

An elegant library for stubbing HTTP requests in Swift, allowing you to stub any HTTP/HTTPS using NSURLConnection or NSURLSession. That includes any request made from libraries such as Alamofire and AFNetworking.

Installation

CocoaPods is the recommended installation method.

pod 'Mockingjay'

Usage

Mockingjay has full integration to XCTest and you simply just need to register a stub, it will automatically be unloaded at the end of your test case. It will also work with the Quick behaviour-driven development framework.

Simple stub using a URI Template, returning a response with the given JSON encoded structure

let body = [ "user": "Kyle" ]
stub(uri("/{user}/{repository}"), json(body))

The uri function takes a URL or path which can have a URI Template. Such as the following:

  • https://github.com/kylef/WebLinking.swift
  • https://github.com/kylef/{repository}
  • /kylef/{repository}
  • /kylef/URITemplate.swift

Stubbing a specific HTTP method with a JSON structure

let body = [ "description": "Kyle" ]
stub(http(.put, uri: "/kylef/Mockingjay"), json(body))

Stubbing everything request to result in an error

let error = NSError()
stub(everything, failure(error))

Stub with a specific HTTP response

stub(everything, http(status: 404))

Note, the http builder can take a set of headers and a body too.

Stub

The stub method in Mockingjay takes two functions or closures, one to match the request and another to build the response. This allows you to easily extend the syntax to provide your own specific functions.

stub(matcher, builder)

Matchers

A matcher is simply a function that takes a request and returns a boolean value for if the stub matches the request.

func matcher(request:URLRequest) -> Bool {
  return true  // Let's match this request
}

stub(matcher, failure(error))

Builders

Builders are very similar to a matcher, it takes a request, and it returns either a success or failure response.

func builder(request: URLRequest) -> Response {
  let response = HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: nil, headerFields: nil)!
  return .success(response, .noContent)
}

stub(matcher, builder)

Generics

You can make use of the builtin generic matchers and builders. These can be used alone, or in conjunction with custom components.

Builtin Matchers

  • everything - Matches every given request.
  • uri(template) - Matches using a URI Template.
  • http(method, template) - Matches using a HTTP Method and URI Template.

Builtin Builders

  • failure(error) - Builds a response using the given error.
  • http(status, headers, data) - Constructs a HTTP response using the given status, headers and data.
  • json(body, status, headers) - Constructs a JSON HTTP response after serialising the given body as JSON data.
  • jsonData(data, status, headers) - Constructs a JSON HTTP response with raw JSON data.

Using JSON files as test fixtures

During setUp, load the JSON file as NSData and use jsonData.

override func setUp() {
  super.setUp()
  let url = Bundle(for: type(of: self)).url(forResource: "fixture", withExtension: "json")!
  let data = try! Data(contentsOf: url)
  stub(matcher, jsonData(data))
}

License

Mockingjay is licensed under the BSD license. See LICENSE for more info.

More Repositories

1

swiftenv

Swift Version Manager
Shell
1,979
star
2

Commander

Compose beautiful command line interfaces in Swift
Swift
1,530
star
3

PathKit

Effortless path operations in Swift
Swift
1,453
star
4

JSONWebToken.swift

Swift implementation of JSON Web Token (JWT).
Swift
763
star
5

Spectre

BDD Framework and test runner for Swift projects and playgrounds
Swift
403
star
6

JSONSchema.swift

JSON Schema validator in Swift
Swift
274
star
7

URITemplate.swift

Swift implementation of URI Template (RFC6570)
Swift
199
star
8

apiblueprint.vim

This vim plugin brings syntax highlighting and linting for API Blueprint.
Vim Script
113
star
9

dotfiles

kylef's dotfile
Vim Script
33
star
10

swiftenv-api

API for swiftenv to return the available versions of Swift
Python
25
star
11

draughtsman

API Blueprint Parser for Python 3
Python
23
star
12

changelog

A formal specification for changelog files and entries
18
star
13

goji

Command line JIRA client
Python
18
star
14

swiftenv-docker

This repository contains swiftenv images for Docker
Makefile
18
star
15

irctk

A Python IRC client library
Python
6
star
16

maintain

A unified interface to maintaining projects of any language.
Python
6
star
17

ucp

A simple and universal command line tool for capturing screenshots and uploading to the web.
Shell
5
star
18

redirector

Simple web server for HTTP redirecting from one domain to another.
Python
4
star
19

pygments-apiblueprint

An API Blueprint Lexer for Pygments
Python
3
star
20

refract.py

A Python library for interacting with Refract
Python
3
star
21

life

This repository is used as a personal issue tracker
2
star
22

homebrew-formulae

Ruby
1
star