### Install Mockingjay via CocoaPods (Ruby) Source: https://github.com/kylef/mockingjay/blob/master/README.md Instructions for adding Mockingjay as a dependency using the CocoaPods dependency manager. Add this line to your Podfile to include the library in your project. ```Ruby pod 'Mockingjay' ``` -------------------------------- ### Stub with JSON Data from File Fixture (Swift) Source: https://github.com/kylef/mockingjay/blob/master/README.md Demonstrates loading JSON data from a local file fixture within a test setup method (`setUp`) and using the `jsonData` builder to create a stub response with that data. ```Swift 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)) } ``` -------------------------------- ### Define Custom Request Matcher Function (Swift) Source: https://github.com/kylef/mockingjay/blob/master/README.md Provides an example of creating a custom function that determines whether a given URLRequest should be matched by a stub. The function takes a URLRequest and returns a boolean value. ```Swift func matcher(request:URLRequest) -> Bool { return true // Let's match this request } stub(matcher, failure(error)) ``` -------------------------------- ### Stub HTTP Request with URI Template and JSON (Swift) Source: https://github.com/kylef/mockingjay/blob/master/README.md Demonstrates creating a simple HTTP stub that matches requests based on a URI template and responds with a JSON body. The `uri` function uses the URITemplate.swift library for flexible path matching. ```Swift let body = [ "user": "Kyle" ] stub(uri("/{user}/{repository}"), json(body)) ``` -------------------------------- ### Stub Specific HTTP Method with JSON Response (Swift) Source: https://github.com/kylef/mockingjay/blob/master/README.md Shows how to create a stub that matches a specific HTTP method (PUT in this case) and a URI, responding with a JSON body. The `http` matcher allows specifying the required method. ```Swift let body = [ "description": "Kyle" ] stub(http(.put, uri: "/kylef/Mockingjay"), json(body)) ``` -------------------------------- ### Stub All Requests with Specific HTTP Status (Swift) Source: https://github.com/kylef/mockingjay/blob/master/README.md Shows how to stub all HTTP requests to return a specific HTTP status code, such as 404 Not Found. The `http` builder can also be configured with headers and a body. ```Swift stub(everything, http(status: 404)) ``` -------------------------------- ### Define Custom Response Builder Function (Swift) Source: https://github.com/kylef/mockingjay/blob/master/README.md Shows how to create a custom function that constructs the Response object for a matched request. The function takes a URLRequest and returns a Mockingjay `Response` enum value. ```Swift func builder(request: URLRequest) -> Response { let response = HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: nil, headerFields: nil)! return .success(response, .noContent) } stub(matcher, builder) ``` -------------------------------- ### Stub All Requests to Fail with Error (Swift) Source: https://github.com/kylef/mockingjay/blob/master/README.md Illustrates how to configure Mockingjay to make all outgoing HTTP requests result in a specified error. The `everything` matcher matches any request, and `failure` builds an error response. ```Swift let error = NSError() stub(everything, failure(error)) ``` -------------------------------- ### Updating Mockingjay Response API (Swift) Source: https://github.com/kylef/mockingjay/blob/master/CHANGELOG.md This snippet illustrates the breaking change in Mockingjay 2.0.0 where the response content specification transitioned from using an optional `NSData` to a `Download` enum. The first code block shows the new syntax using `.NoContent` or `.Content(data)`, while the second shows the deprecated syntax. ```Swift .Success(response, .NoContent) .Success(response, .Content(data)) ``` ```Swift .Success(response, nil) .Success(response, data) ``` -------------------------------- ### Streaming Stubbed Response Data in Mockingjay (Swift) Source: https://github.com/kylef/mockingjay/blob/master/CHANGELOG.md Introduced in Mockingjay 2.0.0, this enhancement allows simulating streaming response data. This snippet demonstrates how to use the `.StreamContent` case of the new `Download` enum, specifying the data and the chunk size for streaming. ```Swift .Success(response, .StreamContent(data, inChunksOf: 1024)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.