• Stars
    star
    111
  • Rank 314,510 (Top 7 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 7 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

HTTP Stubbing in Swift

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

More Repositories

1

Agrume

πŸ‹ A lemony fresh iOS image viewer written in Swift.
Swift
790
star
2

MapleBacon

🍁πŸ₯“ Lightweight and fast Swift library for image downloading, caching and transformations
Swift
342
star
3

Chester

Chester is a Swift GraphQL query builder.
Swift
100
star
4

Table

CLI tables in Swift
Swift
55
star
5

SwiftMessageBar

An iOS message bar, written in Swift
Swift
52
star
6

uppercrust

Convert your JSON schema files to Mantle compatible Objective-C models
Ruby
24
star
7

JGORegExpBuilder

A delightful regular expressions DSL
Objective-C
12
star
8

node-table

node.js Text Table Renderer. No longer maintained. Development has moved to https://github.com/gajus/table
CoffeeScript
10
star
9

AVContentProposal-Sample

How to use AVContentProposalViewController
Swift
10
star
10

Waxwing

iOS version migrations 🦀
Swift
7
star
11

jersey-passbook

A Java implementation of the Apple Passbook REST API running on Jersey
Java
7
star
12

Swift-Design-Patterns

Design patterns applied in Swift. Swift 1 in 2014.
Swift
6
star
13

CRDT

A toy implementation of conflict-free replicated data types in Swift
Swift
4
star
14

Bling

An Open Exchange Rates API wrapper written in Swift
Swift
4
star
15

Zoetrope

Animated gif image view with support for varying frame lengths written in Swift.
Swift
3
star
16

ClosureTask

A Closure Task for phing
PHP
3
star
17

Personal-Jira

Your personal Jira search
CoffeeScript
2
star
18

my-boxen

My boxen
Ruby
1
star
19

VerbalExpressions

Objective-C Verbal Expressions
Objective-C
1
star
20

swiftlint

JavaScript
1
star
21

SwiftlintTestRunner

Test project to execute https://github.com/JanGorman/swiftlint
Swift
1
star
22

ember-by-example

Ember.js integration with Dropwizard
JavaScript
1
star
23

ChampagneBrunch

Swift
1
star
24

HARParser

A Swift .har parser
Swift
1
star
25

Sorte

Sort a directory of mp3s into folders base on their tags
Ruby
1
star
26

SwiftMosaicLayout

A mosaic UICollectionViewLayout written in Swift
Swift
1
star
27

puppet-simpholders

SimPholders Puppet Module for Boxen
Ruby
1
star