Velociraptor 是一个纯 Swift 编写的网络存根框架,用于 iOS 和 OS X。
使用 CocoaPods 或 Carthage 将 Velociraptor 集成到您的测试目标中。其他方法不建议使用。
以下例子使用 Quick 进行描述。
由于 Velociraptor 使用了方法交换,您应该手动激活和关闭 Velociraptor 以避免出现奇怪的行为。
import Quick
import Nimble
import Velociraptor
class VelociraptorSpec: QuickSpec {
override func spec() {
beforeSuite {
Velociraptor.activate()
}
afterSuite {
Velociraptor.deactivate()
}
describe(“Stubbing requests”) {
// …
}
}
}
如果您使用 XCTest,请使用以下代码:
import Foundation
import XCTest
import Velociraptor
class VelociraptorTests: XCTestCase {
override class func setUp() {
super.setUp()
Velociraptor.activate()
}
override class func tearDown() {
Velociraptor.deactivate()
super.tearDown()
}
func testExample() {
// ...
}
}
您可以直接使用 String
、NSURL
和 NSURLRequest
来存根简单的 GET 请求。如果您未指定想要获取的任何响应,您将接收到一个具有状态码 200 和空正文数据的默认响应。
let URLString = "https://github.com/mrahmiao/velociraptor"
let URL = NSURL(string: URLString)!
let request = NSURLRequest(URL: URL)
// String
Velociraptor.request(URLString)
// NSURL
Velociraptor.request(URL)
// NSURLRequest
Velociraptor.request(request)
使用 VLRStubbedRequest
对象,您可以设置您想要存根的请求的完整条件。具有您设置的信息的请求将被存根。
let stubbedRequest = VLRStubbedRequest(URL: URL)
stubbedRequest.HTTPMethod = .GET
stubbedRequest.HTTPBody = "Hello World".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
stubbedRequest.HTTPHeaderFields = ["Auth": "jk1234"]
Velociraptor.request(stubbedRequest)
除了 GET 请求外,您可以使用在 RFC-7231 中定义的任意其他 HTTP 方法来存根请求。
Velociraptor.request(URL).requestHTTPMethod(.POST)
这里有 5 个便捷方法,您可以使用这些方法来存根最常用的 HTTP 方法。
Velociraptor.GET(URL)
Velociraptor.POST(URL)
Velociraptor.PUT(URL)
Velociraptor.DELETE(URL)
Velociraptor.PATCH(URL)
您可以选择同时代理一个或多个头部字段。
// Stubs one particuluar header field
Velociraptor.GET(URL)?.requestHeaderValue("*", forHTTPHeaderField: "Accept")
// Stubs multiple header fields
let headerFields = [
"Accept": "*",
"Other": "HeaderFields"
]
Velociraptor.GET(URL)?.requestHTTPHeaderFields(headerFields)
Velociraptor.VLRHeaderFieldMatchingOptions 列出了您在匹配头部字段时使用的选项。
public enum VLRHeaderFieldMatchingOptions {
case .Exactly
case .Partially(UInt)
case .StrictSubset
case .Arbitrarily
}
默认的头部字段匹配选项是 .Exactly
,表示只有当两个头部字段(接入请求和代理请求)完全相同,该请求才会匹配。
或您可以将选项设置为其他选项。
Velociraptor.headerFieldMatchingOption = .Partially(3)
Exactly
:表示接入请求和代理请求的头部字段必须完全相同。Partially(ValueError)
:使用关联的 ValueError
值指定接入请求和代理请求最小公共头部字段数。当公共头部字段数量大于或等于指定最小数量时,接入请求将匹配。注意,如果设置的数量为 0
,则此选项与 .Arbitrarily
相同。其最大值是代理头部字段的个数。StrictSubset
:只有当接入请求的头部字段是代理请求头部字段的严格子集时,匹配才会成功。Arbitrarily
:匹配具有任意头部字段的请求。设置HTTP请求体时,将在代理请求的头部字段中添加 Content-Length。
let data = "Hello World".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
Velociraptor.request(URL)?.requestBodyData(data)
您可以代理一个或多个头部字段。
// Single Header Field
Velociraptor.GET(URL)?.responseHeaderValue("application/json", forHTTPHeaderField: "Content-Type")
// Multiple Header Fields
let headerFields = [
"Content-Type": "application/json",
"ReceivedRes": "Response"
]
Velociraptor.GET(URL)?.responseHTTPHeaderFields(headerFields)
使用此方法指定完成处理程序中想要接收的状态码。
Velociraptor.GET(URL)?.responseStatusCode(404)
如果要将错误响应代理,请使用以下方法:
let errorCode = 1234
let userInfo = ["Error": "StubbedError"]
let domain = "com.code4blues.mrahmiao.velociraptor"
let stubbedError = NSError(domain: domain, code: errorCode, userInfo: userInfo)
Velociraptor.request(URL)?.failWithError(stubbedError)
您可以将 NSData
作为响应体数据代理。
let data = "Hello World".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
Velociraptor.request(URL)?.responseBodyData(data)
或使用JSON对象。
let JSONObject: AnyObject = [
"Number": 5,
"Bool": false,
"Array": [5, 4, 3]
]
Velociraptor.request(URL)?.responseBodyData(JSONObject)
在代理JSON数据时,Content-Type 将设置为 application/json; charset=utf-8。
假设我想代理一个POST请求
let URL = "http://httpbin.org/post"
let requestHeader = [
"Authorization": "3721"
]
let requestData = "Hello World".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let responseHeaders = [
"Status": "Success"
"uID": "3214"
]
Velociraptor.POST(URL)?
.requestHeaderFields(requestHeader)
.requestBodyData(requestData)
.responseStatusCode(200)
.responseHTTPHeaderFields(responseHeaders)
您可以使用单个VLRStubbedResponse对象来代理响应,而不是使用一系列方法。
let statusCode = 201
let bodyData = "Hello World".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
let expectedResponse = VLRStubbedResponse(URL: URL, statusCode: statusCode)
expectedResponse.HTTPHeaderFields = [
"Content-Type": "text/plain",
"Content-Length": String(bodyData.length)
]
expectedResponse.HTTPBody = bodyData
Velociraptor.request(URL)?.response(expectedResponse)
任何未被代理的请求都会导致致命错误。
Velociraptor 在MIT许可下发布。有关详细信息,请参阅 LICENSE
文件。