Code Monkey home page Code Monkey logo

dvr's Introduction

DVR

Version Swift Version Carthage compatible Swift Package Manager

DVR is a simple Swift framework for making fake NSURLSession requests for iOS, watchOS, and OS X based on VCR.

Easy dependency injection is the main design goal. The API is the same as NSURLSession. DVR.Session is a subclass of NSURLSession so you can use it as a drop in replacement anywhere. (Currently only data tasks are supported.)

Usage

let session = Session(cassetteName: "example")
let task = session.dataTask(with: request) { data, response, error in
    // Do something with the response
}

// Nothing happens until you call `resume` as you'd expect.
task.resume()

This will playback the example cassette. The completion handler exactly the same way it normally would. In this example, DVR will look for a cassette named example.json in your test bundle.

If the recording of the request is missing, it will record and save it to disk. After saving to disk, it will assert with path of the recorded file. This causes the tests to stop so you can add the cassette to your test target and rerun your tests.

Recording Multiple Requests

By default, a DVR session only records one request. You can record multiple requests in the same cassette if you tell DVR when to start and stop recording.

let session = Session(cassetteName: "multiple")

// Begin recording multiple requests
session.beginRecording()

session.dataTask(with: URLRequest(url: URL(string: "http://apple.com")!)) { data, response, error in
    // Do something with the response

    session.dataTask(with: URLRequest(url: URL(string: "http://google.com")!)) { data, response, error in
        // Do something with the response
    }.resume()

    // Finish recording multiple requests
    session.endRecording() {
        // All requests have completed
    }
}.resume()

If you don't call beginRecording and endRecording, DVR will call these for your around the first request you make to a session. You can call endRecording immediately after you've submitted all of your requests to the session. The optional completion block that endRecording accepts will be called when all requests have finished. This is a good spot to fulfill XCTest expectations you've setup or do whatever else now that networking has finished.

dvr's People

Contributors

armstrongnate avatar ayanonagon avatar cshireman avatar danthorpe avatar dasmer avatar dgdosen avatar dmiluski avatar eliperkins avatar florianbuerger avatar hyperspacemark avatar loudmouth avatar misterwell avatar ocurr avatar sethhoward avatar soffes avatar stephencelis avatar

Watchers

 avatar  avatar  avatar  avatar

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.