Skip to content

Compare query parameters (ignoring order) #61

Open

Description

@marcelofabri

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions