WebMock 2.0

WebMock 2.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Oct 2016
SPMSupports SPM

Maintained by Wojtek Lukaszuk.



  • By
  • Wojtek Lukaszuk

Webmock

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

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.