Skip to content

mokten/StubPlay

Repository files navigation

Summary

Save and replay http requests in Swift

Stubs http responses and supports any http response including text, html, json, images, videos and HLS.

CocoaPods Compatible Carthage Compatible Platform

  1. Features
  2. Requirements
  3. Compatible
  4. Installation
  5. Usage
    1. Usage
    2. Global Config
    3. Unit Test
  6. Concepts
  7. License

Features

  • Saves full http request and responses
  • Unit tests are easy - no more having to write a bunch of boilerplate code for the network layer
  • Automated UI tests are easy too
  • Audit responses
    1. Server side dev said they didn’t change anything but you can prove they did because you saved the responses
  • Api not ready? That’s ok you can create your own stubs and use them until the api is ready
  • Debug http requests - view requests being saved as responses are being consumed by your App
  • Replay customer experiences -> need to upload to your server yourself

Requirements

  • iOS 10.0+ / tvOS 11.0+
  • Xcode 11.3+
  • Swift 5.1+

Installation

Swift Package Manager (SPM)

dependencies: [
    .package(name: "StubPlay", url: "https://github.com/mokten/StubPlay.git", .upToNextMajor(from: "0.1.11"))
]

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

  • Cocoapods 1.7+
$ gem install cocoapods

CocoaPods 1.7+ is required to build StubPlay

To integrate StubPlay into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '11.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'StubPlay'
end

Then, run the following command:

$ pod install

Carthage

  • Carthage 0.33+

Cartfile

github "mokten/StubPlay"

Usage

We recommend enabling stubs as soon as possible - in your main.swift file or App delegate init()

By default: requests/response are saved in the caches/com.mokten.stubplay directory. Every time the app is run this directory will be cleared out.

Add a reference folder to your app. ie. "Stub/default", this is where your stubs will be read from

import StubPlay

// This will save all requests and responses to the app cache directory
// Start the app and navigate around
// Once you have completed your scenario then copy the files in the cache directory to your reading stub directory "Stub/default"
let config = StubConfig(folders: ["Stub/default"],
                        saveResponsesDirURL: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("stubplay"),
                        skipSavingStubbedResponses: false,
                        validateResponseFile: false,
                        clearSaveDir: true,
                        bundle: Bundle(for: type(of: self) ,
                        isEnabledServer: true,
                        protocolURLSessionConfiguration: nil,
                        isLogging: true)
try StubPlay.default.start(with: config)

_ = UIApplicationMain(CommandLine.argc, CommandLine.unsafeArgv, NSStringFromClass(Application.self), NSStringFromClass(AppDelegate.self))

or

import UIKit
import StubPlay

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    override init() {
        super.init()
        do {
            // let config = StubConfig() // use default settings or else override them as follows
            let config = StubConfig(folders: ["Stub/default"],
                                    saveResponsesDirURL: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("stubplay"),
                                    skipSavingStubbedResponses: false,
                                    validateResponseFile: false,
                                    clearSaveDir: true,
                                    bundle: Bundle.main,
                                    isEnabledServer: true,
                                    protocolURLSessionConfiguration: nil,
                                    isLogging: true)
            try StubPlay.default.start(with: config)
        } catch {
            print(error)
        }
    }
    ...
}

Optionally, all requests are saved to the Caches directory (this can be turned off with configuration):

ie: ~/Library/Developer/CoreSimulator/Devices/C62F6E5A-6459-45B6-B20B-B7C8E07AA529/data/Containers/Data/Application/1C1E61F8-B7B9-45FA-AA7E-7928E8952989/Library/Caches/com.mokten.stubplay

Concepts

  1. Stubbed request / response has 2 files
  2. request+response/rewrite rule file -> json format
  3. response body file -> native format

The response body is in its own file so that it can be easily be used by viewers/editers ie. image, json, text, html, videos

License

StubPlay is released under the MIT license. See LICENSE for details.