Skip to content

Commit

Permalink
Merge pull request rest-client#333 from jrafanie/fix_unwrapped_ipv6_h…
Browse files Browse the repository at this point in the history
…ost_field_in_request_header

ruby 2+: delegate IPv6 [] wrapping to net/http by passing a URI object.
  • Loading branch information
ab committed Dec 4, 2014
2 parents 3d69b2b + d4ea706 commit 4cc904f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/restclient/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ def initialize args

def execute & block
uri = parse_url_with_auth(url)
transmit uri, net_http_request_class(method).new(uri.request_uri, processed_headers), payload, & block

# With 2.0.0+, net/http accepts URI objects in requests and handles wrapping
# IPv6 addresses in [] for use in the Host request header.
request_uri = RUBY_VERSION >= "2.0.0" ? uri : uri.request_uri
transmit uri, net_http_request_class(method).new(request_uri, processed_headers), payload, & block
ensure
payload.close if payload
end
Expand Down
15 changes: 15 additions & 0 deletions spec/unit/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,21 @@
@request.execute
end

it "IPv6: executes by constructing the Net::HTTP object, headers, and payload and calling transmit" do
@request = RestClient::Request.new(:method => :put, :url => 'http://[::1]/some/resource', :payload => 'payload')
klass = double("net:http class")
@request.should_receive(:net_http_request_class).with(:put).and_return(klass)

if RUBY_VERSION >= "2.0.0"
klass.should_receive(:new).with(kind_of(URI), kind_of(Hash)).and_return('result')
else
klass.should_receive(:new).with(kind_of(String), kind_of(Hash)).and_return('result')
end

@request.should_receive(:transmit)
@request.execute
end

it "transmits the request with Net::HTTP" do
@http.should_receive(:request).with('req', 'payload')
@request.should_receive(:process_result)
Expand Down

0 comments on commit 4cc904f

Please sign in to comment.