OHHTTPStubs 0,4,0,6,-1,3,0,1 travis-ci CocoaPods

Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers!

OHHTTPStubs

Platform Version Carthage Swift 3.0/3.1 Build Status Language: Swift-2.<sup>3</sup>⁄<sub>3</sub>.0/3.1

OHHTTPStubs is a library designed to stub your network requests very easily. It can help you:

  • test your apps with fake network data (stubbed from file) and simulate slow networks, to check your application behavior in bad network conditions
  • write unit tests that use fake network data from your fixtures.

It works with NSURLConnection, NSURLSession, AFNetworking, Alamofire or any networking framework that use Cocoa’s URL Loading System.

Donate


Documentation & Usage Examples

OHHTTPStubs headers are fully documented using Appledoc-like / Headerdoc-like comments in the header files. You can also read the online documentation here.

Basic example

In Objective-C

[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
  return [request.URL.host isEqualToString:@"mywebservice.com"];
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
  // Stub it with our "wsresponse.json" stub file (which is in same bundle as self)
  NSString* fixture = OHPathForFile(@"wsresponse.json", self.class);
  return [OHHTTPStubsResponse responseWithFileAtPath:fixture
            statusCode:200 headers:@{@"Content-Type":@"application/json"}];
}];

In Swift

This example is using the Swift helpers found in OHHTTPStubsSwift.swift provided by the OHHTTPStubs/Swift subspec.

stub(isHost("mywebservice.com")) { _ in
  // Stub it with our "wsresponse.json" stub file (which is in same bundle as self)
  let stubPath = OHPathForFile("wsresponse.json", type(of: self))
  return fixture(filePath: stubPath!, headers: ["Content-Type":"application/json"])
}

Note: if you’re using OHHTTPStubs’s Swiftier API (OHHTTPStubsSwift.swift and the Swift subspec), you can also compose the matcher functions like this: stub(isScheme("http") && isHost("myhost")) { … }

More examples & Help Topics

Recording requests to replay them later

Instead of writing the content of the stubs you want to use manually, you can use tools like SWHttpTrafficRecorder to record network requests into files. This way you can later use those files as stub responses.
This tool can record all three formats that are supported by OHHTTPStubs (the HTTPMessage format, the simple response boby/content file, and the Mocktail format).

(There are also other ways to perform a similar task, including using curl -is <url> >foo.response to generate files compatible with the HTTPMessage format, or using other network recording libraries similar to SWHttpTrafficRecorder).

Compatibility

  • OHHTTPStubs is compatible with iOS5+, OS X 10.7+, tvOS.
  • OHHTTPStubs also works with NSURLSession as well as any network library wrapping them.
  • OHHTTPStubs is fully compatible with Swift 2.2, 2.3, 3.0 and 3.1.

Nullability annotations have also been added to the ObjC API to allow a cleaner API when used from Swift even if you don’t use the dedicated Swift API wrapper provided by OHHTTPStubsSwift.swift.

Swift 2.2 users

If you’re still building for Swift 2.2, you will have some extraneous '_' in parameter warnings. Those are normal: it’s because the code has already done the transition to Swift 3 — which requires those _ in parameters while Swift 2.2 didn’t.

You can safely ignore those warnings in Swift 2.2. See SE-0046 for more info.

Carthage users using Swift 2.x

If you’re using Carthage, we don’t do Swift-2.3-specific branches anymore (too much maintenance work and most people have migrated already anyway) but if you still need Swift 2.3 compatibility, you can follow the tips in the installation instructions below to force Carthage to build this library with Swift 2.3.

Installing in your projects

CocoaPods

Using CocoaPods is the recommended way.

  • If you intend to use OHHTTPStubs from Objective-C only, add pod 'OHHTTPStubs' to your Podfile.
  • If you intend to use OHHTTPStubs from Swift, add pod 'OHHTTPStubs/Swift' to your Podfile instead.
