WiremockClient 是一个 HTTP 客户端,允许用户从 Xcode 项目中与独立的 Wiremock 实例进行交互。
WiremockClient 可以通过 CocoaPods 和 Swift Package Manager 获取。
要使用 CocoaPods 安装,只需将以下行添加到您的 Podfile
pod "WiremockClient"
要使用 SwiftPM 安装,请将以下行添加到您的 Package.swift 文件的 dependencies
部分
.package(url: "https://github.com/mobileforming/WiremockClient", .upToNextMajor(from: Version(major: 2, minor: 2, patch: 0)))
WiremockClient 的功能与 Wiremock 原生 Java API 中的功能紧密对应,如文档中所述。 该 pod 使您能够从 Xcode 项目中构建 JSON 映射并将其发布到独立的 Wiremock 实例。 假定您熟悉 Wiremock 的基本用法,包括初始化独立的实例并使用映射填充它。
要开始使用 WiremockClient,请在 localhost 端口 8080 上启动一个独立的 Wiremock 实例。 所有请求的基本 URL 默认设置为 https://:8080
,并且可以在 WiremockClient
文件的顶部进行修改。 在使用 WiremockClient 之前,请务必将您的基本 URL 添加到白名单,否则您将无法与您的独立实例通信。
以下代码将向您的 Wiremock 独立实例发布一个映射,该映射将匹配发送到 https://:8080/my/path
端点的任何请求,并返回状态 200
WiremockClient.postMapping(stubMapping:
StubMapping.stubFor(requestMethod: .ANY, urlMatchCondition: .urlEqualTo, url: "https://:8080/my/path")
.willReturn(
ResponseDefinition()
.withStatus(200)
)
)
此行为与 Stubbing 文档部分中描述的 Java API 紧密对应,因此此处不再赘述完整说明。 有三个重要的区别需要注意
stubFor()
方法现在是 StubMapping
类的类型函数。aResponse()
方法已被 ResponseDefinition
类的实例替换。stubFor
和 aResponse
方法创建的映射必须传递给 WiremockClient.postMapping(stubMapping: StubMapping)
函数,才能发布到独立的 Wiremock 实例。Java API 中提供的所有请求匹配逻辑都已在 WiremockClient 中重现。 有关以下方法的完整说明,请参考Request Matching文档部分。
一组 StubMapping
实例方法使用户能够逐步构建映射,指定为了使传入的网络请求被视为匹配而必须满足的条件
WiremockClient.postMapping(stubMapping:
StubMapping.stubFor(requestMethod: .ANY, urlMatchCondition: .urlEqualTo, url: "https://:8080/my/path")
.withHeader("Accept", matchCondition: .contains, value: "xml")
.withCookie("session", matchCondition: .matches, value: ".*12345.*")
.withQueryParam("search_term", matchCondition: .equalTo, value: "WireMock")
.withBasicAuth(username: "myUsername", password: "myPassword")
.withRequestBody(.equalTo, value: "Some request body string")
.willReturn(
ResponseDefinition()
)
)
添加了额外的 withRequestBodyEqualToJson
方法,以允许用户设置 Request Matching 文档的“JSON equality”部分中描述的 ignoreArrayOrder
和 ignoreExtraElements
标志
WiremockClient.postMapping(stubMapping:
StubMapping.stubFor(requestMethod: .ANY, urlMatchCondition: .urlEqualTo, url: "https://:8080/my/path")
.withRequestBodyEqualToJson(jsonString: "{ \"total_results\": 4 }", ignoreArrayOrder: true, ignoreExtraElements: true)
.willReturn(ResponseDefinition())
)
WiremockClient 还包含一个便捷方法,用于桩存存储在本地文件中的请求 JSON
WiremockClient.postMapping(stubMapping:
StubMapping.stubFor(requestMethod: .ANY, urlMatchCondition: .urlEqualTo, url: "https://:8080/my/path")
.withRequestBodyFromLocalJsonFile(fileName: "myFile", in: Bundle(for: type(of: self)))
.willReturn(ResponseDefinition())
)
映射也可以像 Stubbing 文档的“Stub priority”部分中所述的那样进行优先级排序
WiremockClient.postMapping(stubMapping:
StubMapping.stubFor(requestMethod: .ANY, urlMatchCondition: .urlEqualTo, url: "https://:8080/my/path")
.withPriority(1)
.willReturn(ResponseDefinition())
)
Java API 中提供的所有响应定义逻辑都已在 WiremockClient 中重现。 有关以下方法的完整说明,请参考Stubbing文档部分。
一组 ResponseDefinition
实例方法使用户能够指定要包含在匹配映射时返回的响应中的元素
WiremockClient.postMapping(stubMapping:
StubMapping.stubFor(requestMethod: .ANY, urlMatchCondition: .urlEqualTo, url: "https://:8080/my/path")
.willReturn(
ResponseDefinition()
.withStatus(200)
.withStatusMessage("Great jorb!")
.withHeaders(["Pragma": "no-cache", "Connection": "keep-alive"])
.withBody("Just a plain old text body")
)
)
WiremockClient 还包含一个便捷方法,用于返回存储在本地文件中的 JSON
WiremockClient.postMapping(stubMapping:
StubMapping.stubFor(requestMethod: .ANY, urlMatchCondition: .urlEqualTo, url: "https://:8080/my/path")
.willReturn(
ResponseDefinition()
.withLocalJsonBodyFile(fileName: "myFile", in: Bundle(for: type(of: self)))
)
)
与 Java API 中一样,请求可以代理到其他主机。 有关此方法的完整说明,请参阅Proxying文档部分
WiremockClient.postMapping(stubMapping:
StubMapping.stubFor(requestMethod: .ANY, urlMatchCondition: .urlEqualTo, url: "https://:8080/my/path")
.willReturn(
ResponseDefinition()
.proxiedFrom("http://myproxyhost.gov")
)
)
WiremockClient 还支持 Stateful Behavior 文档部分中描述的场景
WiremockClient.postMapping(stubMapping:
StubMapping.stubFor(requestMethod: .GET, urlMatchCondition: .urlEqualTo, url: "https://:8080/my/path")
.inScenario("Scenario Title")
.whenScenarioStateIs("Required Scenario State")
.willSetStateTo("New Scenario State")
.willReturn(
ResponseDefinition()
.withStatus(200)
)
)
以下方法将所有场景重置为其默认状态 (“Started”)
WiremockClient.resetAllScenarios()
更新映射需要引用其 UUID。 创建映射时,会自动为其分配 UUID。 但是,也可以手动分配 UUID 并将其缓存在变量中以供将来参考。 在下面的示例中,发布了一个映射,该映射在匹配时返回状态代码 200。 然后更新该映射以返回状态代码 404
let myMappingID = UUID()
WiremockClient.postMapping(stubMapping:
StubMapping.stubFor(requestMethod: .GET, urlMatchCondition: .urlEqualTo, url: "https://:8080/my/path")
.withUUID(myMappingID)
.willReturn(
ResponseDefinition()
.withStatus(200)
)
)
WiremockClient.updateMapping(uuid: myMappingID, stubMapping:
StubMapping.stubFor(requestMethod: .GET, urlMatchCondition: .urlEqualTo, url: "https://:8080/my/path")
.willReturn(
ResponseDefinition()
.withStatus(404)
)
)
与更新映射类似,删除映射需要引用其 UUID
let myMappingID = UUID()
WiremockClient.postMapping(stubMapping:
StubMapping.stubFor(requestMethod: .GET, urlMatchCondition: .urlEqualTo, url: "https://:8080/my/path")
.withUUID(myMappingID)
.willReturn(
ResponseDefinition()
.withStatus(200)
)
)
WiremockClient.deleteMapping(uuid: myMappingID)
也可以通过同时删除所有映射来重置您的 Wiremock 实例
WiremockClient.reset()
可以通过以下方法将映射持久化到 Wiremock 实例的 mappings
目录
WiremockClient.saveAllMappings()
WiremockClient 的典型用例如下所示
setup()
方法中调用 WiremockClient.postMapping()
,以便在应用启动之前发布所需的映射。WiremockClient.updateMapping()
以动态更改映射。tearDown()
方法中调用 WiremockClient.reset()
,以便在测试完成后删除所有映射。Ted Rothrock, theodore.rothrock@gmail.com
WiremockClient 在 MIT 许可证下可用。 有关更多信息,请参阅 LICENSE 文件。