Spider
is a library designed to stub your network requests very easily.
- Xcode 10.2 or later
- iOS 10.0 or later
- Swift 5
Install with CocoaPods by adding the following to your Podfile
:
platform :ios, '10.0'
use_frameworks!
pod 'Spider'
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate Spider into your Xcode project using Carthage, specify it in your Cartfile
:
github "hoangtaiki/Spider" ~> 0.0.1
Run carthage update
to build the framework and drag the built Spider.framework
into your Xcode project.
To stub a request, first you need to create a StubRequest
and StubResponse
. Then you need add this stub with Spider and tell it to intercept network requests by calling the start()
method.
let url = "https://www.apple.com"
let matcher = url.asStringMatcher()
let responseBody = "{\"value\":\"test\"}".data(using: .utf8)
let response = StubResponse.success(200, responseBody!)
let request = StubRequest(method: .GET, matcher: matcher, response: response)
Spider.default.addStubRequest(request)
Spider.default.start()
This request will only accept url has absoluteString
is https://www.apple.com
let url = "https://www.apple.com"
let regex = try! NSRegularExpression(pattern: appleURLString, options: [])
let matcher = RegexMatcher(regex: regex)
let responseBody = "{\"value\":\"test\"}".data(using: .utf8)
let response = StubResponse.success(200, responseBody!)
let request = StubRequest(method: .GET, matcher: matcher, response: response)
Spider.default.addStubRequest(request)
Spider.default.start()
This request will accept any request start with https://www.apple.com
. Example: https://www.apple.com/home
and https://www.apple.com/users/1
let url = "https://www.apple.com"
let matcher = url.asStringMatcher()
let error = NSError(domain: "com.apple.error", code: 0,
userInfo: [NSLocalizedDescriptionKey: "Unauthorized"])
let response = StubResponse.failed(422, nil, error)
let request = StubRequest(method: .GET, matcher: matcher, response: response)
Spider.default.addStubRequest(request)
Spider.default.start()
let url = "https://www.apple.com"
let matcher = url.asStringMatcher()
let responseBody = "{\"value\":\"test\"}".data(using: .utf8)
let response = StubResponse.failed(422, responseBody, nil)
let request = StubRequest(method: .GET, matcher: matcher, response: response)
Spider.default.addStubRequest(request)
Spider.default.start()
This request will only accept url has absoluteString
is https://www.apple.com
We’re glad you’re interested in Spider, and we’d love to see where you take it. If you have suggestions or bug reports, feel free to send pull request or create new issue.
Thanks, and please do take it for a joyride!