kylef / Mockingjay

An elegant library for stubbing HTTP requests with ease in Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Find out if URL was requested

pimnijman opened this issue · comments

Feature request:

I'd like to know whether my stubbed URL is requested.
I imagine this could work something like this:

let body = [ "description": "Kyle" ]
let userStub = stub(http(.PUT, uri: "/kylef/Mockingjay"), json(body))

expect(userStub.requested).toEventually(beTrue())

Since this doesn't work, I came up with a solution using a custom matcher:

///  Creates a custom matcher that calls the requestMatchedHandler when a request was succesfully matched
func matcher(_ method: HTTPMethod, uri: String, requestMatchedHandler: @escaping () -> Void) -> (_ request: URLRequest) -> Bool {
    let matcher = http(method, uri: uri)
    return { (request: URLRequest) in
        let result = matcher(request)
        if result {
            requestMatchedHandler()
        }
        return result
    }
}

var requestMatched = true

let body = [ "description": "Kyle" ]
stub(http(.PUT, uri: "/kylef/Mockingjay", requestMatchedHandler: { () in
    requestMatched = true
}), json(body))

expect(requestMatched).toEventually(beTrue())