GithubHelp home page GithubHelp logo

hippolyte's Introduction

Hippolyte

Build Status codecov Version License Platform

An HTTP stubbing library written in Swift.

Requirements

  • Swift 4.2
  • iOS 9.3+
  • Xcode 10+

Install

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

pod 'Hippolyte'

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.

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

There are convenient Builder classes for both requests and responses:

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

Remember to tear down stubbing in your tests:

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

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

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.