Skip to content

An elegant library for stubbing HTTP requests with ease in Swift

License

Notifications You must be signed in to change notification settings

hoangtaiki/Spider

Repository files navigation

Spider

Build Status codecov Platform Language License

Spider is a library designed to stub your network requests very easily.

Requirements

  • Xcode 10.2 or later
  • iOS 10.0 or later
  • Swift 5

Getting Started

CocoaPods

Install with CocoaPods by adding the following to your Podfile:

platform :ios, '10.0'
use_frameworks!
pod 'Spider'

Carthage

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.

Usage

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.

Create a stub for successful request with absolute url

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

Create a stub request with relative url

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

Create a stub for failed request with error

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()

Create a stub for failed request with response data

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

Contributing

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!