Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed Net::HTTP adapter to not break normal Net::HTTP behaviour when …
…network connections are allowed. (This fixed selenium-webdriver compatibility!!!)
- Loading branch information
Showing
7 changed files
with
238 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
shared_examples_for "Net::HTTP" do | ||
describe "when making real requests", :net_connect => true do | ||
let(:port){ WebMockServer.instance.port } | ||
|
||
before(:each) do | ||
@http = Net::HTTP.new("localhost", port) | ||
end | ||
|
||
it "should return a Net::ReadAdapter from response.body when a real request is made with a block and #read_body", :net_connect => true do | ||
response = Net::HTTP.new("localhost", port).request_get('/') { |r| r.read_body { } } | ||
response.body.should be_a(Net::ReadAdapter) | ||
end | ||
|
||
if RUBY_VERSION < '1.9' #looks like StringIO doesn't respons to bytesize in ruby 1.9 - weird | ||
it "should handle real requests with readable body", :net_connect => true do | ||
req = Net::HTTP::Post.new("/") | ||
Net::HTTP.start("localhost", port) {|http| | ||
http.request(req, StringIO.new("my_params")) | ||
}.body.should =~ /hello world/ | ||
end | ||
end | ||
|
||
it "should handle requests with block passed to read_body", :net_connect => true do | ||
body = "" | ||
req = Net::HTTP::Get.new("/") | ||
Net::HTTP.start("localhost", port) do |http| | ||
http.request(req) do |res| | ||
res.read_body do |str| | ||
body << str | ||
end | ||
end | ||
end | ||
body.should =~ /hello world/ | ||
end | ||
|
||
it "should connect only once when connected on start", :net_connect => true do | ||
@http = Net::HTTP.new('localhost', port) | ||
socket_id_before_request = socket_id_after_request = nil | ||
@http.start {|conn| | ||
socket_id_before_request = conn.instance_variable_get(:@socket).object_id | ||
conn.request(Net::HTTP::Get.new("/")) | ||
socket_id_after_request = conn.instance_variable_get(:@socket).object_id | ||
} | ||
socket_id_after_request.should_not be_nil | ||
socket_id_after_request.should == socket_id_before_request | ||
end | ||
|
||
describe "without start" do | ||
it "should close connection after a real request" do | ||
@http.get('/') { } | ||
@http.should_not be_started | ||
end | ||
|
||
it "should execute block exactly once" do | ||
times = 0 | ||
@http.get('/') { times += 1 } | ||
times.should == 1 | ||
end | ||
|
||
it "should have socket open during a real request" do | ||
socket_id = nil | ||
@http.get('/') { | ||
socket_id = @http.instance_variable_get(:@socket).object_id | ||
} | ||
socket_id.should_not be_nil | ||
end | ||
|
||
it "should be started during a real request" do | ||
started = nil | ||
@http.get('/') { | ||
started = @http.started? | ||
} | ||
started.should == true | ||
@http.started?.should == false | ||
end | ||
end | ||
|
||
describe "with start" do | ||
it "should close connection after a real request" do | ||
@http.start {|conn| conn.get('/') { } } | ||
@http.should_not be_started | ||
end | ||
|
||
it "should execute block exactly once" do | ||
times = 0 | ||
@http.start {|conn| conn.get('/') { times += 1 }} | ||
times.should == 1 | ||
end | ||
|
||
it "should have socket open during a real request" do | ||
socket_id = nil | ||
@http.start {|conn| conn.get('/') { | ||
socket_id = conn.instance_variable_get(:@socket).object_id | ||
} | ||
} | ||
socket_id.should_not be_nil | ||
end | ||
|
||
it "should be started during a real request" do | ||
started = nil | ||
@http.start {|conn| conn.get('/') { | ||
started = conn.started? | ||
} | ||
} | ||
started.should == true | ||
@http.started?.should == false | ||
end | ||
end | ||
|
||
describe "with start without request block" do | ||
it "should close connection after a real request" do | ||
@http.start {|conn| conn.get('/') } | ||
@http.should_not be_started | ||
end | ||
|
||
it "should have socket open during a real request" do | ||
socket_id = nil | ||
@http.start {|conn| | ||
socket_id = conn.instance_variable_get(:@socket).object_id | ||
} | ||
socket_id.should_not be_nil | ||
end | ||
|
||
it "should be started during a real request" do | ||
started = nil | ||
@http.start {|conn| | ||
started = conn.started? | ||
} | ||
started.should == true | ||
@http.started?.should == false | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'rubygems' | ||
require 'rspec' | ||
require 'net/http' | ||
require 'net_http_shared' | ||
require 'net/https' | ||
require 'stringio' | ||
require 'support/webmock_server' | ||
|
||
describe "Real Net:HTTP without webmock", :without_webmock => true do | ||
before(:all) do | ||
raise "WebMock has no access here!!!" if defined?(WebMock::NetHTTPUtility) | ||
WebMockServer.instance.start | ||
end | ||
|
||
after(:all) do | ||
WebMockServer.instance.stop | ||
end | ||
|
||
it_should_behave_like "Net::HTTP" | ||
end |
Oops, something went wrong.