Skip to content

Instantly share code, notes, and snippets.

@omerlh
Created February 23, 2016 14:25
Show Gist options
  • Save omerlh/2617db3d4116bf46bdeb to your computer and use it in GitHub Desktop.
Save omerlh/2617db3d4116bf46bdeb to your computer and use it in GitHub Desktop.
//
// MockURLProtocol.swift
// SolutoHome
//
// Created by Omer Levi Hevroni on 2/23/16.
// Copyright © 2016 Soluto. All rights reserved.
//
import Foundation
/*
Ispired by alamofire
See: https://github.com/Alamofire/Alamofire/blob/master/Tests/URLProtocolTests.swift
*/
class MockURLProtocol : NSURLProtocol {
enum ResponseType{
case Error(NSError)
case Success(NSURLResponse)
}
internal static var lastRequest: NSURLRequest?
static var responseType: ResponseType!
// MARK: Properties
struct PropertyKeys {
static let HandledByForwarderURLProtocol = "HandledByProxyURLProtocol"
}
lazy var session: NSURLSession = {
let configuration: NSURLSessionConfiguration = {
let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
return configuration
}()
let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)
return session
}()
var activeTask: NSURLSessionTask?
// MARK: Class Request Methods
override class func canInitWithRequest(request: NSURLRequest) -> Bool {
if NSURLProtocol.propertyForKey(PropertyKeys.HandledByForwarderURLProtocol, inRequest: request) != nil {
return false
}
return true
}
override class func canonicalRequestForRequest(request: NSURLRequest) -> NSURLRequest {
return request
}
override class func requestIsCacheEquivalent(a: NSURLRequest, toRequest b: NSURLRequest) -> Bool {
return false
}
// MARK: Loading Methods
override func startLoading() {
let mutableRequest = request.URLRequest
NSURLProtocol.setProperty(true, forKey: PropertyKeys.HandledByForwarderURLProtocol, inRequest: mutableRequest)
activeTask = session.dataTaskWithRequest(mutableRequest)
activeTask?.resume()
}
override func stopLoading() {
activeTask?.cancel()
}
}
// MARK: -
extension MockURLProtocol: NSURLSessionDelegate {
// MARK: NSURLSessionDelegate
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
client?.URLProtocol(self, didLoadData: data)
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
MockURLProtocol.lastRequest = task.currentRequest
switch(MockURLProtocol.responseType!){
case let ResponseType.Error(err):
client?.URLProtocol(self, didFailWithError: err)
break
case let ResponseType.Success(response):
client?.URLProtocol(self, didReceiveResponse: response, cacheStoragePolicy: .NotAllowed)
break
}
client?.URLProtocolDidFinishLoading(self)
}
}
extension MockURLProtocol {
static func responseWithFailure(){
MockURLProtocol.responseType = ResponseType.Error(NSError(domain: NSURLErrorDomain, code: NSURLErrorFileDoesNotExist, userInfo: nil))
}
static func responseWithStatusCode(code : Int){
MockURLProtocol.responseType = ResponseType.Success(NSHTTPURLResponse.init(URL: NSURL(fileURLWithPath: "http://www.google.com"), statusCode: code, HTTPVersion: nil, headerFields: nil)!)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment