Latest 2.0
Homepage https://github.com/wojteklu/WebMock
License MIT
Platforms ios 8.0, osx 10.9, requires ARC
Authors

Webmock

Language
Build Status codecov Platform support CocoaPods Compatible Carthage compatible License MIT

HTTP/HTTPS requests stubbing for iOS and macOS. It works with NSURLSession, AFNetworking, Alamofire or any networking framework that use Cocoa’s URL Loading System.

Features

  • Stub HTTP and HTTPS requests.
  • Stub requests with errors.
  • Define response with NSData, JSON object or file URL.
  • Works with AFNetworking, Alamofire and more.
  • Stub only requests you define.
  • Tested.

Usage

WebMock requires using session configuration’s replacements instead of default ones.

let defaultConfiguration = NSURLSessionConfiguration.webmockDefaultSessionConfiguration()

let ephemeralConfiguration = NSURLSessionConfiguration.webmockEphemeralSessionConfiguration()

Stub requests with NSData

let data = "test".dataUsingEncoding(NSUTF8StringEncoding)
let response = Response(data: data)

let URL = NSURL(string: "https://www.google.com")!
let stub = Stub(method: .GET, URL: URL, response: response)

WebMock.startWithStubs([stub])

Stub requests with a JSON object

let json = ["testKey": "testValue"]
let response = Response(json: json)

let URL = NSURL(string: "https://www.google.com")!
let stub = Stub(method: .GET, URL: URL, response: response)

WebMock.startWithStubs([stub])

Stub requests with a file

let fileURL = bundle.pathForResource("test", ofType: "json")!
let response = Response(fileURL: fileURL)

let URL = NSURL(string: "https://www.google.com")!
let stub = Stub(method: .GET, URL: URL, response: response)

WebMock.startWithStubs([stub])

Stub requests with failure

let error = NSError(domain: NSInternalInconsistencyException,
                            code: 0,
                            userInfo: nil)        
let response = Response(error: error)

let URL = NSURL(string: "https://www.google.com")!
let stub = Stub(method: .GET, URL: URL, response: response)

WebMock.startWithStubs([stub])

You can additionaly define response’s header fields and http method.

Installation

Carthage

Add the following to your Cartfile:

github "wojteklu/WebMock"

Then run carthage update.

Follow the current instructions in Carthage’s README
for up to date installation instructions.

CocoaPods

Add the following to your Podfile:

pod 'WebMock'

You will also need to make sure you’re opting into using frameworks:

use_frameworks!

Then run pod install.

Manually

Manually add the file into your Xcode project. Slightly simpler, but updates are also manual.

Author

Wojtek Lukaszuk @wojteklu

License

WebMock is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "WebMock",
    "version": "2.0",
    "license": "MIT",
    "summary": "HTTP stubbing for iOS and Mac OS X",
    "homepage": "https://github.com/wojteklu/WebMock",
    "authors": {
        "Wojtek Lukaszuk": "[email protected]"
    },
    "source": {
        "git": "https://github.com/wojteklu/WebMock.git",
        "tag": "2.0"
    },
    "requires_arc": true,
    "platforms": {
        "ios": "8.0",
        "osx": "10.9"
    },
    "source_files": "WebMock/*.swift"
}

Pin It on Pinterest

Share This