ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - kylef/Mockingjay: An elegant library for stubbing HTTP requests with ease in Swift
An elegant library for stubbing HTTP requests with ease in Swift - kylef/Mockingjay
Visit Site

GitHub - kylef/Mockingjay: An elegant library for stubbing HTTP requests with ease in Swift

GitHub - kylef/Mockingjay: 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 Resources
to explore the angular.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory