Reputation: 35
Question on how to check for a desired 401 REST response with Cucumber.
Items of note: - This is not a Rails app. - Cucumber version is 1.3.17 - I'm using rest_client 1.7.3: https://github.com/rest-client/rest-client - I have steps:
When a GET request is made with no token
Then status code should be '401'
And definitions:
When /^a GET request is made with no token$/ do
@response = RestClient.get "http://<hidden>"
end
Then /^status code should be '(\d+)'$/ do |code|
expect(@response.code).to eq(code.to_i)
end
The "Then" works fine for a '200' response, but I'm stopped from testing for a 401 because the run errors out on the "When" with, you guessed it, a 401 error.
I've searched all over the interwebs for suggestions. I've tried the @allow-rescue tag before my scenario (which seems to be a RAILS thing - but I tried it anyway.) I haven't found a way to do a 401 test.
Anyone have any ideas (other than looking at the failures in the test report and saying 'yup, those are supposed to fail'?)
Thanks, Ed
Upvotes: 1
Views: 2591
Reputation: 156
Looking at the documentation for RestClient: http://www.rubydoc.info/gems/rest-client/1.6.7/frames
You could either run it in a try/catch block:
begin
RestClient.get 'http://example.com/resource'
rescue => e
e.response
end
You can pass a parameter to the RestClient that will have it return the exception instead.
RestClient.get('http://example.com/resource'){|response, request, result| response }
Upvotes: 1