mirror of
https://github.com/jnunemaker/httparty
synced 2023-03-27 23:23:07 -04:00
Add cucumber feature to test timeout option
This commit is contained in:
parent
0cbdb7dfbf
commit
3c208119e3
6 changed files with 28 additions and 5 deletions
|
@ -8,6 +8,7 @@ Before do
|
||||||
@host_and_port = "0.0.0.0:#{port}"
|
@host_and_port = "0.0.0.0:#{port}"
|
||||||
@server = Mongrel::HttpServer.new("0.0.0.0", port)
|
@server = Mongrel::HttpServer.new("0.0.0.0", port)
|
||||||
@server.run
|
@server.run
|
||||||
|
@request_options = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
After do
|
After do
|
||||||
|
|
|
@ -20,7 +20,7 @@ Then /it should return a response with a (\d+) response code/ do |code|
|
||||||
@response_from_httparty.code.should eql(code.to_i)
|
@response_from_httparty.code.should eql(code.to_i)
|
||||||
end
|
end
|
||||||
|
|
||||||
Then /it should raise an HTTParty::RedirectionTooDeep exception/ do
|
Then /it should raise (?:an|a) ([\w:]+) exception/ do |exception|
|
||||||
@exception_from_httparty.should_not be_nil
|
@exception_from_httparty.should_not be_nil
|
||||||
@exception_from_httparty.class.should eql(HTTParty::RedirectionTooDeep)
|
@exception_from_httparty.class.name.should eql(exception)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
|
When /^I set my HTTParty timeout option to (\d+)$/ do |timeout|
|
||||||
|
@request_options[:timeout] = timeout.to_i
|
||||||
|
end
|
||||||
|
|
||||||
When /I call HTTParty#get with '(.*)'$/ do |url|
|
When /I call HTTParty#get with '(.*)'$/ do |url|
|
||||||
begin
|
begin
|
||||||
@response_from_httparty = HTTParty.get("http://#{@host_and_port}#{url}")
|
@response_from_httparty = HTTParty.get("http://#{@host_and_port}#{url}", @request_options)
|
||||||
rescue HTTParty::RedirectionTooDeep => e
|
rescue HTTParty::RedirectionTooDeep, Timeout::Error => e
|
||||||
@exception_from_httparty = e
|
@exception_from_httparty = e
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
def basic_mongrel_handler
|
def basic_mongrel_handler
|
||||||
Class.new(Mongrel::HttpHandler) do
|
Class.new(Mongrel::HttpHandler) do
|
||||||
attr_writer :content_type, :response_body, :response_code
|
attr_writer :content_type, :response_body, :response_code, :preprocessor
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@content_type = "text/html"
|
@content_type = "text/html"
|
||||||
|
@ -10,6 +10,7 @@ def basic_mongrel_handler
|
||||||
end
|
end
|
||||||
|
|
||||||
def process(request, response)
|
def process(request, response)
|
||||||
|
instance_eval &@preprocessor if @preprocessor
|
||||||
reply_with(response, @response_code, @response_body)
|
reply_with(response, @response_code, @response_body)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,11 @@ Given /that service is accessed at the path '(.*)'/ do |path|
|
||||||
@server.register(path, @handler)
|
@server.register(path, @handler)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Given /^that service takes (\d+) seconds to generate a response$/ do |time|
|
||||||
|
preprocessor = lambda { sleep time.to_i }
|
||||||
|
@handler.preprocessor = preprocessor
|
||||||
|
end
|
||||||
|
|
||||||
Given /the response from the service has a Content-Type of '(.*)'/ do |content_type|
|
Given /the response from the service has a Content-Type of '(.*)'/ do |content_type|
|
||||||
@handler.content_type = content_type
|
@handler.content_type = content_type
|
||||||
end
|
end
|
||||||
|
|
12
features/supports_timeout_option.feature
Normal file
12
features/supports_timeout_option.feature
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
Feature: Supports the timeout option
|
||||||
|
In order to handle inappropriately slow response times
|
||||||
|
As a developer
|
||||||
|
I want my request to raise an exception after my specified timeout as elapsed
|
||||||
|
|
||||||
|
Scenario: A long running response
|
||||||
|
Given a remote service that returns '<h1>Some HTML</h1>'
|
||||||
|
And that service is accessed at the path '/service.html'
|
||||||
|
And that service takes 2 seconds to generate a response
|
||||||
|
When I set my HTTParty timeout option to 1
|
||||||
|
And I call HTTParty#get with '/service.html'
|
||||||
|
Then it should raise a Timeout::Error exception
|
Loading…
Add table
Add a link
Reference in a new issue