Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Request block

9 views
Skip to first unread message

Chris Schumann

unread,
Jun 16, 2018, 4:50:19 AM6/16/18
to webmock-users
Hi folks,

I did a quick search of this email list history and found nothing useful so far, but please direct me if I missed something.

I want to do a stub_request with a block that allows for checking if an expected time is within a certain range of "right now", but I'm having a terrible time coming up with an incantation that will let me do that. Really, I expect any sufficiently complex example would be helpful, so any links to examples, or maybe some explanation of how the thing works, would be very much appreciated.

Many thanks,
Chris

Bartosz Blimke

unread,
Jun 16, 2018, 4:51:56 AM6/16/18
to webmoc...@googlegroups.com
Hi Chris,

Could you please be more precise and explain what you mean by "expected time"? Expected request parameter with a string value representing time?

Bartosz

--

---
You received this message because you are subscribed to the Google Groups "webmock-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webmock-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Schumann

unread,
Jun 18, 2018, 9:41:21 AM6/18/18
to webmock-users
Thanks for the add and the reply. I managed to muddle through it, and here's what worked for me:

stub_request(:head, 'http://localhost:port/endpoint').
with(
query: hash_including(fixed_params),
    headers: {
'Accept' => '*/*',
'Authorization' => 'Basic SECRET'
}
) do |request|
time_location = request.uri.query.index('occurred_since=')
requested_time = Time.zone.parse(request.uri.query[time_location..-1])
(occurred_since - requested_time).abs < 2
end.
to_return(status: 200, headers: headers)

This asserts the time in the query is within 2 seconds of some expected time that gets passed in.
Chris
Reply all
Reply to author
Forward
0 new messages