All Projects → JanGorman → Hippolyte

JanGorman / Hippolyte

Licence: MIT License
HTTP Stubbing in Swift

Programming Languages

swift
15916 projects
ruby
36898 projects - #4 most used programming language
c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to Hippolyte

SwiftGradients
Useful extensions for UIViews and CALayer classes to add beautiful color gradients.
Stars: ✭ 15 (-86.24%)
Mutual labels:  carthage, swift-package-manager, spm, swift5
Table
CLI tables in Swift
Stars: ✭ 53 (-51.38%)
Mutual labels:  swift-package-manager, spm, swift5, swift-5
aem-stubs
Tool for providing sample data for AEM applications in a simple and flexible way. Stubbing server on AEM, no separate needed.
Stars: ✭ 40 (-63.3%)
Mutual labels:  mock, stub, mocking, stubbing
Ohhttpstubs
Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers!
Stars: ✭ 4,831 (+4332.11%)
Mutual labels:  mock, stub, mocking, stubbing
Mockery
Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its core goal is to offer a test double framework with a succinct API capable of clearly defining all possible object operations and interactions using a human readable Domain Specific Language (DSL).
Stars: ✭ 10,048 (+9118.35%)
Mutual labels:  mock, stub, mocking
Mocktopus
Mocking framework for Rust
Stars: ✭ 179 (+64.22%)
Mutual labels:  mock, test, mocking
stub-server
Stub server for REST APIs
Stars: ✭ 14 (-87.16%)
Mutual labels:  mock, test, stub
mockingbird
🐦 Decorator Powered TypeScript Library for Creating Mocks
Stars: ✭ 70 (-35.78%)
Mutual labels:  mock, test, mocking
Prig
Prig is a lightweight framework for test indirections in .NET Framework.
Stars: ✭ 106 (-2.75%)
Mutual labels:  mock, test, mocking
Tablier
A micro-framework for Table Driven Tests.
Stars: ✭ 33 (-69.72%)
Mutual labels:  test, swift-package-manager, xctest
TVToday
iOS TV Shows app with TMDb Api. RxSwift, MVVM, Clean Architecture. Tuist + Swift Package Manager
Stars: ✭ 27 (-75.23%)
Mutual labels:  swift-package-manager, spm, swift5
Spy
Clojure/ClojureScript library for stubs, spies and mocks.
Stars: ✭ 131 (+20.18%)
Mutual labels:  mock, stub, mocking
Nsubstitute
A friendly substitute for .NET mocking libraries.
Stars: ✭ 1,646 (+1410.09%)
Mutual labels:  mock, test, mocking
node-mock-examples
Examples of tests that mock Node system APIs: fs, http, child_process, timers
Stars: ✭ 38 (-65.14%)
Mutual labels:  mock, test, stub
laika
Log, test, intercept and modify Apollo Client's operations
Stars: ✭ 99 (-9.17%)
Mutual labels:  mock, test, mocking
BetterMappable
Better Mappable through Property Wrappers using ObjectMapper
Stars: ✭ 26 (-76.15%)
Mutual labels:  carthage, swift-package-manager, swift5
Match3Kit
Library for simple Match3 games.
Stars: ✭ 38 (-65.14%)
Mutual labels:  swift-package-manager, spm, swift5
Mockswift
MockSwift is a Mock library written in Swift.
Stars: ✭ 56 (-48.62%)
Mutual labels:  mock, test, swift-package-manager
Cuckoo
Boilerplate-free mocking framework for Swift!
Stars: ✭ 1,344 (+1133.03%)
Mutual labels:  mock, stub, mocking
PagedLists
Paginated UITableView and UICollectionViews for iOS.
Stars: ✭ 69 (-36.7%)
Mutual labels:  swift-package-manager, spm, swift5

Hippolyte

Run tests codecov Version License Platform

An HTTP stubbing library written in Swift.

Requirements

  • Swift 5
  • iOS 12+
  • macOS 10.13+

Install

Cocoapods

Hippolyte is available on Cocoapods. Add it to your Podfile's test target:

pod 'Hippolyte'

Carthage

Hippolyte is also available on Carthage. Make the following entry in your Cartfile:

github "JanGorman/Hippolyte"

Then run carthage update.

Add the Hippolyte.framework to the Link Binary with Libraries.

You'll need to go through some additional steps. Please see here.

Usage

To stub a request, first you need to create a StubRequest and StubResponse. You then register this stub with Hippolyte and tell it to intercept network requests by calling the start() method.

There are convenient Builder classes for both requests and responses:

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()
  
}

Alternatively you can also construct them directly:

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)
}

It's also possible to configure a StubRequest to use a regular expression matcher to intercept URLs. The following example also shows a StubResponse that returns a certain status code:

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")!) {
    
  }
}

To match a POST request on the body that's sent, Hippolyte uses a Matcher. There is a ready made DataMatcher and JSONMatcher class available to use. Say you're POSTing a JSON to your server, you could make your stub match a particular value like this:

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()
}

Delegate

You can add an optional ResponseDelegate into the mix to be notified when a response is mocked by Hippolyte. It has only one method onResponse.

Teardown

Remember to tear down stubbing in your tests:

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

You can configure your stub response in a number of ways, such as having it return different HTTP status codes, headers, and errors.

License

Hippolyte is released under the MIT license. See LICENSE for details

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].