Mockingjay icon indicating copy to clipboard operation
Mockingjay copied to clipboard

Proposal for callback for matcher

Open icanswiftabit opened this issue 8 years ago • 1 comments

I was thinking what would help me in testing. Callback when stub responds to request. I am more than happy to implement it. What do you think @kylef ?

icanswiftabit avatar Oct 25 '17 09:10 icanswiftabit

Both the matcher and builder are already callbacks, you are free to put whatever logic you want in those places. The "builder" is invoked when the stub is matched.

stub(http(.post, uri: "https://example.com/authorisations")) { request in
  XCTAssertEqual(request.allHTTPHeaderFields?["Authorization"], "Bearer abc123")
  XCTAssertEqual(request.allHTTPHeaderFields?["Content-Type"], "application/json")

  if denied {
    return json(["token": "newtokenabc"])(request)
  }

  denied = true
  return problem(code: 403, title: "Access Denied")(request)
}

kylef avatar Oct 04 '21 12:10 kylef