Skip to content

Commit

Permalink
Add timeout to post calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrayner committed Nov 10, 2017
1 parent 2b13a36 commit f372e78
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/parliament/request/base_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,18 @@ def get(params: nil)
#
# @param [Hash] params (optional) additional URI encoded form values to be added to the URI.
# @param [String] body (optional) body of the post request.
# @param [Integer] timeout (optional) a Net::HTTP.read_timeout value passed suring the post.
#
# @return [Parliament::Response::BaseResponse] a Parliament::Response::BaseResponse object containing all of the data returned from the URL.
def post(params: nil, body: nil)
def post(params: nil, body: nil, timeout: 60)
@query_params = @query_params.merge(params) unless params.nil?

endpoint_uri = URI.parse(query_url)
endpoint_uri.query = URI.encode_www_form(@query_params.to_a) unless @query_params.empty?

http = Net::HTTP.new(endpoint_uri.host, endpoint_uri.port)
http.use_ssl = true if endpoint_uri.scheme == 'https'
http.read_timeout = timeout

request = Net::HTTP::Post.new(
endpoint_uri.request_uri,
Expand Down
2 changes: 1 addition & 1 deletion lib/parliament/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Parliament
VERSION = '0.10.0'.freeze
VERSION = '0.10.1'.freeze
end
13 changes: 13 additions & 0 deletions spec/parliament/request/base_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,18 @@
with(:body => '{"foo":"bar","test":true,"number":1}').once
end
end

context 'it accepts a timeout' do
before :each do
stub_request(:post, 'http://localhost:3030/people').to_return(status: 200, body: '{}')
end

it 'still processes our request' do
Parliament::Request::BaseRequest.new(base_url: 'http://localhost:3030/people').post(body: { foo: 'bar', test: true, number: 1 }.to_json, timeout: 1)

expect(WebMock).to have_requested(:post, 'http://localhost:3030/people').
with(:body => '{"foo":"bar","test":true,"number":1}').once
end
end
end
end

0 comments on commit f372e78

Please sign in to comment.