ohhttpstubs

About ohhttpstubs

OhHttpStubs is a library for stubbing and recording HTTP(S) network requests. It provides an easy way to simulate different network scenarios and test your application’s behavior under various conditions.

Key Features

  • Automatic Stubbing: Easily stub and intercept network requests made by your application.
  • Customizable Responses: Define custom responses for stubbed requests, including status codes, headers, and body.
  • Network Recording: Record network requests and responses for offline testing or debugging.
  • Synchronization with URLSession: Seamlessly integrates with URLSession to stub requests made through the network stack.
  • Dynamic Behavior: Customize the behavior of stubbed requests based on dynamic conditions or criteria.
  • Easy-to-Use: Simple and intuitive API for stubbing and intercepting network traffic.

Installation

You can install OhHttpStubs through Cocoapods. Just add the following line to your Podfile:

pod 'OHHTTPStubs'

Then run the command:

pod update

Usage

Using OhHttpStubs is straightforward. Here’s an example of how to stub a network request:

import OHHTTPStubs

OHHTTPStubs.stubRequests(passingTest: { request in
    return request.url?.host == "api.example.com"
}) { _ in
    let stubPath = OHPathForFile("response.json", type(of: self))
    return fixture(filePath: stubPath, headers: ["Content-Type":"application/json"])
}

This will stub all network requests made to api.example.com and return the contents of the response.json file as the response.

API Reference

For detailed documentation on how to use OhHttpStubs, refer to the official API Reference:

API Reference