Giter Site home page Giter Site logo

swiftvcr's Introduction

VCR

Build Status Version Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

In order to save tapes, you will need to define VCR_DIR in your scheme. This should point to the directory where you want tapes to be stored. We normally use this:

Name Value
VCR_DIR $(SOURCE_ROOT)/$(PROJECT_NAME)Tests/VCRTapes

Cocoapods

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate VCR into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'VCR'

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

Once you have your Swift package set up, adding VCR as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/aarons22/SwiftVCR.git", .upToNextMajor(from: "0.3.0"))
]

Usage

Recording

let session = VCRSession()
session.insertTape("example", record: true)

A json file called example.json will be placed into the VCR_DIR (defined above).

Mulitple requests

You can record multiple requests in given session by inserting additional tapes. VCRSession will write out a json file for each tape inserted.

For any additional requests made, it will fallback to the actual URLSession request.

Playback

let session = VCRSession()
session.insertTape("example")

This will read the file example.json from the VCR_DIR and return it in place of making the request.

Dependency Injection

VCRSession is a an instance of URLSession, so it can be injected into any class that uses it. For example, given an http client:

class HTTPClient {
    let session: URLSession

    init(session: URLSession = URLSession.shared) {
        self.session = session
    }

    func request(_ request: URLRequest, completion: @escaping (Bool) -> Void) {
        let task = self.session.dataTask(with: request) { (data, response, error) in
            completion(true)
        }

        task.resume()
    }
}

You can inject VCRSession in place of the default:

let session = VCRSession()
session.insertTape("example")
let client = HTTPClient(session: session)

Author

Aaron Sapp, @aaronsapp

License

VCR is available under the MIT license. See the LICENSE file for more info.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.