pod 'OHHTTPStubs/Swift' # includes the Default subspec, with support for NSURLSession & JSON, and the Swiftier API wrappers

All available subspecs

OHHTTPStubs is split into subspecs so that when using Cocoapods, you can get only what you need, no more, no less.

  • The default subspec includes NSURLSession, JSON, and OHPathHelpers
  • The Swift subspec adds the Swiftier API to that default subspec
  • HTTPMessage and Mocktail are opt-in subspecs: list them explicitly if you need them
  • OHPathHelpers doesn’t depend on Core and can be used independently of OHHTTPStubs altogether

List of all the subspecs & their dependencies

Here’s a list of which subspecs are included for each of the different lines you could use in your Podfile:

Subspec Core NSURLSession JSON Swift OHPathHelpers HTTPMessage Mocktail
pod 'OHHTTPStubs'
pod 'OHHTTPStubs/Default'
pod 'OHHTTPStubs/Swift'
pod 'OHHTTPStubs/Core'
pod 'OHHTTPStubs/NSURLSession'
pod 'OHHTTPStubs/JSON'
pod 'OHHTTPStubs/OHPathHelpers'
pod 'OHHTTPStubs/HTTPMessage'
pod 'OHHTTPStubs/Mocktail'

Carthage

OHHTTPStubs is also be compatible with Carthage. Just add it to your Cartfile.

Note: The OHHTTPStubs.framework built with Carthage will include all features of OHHTTPStubs turned on (in other words, all subspecs of the pod), including NSURLSession and JSON support, OHPathHelpers, HTTPMessage and Mocktail support, and the Swiftier API.

Be warned that I don’t personally use Carthage, so I won’t be able to guarantee much help/support for it.

Using the right Swift version for your project

OHHTTPStubs supports Swift 2.2 (Xcode 7), Swift 2.3 (Xcode 8), Swift 3.0 (Xcode 8+) and Swift 3.1 (Xcode 8.3+)

Related Repositories

OHHTTPStubs

OHHTTPStubs

Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers! ...

AFNetworkingDemo

AFNetworkingDemo

有关iOS使用网络的Demo,使用AFNetworking, SDWebimage和OHHTTPStubs ...

OHHTTPStubs

OHHTTPStubs

Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers! ...

OHHTTPStubs-NetworkCapture

OHHTTPStubs-NetworkCapture

NetworkCapture for OHHTTPStubs. Easily capture and recreate http content. ...


Top Contributors

AliSoftware corinnekrych MaxGabriel JinlianWang Liquidsoul ikesyo kcharwood jzucker2 indragiek ndonald2 tiagomartinho attheodo ishkawa NSProgrammer tibr mxcl Ashton-W hpique hq-mobile JeanAzzopardi DerLobi shagedorn KyleLeneau adurdin cdzombak amleszk felixLam jschmid DarkKilauea kylef

Releases

-   5.2.1 zip tar
-   5.2.1-swift3 zip tar
-   5.2.0 zip tar
-   5.2.0-swift3 zip tar
-   5.1.0 zip tar
-   5.0.0 zip tar
-   4.8.0 zip tar
-   4.7.1 zip tar
-   4.7.0 zip tar
-   4.6.0 zip tar
-   4.5.1 zip tar
-   4.5.0 zip tar
-   4.4.0 zip tar
-   4.3.0 zip tar
-   4.2.1 zip tar
-   4.2.0 zip tar
-   4.1.0 zip tar
-   4.0.2 zip tar
-   4.0.1 zip tar
-   4.0.0 zip tar
-   3.1.12 zip tar
-   3.1.11 zip tar
-   3.1.10 zip tar
-   3.1.9 zip tar
-   3.1.8 zip tar
-   3.1.7 zip tar
-   3.1.6 zip tar
-   3.1.5 zip tar
-   3.1.4 zip tar
-   3.1.3 zip tar