markandrewj / WebMock

HTTP stubbing for iOS and macOS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Webmock

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

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.

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.

About

HTTP stubbing for iOS and macOS

License:MIT License


Languages

Language:Swift 92.3%Language:Objective-C 3.1%Language:Ruby 3.1%Language:Shell 1.5%