Skip to content

How is Hippolyte different from X

Jan Gorman edited this page Oct 10, 2017 · 2 revisions

OHHTTPStubs

OHHTTPStubs has been around for a long while. It's written in Objective-C but provides Swift helper methods to make using it with Swift more enjoyable. Hippolyte is Swift from day one.

Usage

OHHTTPStubs follows a block based approach for stubbing requests in the form of

stub(isHost("foo")) { _ in
  return OHHTTPStubsResponse()
}

Hippolyte takes a more "traditional" approach of creating a StubRequest, adding a StubResponse to each request and registering this with the shared Hippolyte instance:

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 also comes with Builder classes for both stubbed requests and responses which read quite nicely:

func testStub() {
    let response = StubResponse.Builder()
        .stubResponse(withStatusCode: 204)
        .addHeader(withKey: "X-Foo", value: "Bar")
        .build()
    let request = StubRequest.Builder()
        .stubRequest(withMethod: .GET, url: URL(string: "http://www.apple.com")!)
        .addResponse(response)
        .build()
    // More code
}

This also highlights a difference over more fine grained control what type of request you want to match: You control which method is matched and also how to match URLs. Either use a fixed URL or a regular expression if you're stubbing different endpoints.

StubResponse can return any status code you need to test, different types of headers, etc.

It's up to you which you prefer or find more readable.

Limitations

OHHTTPStubs can stub both URLConnection and URLSession. Hippolyte focuses just on URLSession, it's the future.

Clone this wiki locally