测试已测试 | ✓ |
语言语言 | Obj-CObjective C |
许可证 | MIT |
发布上次发布 | 2015年11月 |
由 Paul Evans 维护。
依赖 | |
PEObjc-Commons | ~> 1.0.86 |
KissXML | ~> 5.0 |
PEXML-Utils | ~> 1.0.4 |
OHHTTPStubs | ~> 3.1.10 |
一个用于通过简单的 XML 配置文件轻松模拟 HTTP 响应的 iOS 静态库。PEWire-Control 是建立在优秀的 OHHTTPStubs iOS 库 之上的。
PEWire-Control 是 PE* iOS 库套件的一部分。
目录
使用 PEWire-Control 的典型用途将是在单元测试的上下文中。想象一下,您有一个单元测试,它执行调用某些网络服务的代码。
通常您想在测试源文件夹内创建一个物理文件夹;命名它为类似于 "http-mock-responses"的东西。在 Xcode 中,将该文件夹添加为测试组 "支持文件" 组内的 "参考文件夹"。在 http-mock-responses/ 文件夹中创建您的模拟响应 XML 文件。
<?xml version="1.0" encoding="UTF-8" ?>
<http-response statusCode="200">
<annotation name="fetch success" host="example.com" port="80" scheme="http" uri-path="/fp/users"
request-method="GET">Successful fetch of user.</annotation>
<headers>
<header name="Content-Type" value="application/vnd.name.paulevans.user-v0.0.1+json" />
<header name="Last-Modified" value="Tue, 02 Sep 2014 8:03:12 GMT" />
<header name="fp-auth-token" value="1092348123049OLSDFJLIE001234" />
</headers>
<cookies>
<cookie name="cookie1"
value="some value"
path="/"
secure="true"
domain=".paulevans.name"
expires="Sat, 16-Nov-2015 15:00:00 GMT" />
</cookies>
<body>
<![CDATA[
{ "fpuser/name": "Paul Evans",
"fpuser/email": "[email protected]",
"fpuser/username": "evansp2",
"fpuser/creation-date": "Tue, 02 Sep 2014 8:03:12 GMT",
"_links": {
"vehicles": {
"href": "http://example.com/fp/users/U1123409100/vehicles",
"type": "application/vnd.name.paulevans.vehicle-v0.0.1+json"},
"fuelpurchase-logs": {
"href": "http://example.com/fp/users/U1123409100/fplogs",
"type": "application/vnd.name.paulevans.fplog-v0.0.1+json"}}}
]]>
</body>
</http-response>
将此文件保存为 http-mock-responses/ 文件夹中的 "fetch-user-success.xml"。
// Get the path to our mock HTTP response XML file.
NSBundle *testBundle = [NSBundle bundleForClass:[self class]];
NSString *path = [testBundle pathForResource:@"fetch-user-success"
ofType:@"xml"
inDirectory:@"http-mock-responses"];
// Fake out Cocoa's URL loading system such that it will return an HTTP response as
// defined in our XML file for any GET requests to: http://example.com/fp/users. And,
// simulate a request latency of 5 seconds.
NSError *err;
NSStringEncoding encoding;
[PEHttpResponseSimulator simulateResponseFromXml:[NSString stringWithContentsOfFile:path
usedEncoding:&encoding
error:&err]
requestLatency:5.0
responseLatency:0];
(每个库均作为一个 CocoaPod 启用的 iOS 静态库实现。)