1
0
Fork 0
mirror of https://github.com/rest-client/rest-client.git synced 2022-11-09 13:49:40 -05:00

close those streams

This commit is contained in:
bmizerany 2008-07-25 13:10:40 -07:00
parent 0fc988ae21
commit 8b000e1a60
4 changed files with 32 additions and 18 deletions

View file

@ -148,6 +148,8 @@ module RestClient
raise RestClient::ServerBrokeConnection raise RestClient::ServerBrokeConnection
rescue Timeout::Error rescue Timeout::Error
raise RestClient::RequestTimeout raise RestClient::RequestTimeout
ensure
payload.close
end end
def setup_credentials(req) def setup_credentials(req)

View file

@ -48,6 +48,9 @@ module RestClient
end end
alias :length :size alias :length :size
def close
@stream.close
end
end end
@ -95,12 +98,16 @@ module RestClient
end end
def create_file_field(s, k, v) def create_file_field(s, k, v)
begin
s.write("Content-Disposition: multipart/form-data; name=\"#{k}\"; filename=\"#{v.path}\"#{EOL}") s.write("Content-Disposition: multipart/form-data; name=\"#{k}\"; filename=\"#{v.path}\"#{EOL}")
s.write("Content-Type: #{mime_for(v.path)}#{EOL}") s.write("Content-Type: #{mime_for(v.path)}#{EOL}")
s.write(EOL) s.write(EOL)
while data = v.read(8124) while data = v.read(8124)
s.write(data) s.write(data)
end end
ensure
v.close
end
end end
def mime_for(path) def mime_for(path)
@ -116,6 +123,10 @@ module RestClient
super.merge({'Content-Type' => %Q{multipart/form-data; boundary="#{boundary}"}}) super.merge({'Content-Type' => %Q{multipart/form-data; boundary="#{boundary}"}})
end end
def close
@stream.close
end
end end
# :stopdoc: # :stopdoc:

View file

@ -2,3 +2,4 @@ require 'rubygems'
require 'spec' require 'spec'
require File.dirname(__FILE__) + '/../lib/rest_client' require File.dirname(__FILE__) + '/../lib/rest_client'

View file

@ -1,6 +1,11 @@
require File.dirname(__FILE__) + '/base' require File.dirname(__FILE__) + '/base'
describe RestClient do describe RestClient do
def generate_payload(v)
RestClient::Payload::Base.new(v)
end
context "public API" do context "public API" do
it "GET" do it "GET" do
RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {}) RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {})
@ -104,9 +109,9 @@ describe RestClient do
end end
it "transmits the request with Net::HTTP" do it "transmits the request with Net::HTTP" do
@http.should_receive(:request).with('req', 'payload') @http.should_receive(:request).with('req', be_kind_of(RestClient::Payload::Base))
@request.should_receive(:process_result) @request.should_receive(:process_result)
@request.transmit(@uri, 'req', 'payload') @request.transmit(@uri, 'req', generate_payload('payload'))
end end
it "uses SSL when the URI refers to a https address" do it "uses SSL when the URI refers to a https address" do
@ -114,13 +119,7 @@ describe RestClient do
@net.should_receive(:use_ssl=).with(true) @net.should_receive(:use_ssl=).with(true)
@http.stub!(:request) @http.stub!(:request)
@request.stub!(:process_result) @request.stub!(:process_result)
@request.transmit(@uri, 'req', 'payload') @request.transmit(@uri, 'req', generate_payload('payload'))
end
it "doesn't send nil payloads" do
@http.should_receive(:request).with('req', '')
@request.should_receive(:process_result)
@request.transmit(@uri, 'req', nil)
end end
it "passes non-hash payloads straight through" do it "passes non-hash payloads straight through" do
@ -151,7 +150,7 @@ describe RestClient do
@request.stub!(:password).and_return('mypass') @request.stub!(:password).and_return('mypass')
@request.should_receive(:setup_credentials).with('req') @request.should_receive(:setup_credentials).with('req')
@request.transmit(@uri, 'req', nil) @request.transmit(@uri, 'req', generate_payload(''))
end end
it "does not attempt to send any credentials if user is nil" do it "does not attempt to send any credentials if user is nil" do
@ -171,7 +170,8 @@ describe RestClient do
it "catches EOFError and shows the more informative ServerBrokeConnection" do it "catches EOFError and shows the more informative ServerBrokeConnection" do
@http.stub!(:request).and_raise(EOFError) @http.stub!(:request).and_raise(EOFError)
lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(RestClient::ServerBrokeConnection) lambda { @request.transmit(@uri, 'req', generate_payload('')) }.
should raise_error(RestClient::ServerBrokeConnection)
end end
it "execute calls execute_inner" do it "execute calls execute_inner" do