Hippolyte 1.4.1

Hippolyte 1.4.1

测试已测试
语言语言 SwiftSwift
许可 MIT
发布最后发布2024年7月
SPM支持SPM

Jan GormanCornelius Behrend维护。



Hippolyte 1.4.1

  • 作者:
  • Jan Gorman

Hippolyte

Run tests codecov Version License Platform

使用Swift编写的HTTP补丁库。

要求

  • Swift 5
  • iOS 12+
  • macOS 10.13+

安装

Cocoapods

Hippolyte可以在Cocoapods上使用。将其添加到Podfile的测试目标中。

pod 'Hippolyte'

carthage

Hippolyte还可在Carthage上使用。在你的Cartfile中加入以下条目

github "JanGorman/Hippolyte"

然后运行carthage update

将Hippolyte.framework加入链接二进制与库

您需要执行一些额外的步骤。请参阅此处

用法

要模拟网络请求,首先需要创建一个StubRequestStubResponse。然后注册模拟并调用其start()方法以截获网络请求。

为请求和响应提供了方便的Builder类

func testStub() {
  // The stub response
  let response = StubResponse.Builder()
    .stubResponse(withStatusCode: 204)
    .addHeader(withKey: "X-Foo", value: "Bar")
    .build()
  // The request that will match this URL and return the stub response
  let request = StubRequest.Builder()
    .stubRequest(withMethod: .GET, url: URL(string: "http://www.apple.com")!)
    .addResponse(response)
    .build()
  // Register the request
  Hippolyte.shared.add(stubbedRequest: request)
  // And start intercepting requests by calling start
  Hippolyte.shared.start()
  
}

另外,您也可以直接构建它们

func testStub() {
  let url = URL(string: "http://www.apple.com")!
  var stub = StubRequest(method: .GET, url: url)
  var response = StubResponse()
  let body = "Hippolyte".data(using: .utf8)!
  response.body = body
  stub.response = response
  Hippolyte.shared.add(stubbedRequest: stub)
  Hippolyte.shared.start()

  let expectation = self.expectation(description: "Stubs network call")
  let task = URLSession.shared.dataTask(with: url) { data, _, _ in
    XCTAssertEqual(data, body)
    expectation.fulfill()
  }
  task.resume()

  wait(for: [expectation], timeout: 1)
}

您还可以配置StubRequest使用正则表达式匹配器来截获URL。以下示例还展示了一个返回特定状态码的StubResponse

func testStub() throws {
  let regex = try NSRegularExpression(pattern: "http://www.google.com/+", options: [])
  var stub = StubRequest(method: .GET, urlMatcher: RegexMatcher(regex: regex))
  stub.response = StubResponse(statusCode: 404)
  Hippolyte.shared.add(stubbedRequest: stub)
  Hippolyte.shared.start()

  myFictionalDataSource.get(URL(string: "http://www.google.com/foo.html")!) {
    
  }
}

要匹配在体中发送的POST请求,Hippolyte使用Matcher。有现成的DataMatcherJSONMatcher类可供使用。假设您正在将JSON发送到服务器,您可以让您的模拟匹配特定值,如下所示

struct MyPostBody: Codable, Hashable {
  let id: Int
  let name: String
}

func testStub() throws {
  // The POST body that you want to match
  let body = MyPostbody(id: 100, name: "Tim")
  let matcher = JSONMatcher<MyPostBody>(object: body)
  // Construct your stub response
  let response = StubResponse.Builder()
    .stubResponse(withStatusCode: 204)
    .build()
  // The request that will match the URL and the body JSON
  let request = StubRequest.Builder()
    .stubRequest(withMethod: .POST, url: URL(string: "http://www.apple.com")!)
    .addMatcher(matcher)
    .addResponse(response)
    .build()
}

请记得在测试中清除模拟

override func tearDown() {
  Hippolyte.shared.stop()
  super.tearDown()
}

您可以通过多种方式配置您的模拟响应,如使其返回不同的HTTP状态码、标题和错误。

许可

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