Easily way to write a stub for the network request
Please call the MockProtocol.register() when app is launched.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
MockProtocol.register()
return true
}You can create a stub with the Mock.install(). And using request and response to defined the content.
Mock.install().request(url: "/").response(200, body: nil, header: nil)Write a network request using the usual NSURLSession or Alamofire.
let request = NSURLRequest(URL: NSURL(string: "/")!)
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
session.dataTaskWithRequest(request) { data, response, error in
let httpResponse = response as! NSHTTPURLResponse
// Status code is 200 !!!
ex.fulfill()
}.resume()Alamofire.request(.GET, "/").response { _, response, _, _ in
// Status code is 200 !!!
}Pass the JSON to the body.
let json: [String: AnyObject] = ["name": "yukiasai", "age": 28]
Mock.install().request(url: url).response(200, body: JSON(json), header: nil)let fileUrl: NSURL = NSURL(string: "path/to/file")!
Mock.install().request(url: url).response(200, body: File(fileUrl), header: nil)You can also write the responseHandler.
Mock.install().request(url: someUrl).response { request -> Response in
let response = NSHTTPURLResponse(URL: request.URL!, statusCode: 200, HTTPVersion: nil, headerFields: nil)!
let data = "12345".dataUsingEncoding(NSUTF8StringEncoding)
return .Success(response, data)
// or return .Failure(error)
}You can control speed by using the speed(). Speed will change depending on the size of the http-body data.
Mock.install().request(url: url).response(200, body: File(fileUrl), header: nil).speed(.Wifi)It supports the following speed.
public enum Speed {
case Prompt
case Wifi // 45Mbps
case Mobile4G // 10Mbps
case Mobile3G // 1Mbps
case Edge // 200kbps
}Kagee is released under the MIT license. See LICENSE for details.