Giter Site home page Giter Site logo

hippolyte's Introduction

Hippolyte

Run tests codecov Version License Platform

An HTTP stubbing library written in Swift.

Requirements

  • Swift 5
  • iOS 9.3+
  • macOS 10.13+
  • Xcode 10.2+

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: stub)
  // 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()
}

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

hippolyte's People

Contributors

jangorman avatar dependabot-preview[bot] avatar davbeck avatar clemens-schulz avatar richardpineo avatar fumoboy007 avatar doronkatz avatar srekke avatar iamtomcat avatar tjnet avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.