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.
- 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.
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.
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 add the file into your Xcode project. Slightly simpler, but updates are also manual.
Wojtek Lukaszuk @wojteklu
WebMock is available under the MIT license. See the LICENSE file for more info.