Mockingjay icon indicating copy to clipboard operation
Mockingjay copied to clipboard

Compare query parameters (ignoring order)

Open marcelofabri opened this issue 9 years ago • 0 comments

I think it'd be useful to have a matcher (or change uri) to compare query parameters ignoring their order. I was able to implement that with a custom matcher:

func uriWithQuery(uriString:String) -> (request:NSURLRequest) -> Bool {
    return { (request:NSURLRequest) in
        if uri(uriString)(request: request) {
            return true
        }

        if let components = NSURLComponents(string: uriString),
            requestComponents = request.URL.flatMap({ NSURLComponents.init(URL: $0, resolvingAgainstBaseURL: false)}) {
            let componentsWithoutParams = components.copy() as! NSURLComponents
            componentsWithoutParams.query = nil

            let requestComponentsWithoutParams = requestComponents.copy() as! NSURLComponents
            requestComponentsWithoutParams.query = nil

            if requestComponentsWithoutParams.URL == componentsWithoutParams.URL {
                return requestComponents.queryItems.flatMap(Set.init) == components.queryItems.flatMap(Set.init)
            }
        }

        return false
    }
}

Note that this probably doesn't support path templates.

marcelofabri avatar Sep 06 '16 21:09 marcelofabri