Browse files

Hit a local sinatra app rather than example.com in specs.

  • Loading branch information...
1 parent 84992d2 commit 76137bfce84488f3834d8e440a2d2cc5b35c4fc8 @myronmarston committed Nov 21, 2010
View
8 CHANGELOG.md
@@ -1,5 +1,13 @@
# Changelog
+## In git
+
+[Full Changelog](http://github.com/myronmarston/vcr/compare/v1.3.2...master)
+
+* In specs, hit a local sinatra server rather than example.com. This makes
+ the specs faster and removes an external dependency. The specs can pass
+ without being online!
+
## 1.3.2 (November 16, 2010)
[Full Changelog](http://github.com/myronmarston/vcr/compare/v1.3.1...v1.3.2)
View
1 TODO.md
@@ -1,4 +1,3 @@
* use `expect { }.to` rather than `lambda { }.should` in specs.
* Use fakefs gem instead of using temp_dir in specs.
-* Use localhost server in specs instead of hitting example.com.
* Fix #method vs .method in specs.
View
4 spec/extensions/net_http_response_spec.rb
@@ -42,7 +42,7 @@ def self.it_allows_the_body_to_be_read(expected_regex)
end
end
- { :get => /You have reached this web page by typing.*example\.com/, :head => /\A\z/ }.each do |http_verb, expected_body_regex|
+ { :get => /GET to root/, :head => /\A\z/ }.each do |http_verb, expected_body_regex|
context "for a #{http_verb.to_s.upcase} request" do
let(:http_verb_method) { :"request_#{http_verb}" }
@@ -53,7 +53,7 @@ def response(&block)
end
@response ||= begin
- http = Net::HTTP.new('example.com', 80)
+ http = Net::HTTP.new('localhost', VCR::SinatraApp.port)
res = http.send(http_verb_method, '/', &block)
res.should_not be_a(VCR::Net::HTTPResponse)
res.should_not be_a(::WebMock::Net::HTTPResponse)
View
14 spec/extensions/net_http_spec.rb
@@ -3,12 +3,14 @@
describe "Net::HTTP Extensions" do
without_webmock_callbacks
- let(:uri) { URI.parse('http://example.com') }
+ before(:all) { VCR::SinatraApp.port } # ensure the server is started before instantiating any Net::HTTP instances
+
+ let(:uri) { URI.parse("http://localhost:#{VCR::SinatraApp.port}/") }
before(:each) { VCR.stub(:http_stubbing_adapter).and_return(VCR::HttpStubbingAdapters::FakeWeb) }
it 'checks if the request is stubbed using a VCR::Request' do
VCR.http_stubbing_adapter.should_receive(:request_stubbed?) do |request, _|
- request.uri.should == 'http://example.com:80/'
+ request.uri.should == "http://localhost:#{VCR::SinatraApp.port}/"
request.method.should == :get
true
end
@@ -33,7 +35,7 @@
end
def perform_get_with_returning_block
- Net::HTTP.new('example.com', 80).request(Net::HTTP::Get.new('/', {})) do |response|
+ Net::HTTP.new('localhost', VCR::SinatraApp.port).request(Net::HTTP::Get.new('/', {})) do |response|
return response
end
end
@@ -43,15 +45,15 @@ def perform_get_with_returning_block
interaction.request.headers.should_not have_key('content-type')
interaction.request.headers.should_not have_key('host')
end
- Net::HTTP.new('example.com', 80).send_request('POST', '/', '', { 'x-http-user' => 'me' })
+ Net::HTTP.new('localhost', VCR::SinatraApp.port).send_request('POST', '/', '', { 'x-http-user' => 'me' })
end
it "records headers for which Net::HTTP usually sets defaults when the user manually sets their values" do
VCR.should_receive(:record_http_interaction) do |interaction|
interaction.request.headers['content-type'].should == ['foo/bar']
interaction.request.headers['host'].should == ['my-example.com']
end
- Net::HTTP.new('example.com', 80).send_request('POST', '/', '', { 'Content-Type' => 'foo/bar', 'Host' => 'my-example.com' })
+ Net::HTTP.new('localhost', VCR::SinatraApp.port).send_request('POST', '/', '', { 'Content-Type' => 'foo/bar', 'Host' => 'my-example.com' })
end
it 'calls VCR.record_http_interaction' do
@@ -61,7 +63,7 @@ def perform_get_with_returning_block
it 'calls #record_http_interaction only once, even when Net::HTTP internally recursively calls #request' do
VCR.should_receive(:record_http_interaction).once
- Net::HTTP.new('example.com', 80).post('/', nil)
+ Net::HTTP.new('localhost', VCR::SinatraApp.port).post('/', nil)
end
it 'calls #record_http_interaction when Net::HTTP#request is called with a block with a return statement' do
View
25 spec/support/http_library_adapters.rb
@@ -200,29 +200,31 @@ def make_http_request(headers)
end
def self.test_real_http_request(http_allowed)
+ let(:url) { "http://localhost:#{VCR::SinatraApp.port}/foo" }
+
if http_allowed
it 'allows real http requests' do
- get_body_string(make_http_request(:get, 'http://example.com/foo')).should =~ /The requested URL \/foo was not found/
+ get_body_string(make_http_request(:get, url)).should == 'FOO!'
end
it 'records new http requests' do
VCR.should_receive(:record_http_interaction) do |interaction|
- interaction.request.uri.should == 'http://example.com:80/foo'
+ interaction.request.uri.should == url
interaction.request.method.should == :get
- interaction.response.status.code.should == 404
- interaction.response.status.message.should == 'Not Found'
- interaction.response.body.should =~ /The requested URL \/foo was not found/
- interaction.response.headers['content-type'].should == ["text/html; charset=iso-8859-1"]
+ interaction.response.status.code.should == 200
+ interaction.response.status.message.should == 'OK'
+ interaction.response.body.should == 'FOO!'
+ interaction.response.headers['content-type'].should == ["text/html;charset=utf-8"]
end
- make_http_request(:get, 'http://example.com/foo')
+ make_http_request(:get, url)
end
else
it 'does not allow real HTTP requests or record them' do
VCR.should_receive(:record_http_interaction).never
- lambda { make_http_request(:get, 'http://example.com/foo') }.should raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)
+ lambda { make_http_request(:get, url) }.should raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)
end
end
end
@@ -248,24 +250,23 @@ def test_request_stubbed(method, url, expected)
unless http_allowed
describe '.ignore_localhost =' do
- let(:localhost_response) { 'A localhost response!' }
- let(:localhost_server) { VCR::LocalhostServer::STATIC_SERVERS[localhost_response] }
+ localhost_response = "Localhost response"
VCR::LOCALHOST_ALIASES.each do |localhost_alias|
describe 'when set to true' do
extend PendingOnHeroku
before(:each) { subject.ignore_localhost = true }
it "allows requests to #{localhost_alias}" do
- get_body_string(make_http_request(:get, "http://#{localhost_alias}:#{localhost_server.port}/")).should == localhost_response
+ get_body_string(make_http_request(:get, "http://#{localhost_alias}:#{VCR::SinatraApp.port}/localhost_test")).should == localhost_response
end
end
describe 'when set to false' do
before(:each) { subject.ignore_localhost = false }
it "does not allow requests to #{localhost_alias}" do
- expect { make_http_request(:get, "http://#{localhost_alias}:#{localhost_server.port}/") }.to raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)
+ expect { make_http_request(:get, "http://#{localhost_alias}:#{VCR::SinatraApp.port}/localhost_test") }.to raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)
end
end
end
View
25 spec/support/sinatra_app.rb
@@ -0,0 +1,25 @@
+require 'sinatra'
+
+module VCR
+ class SinatraApp < ::Sinatra::Base
+ get '/' do
+ "GET to root"
+ end
+
+ get '/localhost_test' do
+ "Localhost response"
+ end
+
+ get '/foo' do
+ "FOO!"
+ end
+
+ def self.port
+ server.port
+ end
+
+ def self.server
+ @server ||= VCR::LocalhostServer.new(new)
+ end
+ end
+end
View
4 spec/support/vcr_localhost_server.rb
@@ -92,10 +92,6 @@ def wait_until(timeout, error_message, &block)
sleep(0.05)
end
end
-
- STATIC_SERVERS = Hash.new do |h, k|
- h[k] = new(lambda { |env| [200, {}, StringIO.new(k)] })
- end
end
end

0 comments on commit 76137bf

Please sign in to comment.