StubPlay 0.1.13

StubPlay 0.1.13

Yoo-Jin Lee 维护。



StubPlay 0.1.13

  • 作者
  • mokten

摘要

使用 Swift 保存和重放 HTTP 请求

模拟 HTTP 响应,支持任何 HTTP 响应,包括文本、HTML、JSON、图片、视频和 HLS。

CocoaPods Compatible Carthage Compatible Platform

  1. 特性
  2. 要求
  3. 兼容性
  4. 安装
  5. 使用方法
    1. 使用方法
    2. 单元测试
  6. 概念
  7. 许可

特性

  • 保存完整的 HTTP 请求和响应
  • 单元测试操作简单 - 不再需要为网络层编写大量样板代码
  • 自动化的 UI 测试也很简单
  • 审核响应
    1. 后端开发人员表示他们没有做任何修改,但是您可以验证他们确实做了修改,因为您保存了响应
  • API 是否已准备好?没关系,您可以创建自己的占位符并在 API 准备好之前使用它们
  • 调试 HTTP 请求 - 查看请求被保存为响应,并被您的应用程序消耗
  • 重放客户体验 -> 您需要自己将其上传到您的服务器

要求

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

安装

Swift 包管理器 (SPM)

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

CocoaPods

CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它:

  • Cocoapods 1.7+
$ gem install cocoapods

Cocoapods 1.7+ 是构建 StubPlay 所必需的。

要使用 CocoaPods 在 Xcode 项目中集成 StubPlay,请在您的 Podfile 中指定它。

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

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

然后运行以下命令:

$ pod install

Carthage

  • Carthage 0.33+

Cartfile

github "mokten/StubPlay"

用法

我们建议尽快启用占位符 - 在您的 main.swift 文件或 AppDelegate 的 init() 方法中。

默认情况下:请求/响应保存在 caches/com.mokten.stubplay 目录中。每次运行应用程序,此目录都会被清空。

将引用文件夹添加到您的应用程序中。例如,"Stub/default",这是您的占位符将从中读取的地方

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"
try? StubPlay.default.enableStub(for: StubConfig(folders: ["Stub/default"], isEnabledServer: true, isLogging: true))

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

或者

import UIKit
import StubPlay

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    override init() {
        super.init()
        try? StubPlay.default.enableStub(for: StubConfig(folders: ["Stub/default"], isEnabledServer: true, isLogging: true))
    }
    ...
}

可选地,所有请求都保存到 Caches 目录(这可以通过配置关闭)

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

概念

  1. 占位符请求/响应有 2 个文件
  2. request+response/rewrite 规则文件 -> JSON 格式
  3. 响应体文件 -> 本地格式

响应体存储在自己的文件中,以便观众/编辑者方便使用,即:图片、json、文本、html、视频

许可证

StubPlay采用MIT许可证发布。有关详情,请参阅LICENSE