import Mockingjay

func api(method: HTTPMethod, _ uri: String, token: String)(request: NSURLRequest) -> Bool {
    guard let headers = request.allHTTPHeaderFields,
        authorization = headers["Authorization"] where
        authorization == "Bearer \(token)"
    else {
        return false
    }
    
    return api(method, uri)(request: request)
}

func api(method: HTTPMethod, _ uri: String)(request: NSURLRequest) -> Bool {
    guard let headers = request.allHTTPHeaderFields,
        accept = headers["Accept"],
        acceptEncoding = headers["Accept-Encoding"],
        contentType = headers["Content-Type"] where
        accept == "application/json" &&
        contentType == "application/json" &&
        acceptEncoding == "gzip;q=1.0,compress;q=0.5"
    else {
        return false
    }
    
    return http(method, uri: uri)(request: request)
}

func api(method: HTTPMethod, _ uri: String, body: [String: AnyObject])(request: NSURLRequest) -> Bool {
    let bodyJson = try? NSJSONSerialization.dataWithJSONObject(body, options: NSJSONWritingOptions())
    guard bodyJson == request.HTTPBody else { return false }
    return api(method, uri)(request: request)
}

import Mockingjay

func api(method: HTTPMethod, _ uri: String, token: String)(request: NSURLRequest) -> Bool {
    guard let headers = request.allHTTPHeaderFields,
        authorization = headers["Authorization"] where
        authorization == "Bearer \(token)"
    else {
        return false
    }
    
    return api(method, uri)(request: request)
}

func api(method: HTTPMethod, _ uri: String)(request: NSURLRequest) -> Bool {
    guard let headers = request.allHTTPHeaderFields,
        accept = headers["Accept"],
        acceptEncoding = headers["Accept-Encoding"],
        contentType = headers["Content-Type"] where
        accept == "application/json" &&
        contentType == "application/json" &&
        acceptEncoding == "gzip;q=1.0,compress;q=0.5"
    else {
        return false
    }
    
    return http(method, uri: uri)(request: request)
}

func api(method: HTTPMethod, _ uri: String, body: [String: AnyObject])(request: NSURLRequest) -> Bool {
    guard let bodyStream = request.HTTPBodyStream else { return false }
    
    bodyStream.open()
    
    guard let bodyStreamJsonObject = try? NSJSONSerialization.JSONObjectWithStream(bodyStream, options: NSJSONReadingOptions()) else {
        return false
    }
    
    let bodyStreamJsonData = try? NSJSONSerialization.dataWithJSONObject(bodyStreamJsonObject, options: NSJSONWritingOptions())
    let bodyJsonData = try? NSJSONSerialization.dataWithJSONObject(body, options: NSJSONWritingOptions())
    
    guard bodyStreamJsonData == bodyJsonData else { return false }
    
    return api(method, uri)(request: request)
}

Fix Mockingjay matcher to workaround issue with HTTPBody.
Fix Mockingjay matcher to workaround issue with HTTPBody.
