Coder Social home page Coder Social logo

inigollamosasconneqtech / mockingjay Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kylef/mockingjay

0.0 0.0 0.0 396 KB

An elegant library for stubbing HTTP requests with ease in Swift

License: BSD 3-Clause "New" or "Revised" License

Ruby 3.17% Objective-C 2.64% Swift 94.20%

mockingjay's Introduction

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.

mockingjay's People

Contributors

kylef avatar hellostu avatar iamdecode avatar nwest avatar neonichu avatar hiroraba avatar blackm00n avatar modocache avatar antigp avatar felixclack avatar j-j-m avatar klaaspieter avatar shams-ahmed avatar teameh avatar itszero 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.