{"id":15028484,"url":"https://github.com/kylef/mockingjay","last_synced_at":"2025-05-15T07:03:52.713Z","repository":{"id":26139071,"uuid":"29584081","full_name":"kylef/Mockingjay","owner":"kylef","description":"An elegant library for stubbing HTTP requests with ease in Swift","archived":false,"fork":false,"pushed_at":"2023-10-13T20:53:30.000Z","size":396,"stargazers_count":1491,"open_issues_count":24,"forks_count":179,"subscribers_count":26,"default_branch":"master","last_synced_at":"2025-05-15T07:03:50.856Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kylef.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2015-01-21T10:16:13.000Z","updated_at":"2025-05-09T09:20:50.000Z","dependencies_parsed_at":"2022-07-12T16:08:24.510Z","dependency_job_id":"3063b722-b053-44d1-ab66-d569f88a1385","html_url":"https://github.com/kylef/Mockingjay","commit_stats":{"total_commits":121,"total_committers":19,"mean_commits":6.368421052631579,"dds":0.4380165289256198,"last_synced_commit":"b88c9dce2b7561cccbf35e2882b3c71a2efa387a"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylef%2FMockingjay","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylef%2FMockingjay/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylef%2FMockingjay/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylef%2FMockingjay/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kylef","download_url":"https://codeload.github.com/kylef/Mockingjay/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254291974,"owners_count":22046425,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-24T20:08:25.828Z","updated_at":"2025-05-15T07:03:52.682Z","avatar_url":"https://github.com/kylef.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mockingjay\n\nAn 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).\n\n## Installation\n\n[CocoaPods](http://cocoapods.org/) is the recommended installation method.\n\n```ruby\npod 'Mockingjay'\n```\n\n## Usage\n\nMockingjay 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.\n\n#### Simple stub using a URI Template, returning a response with the given JSON encoded structure\n\n```swift\nlet body = [ \"user\": \"Kyle\" ]\nstub(uri(\"/{user}/{repository}\"), json(body))\n```\n\nThe `uri` function takes a URL or path which can have a [URI Template](https://github.com/kylef/URITemplate.swift). Such as the following:\n\n- `https://github.com/kylef/WebLinking.swift`\n- `https://github.com/kylef/{repository}`\n- `/kylef/{repository}`\n- `/kylef/URITemplate.swift`\n\n#### Stubbing a specific HTTP method with a JSON structure\n\n```swift\nlet body = [ \"description\": \"Kyle\" ]\nstub(http(.put, uri: \"/kylef/Mockingjay\"), json(body))\n```\n\n#### Stubbing everything request to result in an error\n\n```swift\nlet error = NSError()\nstub(everything, failure(error))\n```\n\n#### Stub with a specific HTTP response\n\n```swift\nstub(everything, http(status: 404))\n```\n\n*Note, the `http` builder can take a set of headers and a body too.*\n\n## Stub\n\nThe `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.\n\n```swift\nstub(matcher, builder)\n```\n\n### Matchers\n\nA matcher is simply a function that takes a request and returns a boolean value for if the stub matches the request.\n\n```swift\nfunc matcher(request:URLRequest) -\u003e Bool {\n  return true  // Let's match this request\n}\n\nstub(matcher, failure(error))\n```\n\n### Builders\n\nBuilders are very similar to a matcher, it takes a request, and it returns either a success or failure response.\n\n```swift\nfunc builder(request: URLRequest) -\u003e Response {\n  let response = HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: nil, headerFields: nil)!\n  return .success(response, .noContent)\n}\n\nstub(matcher, builder)\n```\n\n### Generics\n\nYou can make use of the builtin generic matchers and builders. These can be used alone, or in conjunction with custom components.\n\n### Builtin Matchers\n\n- `everything` - Matches every given request.\n- `uri(template)` - Matches using a URI Template.\n- `http(method, template)` - Matches using a HTTP Method and URI Template.\n\n### Builtin Builders\n\n- `failure(error)` - Builds a response using the given error.\n- `http(status, headers, data)` - Constructs a HTTP response using the given status, headers and data.\n- `json(body, status, headers)` - Constructs a JSON HTTP response after serialising the given body as JSON data.\n- `jsonData(data, status, headers)` - Constructs a JSON HTTP response with raw JSON data.\n\n### Using JSON files as test fixtures\n\nDuring `setUp`, load the JSON file as `NSData` and use `jsonData`.\n\n```swift\noverride func setUp() {\n  super.setUp()\n  let url = Bundle(for: type(of: self)).url(forResource: \"fixture\", withExtension: \"json\")!\n  let data = try! Data(contentsOf: url)\n  stub(matcher, jsonData(data))\n}\n```\n\n## License\n\nMockingjay is licensed under the BSD license. See [LICENSE](LICENSE) for more\ninfo.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkylef%2Fmockingjay","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkylef%2Fmockingjay","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkylef%2Fmockingjay/lists"}