Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kylef/Mockingjay
An elegant library for stubbing HTTP requests with ease in Swift
https://github.com/kylef/Mockingjay
Last synced: 3 months ago
JSON representation
An elegant library for stubbing HTTP requests with ease in Swift
- Host: GitHub
- URL: https://github.com/kylef/Mockingjay
- Owner: kylef
- License: bsd-3-clause
- Created: 2015-01-21T10:16:13.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2023-10-13T20:53:30.000Z (about 1 year ago)
- Last Synced: 2024-05-19T14:01:20.376Z (6 months ago)
- Language: Swift
- Homepage:
- Size: 387 KB
- Stars: 1,487
- Watchers: 27
- Forks: 178
- Open Issues: 25
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ios - Mockingjay - An elegant library for stubbing HTTP requests with ease in Swift. (Testing / Other Testing)
- awesome-swift - Mockingjay - An elegant library for stubbing HTTP requests with ease. (Libs / Testing)
- awesome-swift - Mockingjay - An elegant library for stubbing HTTP requests with ease. (Libs / Testing)
- awesome-swift - Mockingjay - Library for stubbing HTTP requests. (Testing)
- awesome-ios-star - Mockingjay - An elegant library for stubbing HTTP requests with ease in Swift. (Testing / Other Testing)
- fucking-awesome-swift - Mockingjay - An elegant library for stubbing HTTP requests with ease. (Libs / Testing)
- awesome-swift-cn - Mockingjay - An elegant library for stubbing HTTP requests with ease in Swift. (Libs / Testing)
- awesome-swift - Mockingjay - An elegant library for stubbing HTTP requests with ease in Swift ` 📝 2 years ago` (Testing [🔝](#readme))
README
# 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](https://github.com/Alamofire/Alamofire) and [AFNetworking](https://github.com/AFNetworking/AFNetworking).
## Installation
[CocoaPods](http://cocoapods.org/) is the recommended installation method.
```ruby
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](https://github.com/Quick/Quick) behaviour-driven development framework.
#### Simple stub using a URI Template, returning a response with the given JSON encoded structure
```swift
let body = [ "user": "Kyle" ]
stub(uri("/{user}/{repository}"), json(body))
```The `uri` function takes a URL or path which can have a [URI Template](https://github.com/kylef/URITemplate.swift). 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
```swift
let body = [ "description": "Kyle" ]
stub(http(.put, uri: "/kylef/Mockingjay"), json(body))
```#### Stubbing everything request to result in an error
```swift
let error = NSError()
stub(everything, failure(error))
```#### Stub with a specific HTTP response
```swift
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.
```swift
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.
```swift
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.
```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)
```### 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`.
```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))
}
```## License
Mockingjay is licensed under the BSD license. See [LICENSE](LICENSE) for more
info.