How to use port method of VCR Package

Best Vcr_ruby code snippet using VCR.port

hook_into_http_library.rb

Source:hook_into_http_library.rb Github

copy

Full Screen

...5 include VCRStubHelpers6 unless adapter_module = HTTP_LIBRARY_ADAPTERS[library]7 raise ArgumentError.new("No http library adapter module could be found for #{library}")8 end9 http_lib_unsupported = (RUBY_INTERPRETER != :mri && library =~ /(typhoeus|curb|patron|em-http)/)10 describe "using #{adapter_module.http_library_name}", :unless => http_lib_unsupported do11 include adapter_module12 # Necessary for ruby 1.9.2. On 1.9.2 we get an error when we use super,13 # so this gives us another alias we can use for the original method.14 alias make_request make_http_request15 1.upto(2) do |header_count|16 describe "making an HTTP request that responds with #{header_count} Set-Cookie header(s)" do17 define_method :get_set_cookie_header do18 VCR.use_cassette('header_test', :record => :once) do19 get_header 'Set-Cookie', make_http_request(:get, "http://localhost:#{VCR::SinatraApp.port}/set-cookie-headers/#{header_count}")20 end21 end22 it 'returns the same header value when recording and replaying' do23 (recorded_val = get_set_cookie_header).should_not be_nil24 replayed_val = get_set_cookie_header25 replayed_val.should eq(recorded_val)26 end27 end28 end29 def self.test_record_and_playback(description, query)30 describe "a request to a URL #{description}" do31 define_method :get_body do32 VCR.use_cassette('record_and_playback', :record => :once) do33 get_body_string make_http_request(:get, "http://localhost:#{VCR::SinatraApp.port}/record-and-playback?#{query}")34 end35 end36 it "properly records and playsback a request with a URL #{description}" do37 recorded_body = get_body38 played_back_body = get_body39 played_back_body.should eq(recorded_body)40 end41 end42 end43 test_record_and_playback "with spaces encoded as +", "q=a+b"44 test_record_and_playback "with spaces encoded as %20", "q=a%20b"45 test_record_and_playback "with a complex escaped query param", "q=#{CGI.escape("A&(! 234k !@ kasdj232\#$ kjw35")}"46 describe 'making an HTTP request' do47 let(:status) { VCR::ResponseStatus.new(200, 'OK') }48 let(:interaction) { VCR::HTTPInteraction.new(request, response) }49 let(:response_body) { "The response body" }50 before(:each) do51 stub_requests([interaction], [:method, :uri])52 end53 context "when the the stubbed request and response has no headers" do54 let(:request) { VCR::Request.new(:get, 'http://example.com:80/') }55 let(:response) { VCR::Response.new(status, nil, response_body, '1.1') }56 it 'returns the response for a matching request' do57 get_body_string(make_http_request(:get, 'http://example.com/')).should eq(response_body)58 end59 end60 def self.test_playback(description, url)61 context "when a URL #{description} has been stubbed" do62 let(:request) { VCR::Request.new(:get, url) }63 let(:response) { VCR::Response.new(status, nil, response_body, '1.1') }64 it 'returns the expected response for the same request' do65 get_body_string(make_http_request(:get, url)).should eq(response_body)66 end67 end68 end69 test_playback "using https and no explicit port", "https://example.com/foo"70 test_playback "using https and port 443", "https://example.com:443/foo"71 test_playback "using https and some other port", "https://example.com:5190/foo"72 test_playback "that has query params", "http://example.com/search?q=param"73 test_playback "with an encoded ampersand", "http://example.com:80/search?q=#{CGI.escape("Q&A")}"74 end75 it 'does not query the http interaction list excessively' do76 call_count = 077 [:has_interaction_matching?, :response_for].each do |method_name|78 orig_meth = VCR.http_interactions.method(method_name)79 VCR.http_interactions.stub(method_name) do |*args|80 call_count += 181 orig_meth.call(*args)82 end83 end84 VCR.insert_cassette('foo')85 make_http_request(:get, "http://localhost:#{VCR::SinatraApp.port}/foo")86 call_count.should eq(1)87 end88 describe "using the library's stubbing/disconnection APIs" do89 let!(:request_url) { "http://localhost:#{VCR::SinatraApp.port}/foo" }90 if method_defined?(:disable_real_connections)91 it 'can make a real request when VCR is turned off' do92 enable_real_connections93 VCR.turn_off!94 get_body_string(make_http_request(:get, request_url)).should eq("FOO!")95 end96 it 'does not mess with VCR when real connections are disabled' do97 VCR.insert_cassette('example')98 disable_real_connections99 VCR.should_receive(:record_http_interaction) do |interaction|100 interaction.request.uri.should eq(request_url)101 end102 make_http_request(:get, request_url)103 end104 it 'can disable real connections when VCR is turned off' do105 VCR.turn_off!106 expected_error = disable_real_connections107 expect {108 make_http_request(:get, request_url)109 }.to raise_error(expected_error)110 end111 end112 if method_defined?(:directly_stub_request)113 it 'can directly stub the request when VCR is turned off' do114 VCR.turn_off!115 directly_stub_request(:get, request_url, "stubbed response")116 get_body_string(make_http_request(:get, request_url)).should eq("stubbed response")117 end118 end119 end120 describe "request hooks" do121 context 'when there is an around_http_request hook' do122 let(:request_url) { "http://localhost:#{VCR::SinatraApp.port}/foo" }123 it 'yields the request to the block' do124 yielded_request = nil125 VCR.configuration.around_http_request do |request|126 yielded_request = request127 request.proceed128 end129 VCR.use_cassette('new_cassette') do130 make_http_request(:get, request_url)131 end132 yielded_request.method.should eq(:get)133 yielded_request.uri.should eq(request_url)134 end135 it 'returns the response from request.proceed' do136 response = nil137 VCR.configuration.around_http_request do |request|138 response = request.proceed139 end140 VCR.use_cassette('new_cassette') do141 make_http_request(:get, request_url)142 end143 response.body.should eq("FOO!")144 end145 it 'can be used to use a cassette for a request' do146 VCR.configuration.around_http_request do |request|147 VCR.use_cassette('new_cassette', &request)148 end149 VCR.should_receive(:record_http_interaction) do150 VCR.current_cassette.name.should eq('new_cassette')151 end152 VCR.current_cassette.should be_nil153 make_http_request(:get, request_url)154 VCR.current_cassette.should be_nil155 end156 it 'nests them inside each other, making the first declared hook the outermost' do157 order = []158 VCR.configure do |c|159 c.ignore_request { |r| true }160 c.around_http_request do |request|161 order << :before_1162 request.proceed163 order << :after_1164 end165 c.around_http_request do |request|166 order << :before_2167 request.proceed168 order << :after_2169 end170 end171 make_http_request(:get, request_url)172 order.should eq([:before_1, :before_2, :after_2, :after_1])173 end174 it 'raises an appropriate error if the hook does not call request.proceed' do175 VCR.configuration.ignore_request { |r| true }176 hook_declaration = "#{__FILE__}:#{__LINE__ + 1}"177 VCR.configuration.around_http_request { |r| }178 expect {179 make_http_request(:get, request_url)180 }.to raise_error { |error|181 error.message.should include('must call #proceed on the yielded request')182 error.message.should include(hook_declaration)183 }184 end185 it 'does not get a dead fiber error when multiple requests are made' do186 VCR.configuration.around_http_request do |request|187 VCR.use_cassette('new_cassette', &request)188 end189 3.times { make_http_request(:get, request_url) }190 end191 it 'allows the hook to be filtered' do192 order = []193 VCR.configure do |c|194 c.ignore_request { |r| true }195 c.around_http_request(lambda { |r| r.uri =~ /foo/}) do |request|196 order << :before_foo197 request.proceed198 order << :after_foo199 end200 c.around_http_request(lambda { |r| r.uri !~ /foo/}) do |request|201 order << :before_not_foo202 request.proceed203 order << :after_not_foo204 end205 end206 make_http_request(:get, request_url)207 order.should eq([:before_foo, :after_foo])208 end209 it 'ensures that both around/before are invoked or neither' do210 order = []211 allow_1, allow_2 = false, true212 VCR.configure do |c|213 c.ignore_request { |r| true }214 c.around_http_request(lambda { |r| allow_1 = !allow_1 }) do |request|215 order << :before_1216 request.proceed217 order << :after_1218 end219 c.around_http_request(lambda { |r| allow_2 = !allow_2 }) do |request|220 order << :before_2221 request.proceed222 order << :after_2223 end224 end225 make_http_request(:get, request_url)226 order.should eq([:before_1, :after_1])227 end228 end if RUBY_VERSION >= '1.9'229 it 'correctly assigns the correct type to both before and after request hooks, even if they are different' do230 before_type = after_type = nil231 VCR.configuration.before_http_request do |request|232 before_type = request.type233 VCR.insert_cassette('example')234 end235 VCR.configuration.after_http_request do |request|236 after_type = request.type237 VCR.eject_cassette238 end239 make_http_request(:get, "http://localhost:#{VCR::SinatraApp.port}/foo")240 before_type.should be(:unhandled)241 after_type.should be(:recordable)242 end243 context "when the request is ignored" do244 before(:each) do245 VCR.configuration.ignore_request { |r| true }246 end247 it_behaves_like "request hooks", library_hook_name, :ignored248 end249 context 'when the request is recorded' do250 let!(:inserted_cassette) { VCR.insert_cassette('new_cassette') }251 it_behaves_like "request hooks", library_hook_name, :recordable do252 let(:string_in_cassette) { 'example.com get response 1 with path=foo' }253 it 'plays back the cassette when a request is made' do254 VCR.eject_cassette255 VCR.configure do |c|256 c.cassette_library_dir = File.join(VCR::SPEC_ROOT, 'fixtures')257 c.before_http_request do |request|258 VCR.insert_cassette('fake_example_responses', :record => :none)259 end260 end261 get_body_string(make_http_request(:get, 'http://example.com/foo')).should eq(string_in_cassette)262 end263 specify 'the after_http_request hook can be used to eject a cassette after the request is recorded' do264 VCR.configuration.after_http_request { |request| VCR.eject_cassette }265 VCR.should_receive(:record_http_interaction) do |interaction|266 VCR.current_cassette.should be(inserted_cassette)267 end268 make_request269 VCR.current_cassette.should be_nil270 end271 end272 end273 context 'when a stubbed response is played back for the request' do274 before(:each) do275 stub_requests([http_interaction(request_url)], [:method, :uri])276 end277 it_behaves_like "request hooks", library_hook_name, :stubbed278 end279 context 'when the request is not allowed' do280 it_behaves_like "request hooks", library_hook_name, :unhandled do281 undef assert_expected_response282 def assert_expected_response(response)283 response.should be_nil284 end285 undef make_request286 def make_request(disabled = false)287 if disabled288 make_http_request(:get, request_url)289 else290 expect { make_http_request(:get, request_url) }.to raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)291 end292 end293 end294 end295 end296 describe '.stub_requests using specific match_attributes' do297 before(:each) { VCR.stub(:real_http_connections_allowed? => false) }298 let(:interactions) { interactions_from('match_requests_on.yml') }299 let(:normalized_interactions) do300 interactions.each do |i|301 i.request.headers = normalize_request_headers(i.request.headers)302 end303 interactions304 end305 def self.matching_on(attribute, valid, invalid, &block)306 describe ":#{attribute}" do307 let(:perform_stubbing) { stub_requests(normalized_interactions, [attribute]) }308 before(:each) { perform_stubbing }309 module_eval(&block)310 valid.each do |val, response|311 it "returns the expected response for a #{val.inspect} request" do312 get_body_string(make_http_request(val)).should eq(response)313 end314 end315 it "raises an error for a request with a different #{attribute}" do316 expect { make_http_request(invalid) }.to raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)317 end318 end319 end320 matching_on :method, { :get => "get method response", :post => "post method response" }, :put do321 def make_http_request(http_method)322 make_request(http_method, 'http://some-wrong-domain.com/', nil, {})323 end324 end325 matching_on :host, { 'example1.com' => 'example1.com host response', 'example2.com' => 'example2.com host response' }, 'example3.com' do326 def make_http_request(host)327 make_request(:get, "http://#{host}/some/wrong/path", nil, {})328 end329 end330 matching_on :path, { '/path1' => 'path1 response', '/path2' => 'path2 response' }, '/path3' do331 def make_http_request(path)332 make_request(:get, "http://some.wrong.domain.com#{path}?p=q", nil, {})333 end334 end335 matching_on :uri, { 'http://example.com/uri1' => 'uri1 response', 'http://example.com/uri2' => 'uri2 response' }, 'http://example.com/uri3' do336 def make_http_request(uri)337 make_request(:get, uri, nil, {})338 end339 end340 matching_on :body, { 'param=val1' => 'val1 body response', 'param=val2' => 'val2 body response' }, 'param=val3' do341 def make_http_request(body)342 make_request(:put, "http://wrong-domain.com/wrong/path", body, {})343 end344 end345 matching_on :headers, {{ 'X-Http-Header1' => 'val1' } => 'val1 header response', { 'X-Http-Header1' => 'val2' } => 'val2 header response' }, { 'X-Http-Header1' => 'val3' } do346 def make_http_request(headers)347 make_request(:get, "http://wrong-domain.com/wrong/path", nil, headers)348 end349 end350 end351 def self.test_real_http_request(http_allowed, *other)352 let(:url) { "http://localhost:#{VCR::SinatraApp.port}/foo" }353 if http_allowed354 it 'allows real http requests' do355 get_body_string(make_http_request(:get, url)).should eq('FOO!')356 end357 describe 'recording new http requests' do358 let(:recorded_interaction) do359 interaction = nil360 VCR.should_receive(:record_http_interaction) { |i| interaction = i }361 make_http_request(:post, url, "the body", { 'X-Http-Foo' => 'bar' })362 interaction363 end364 it 'does not record the request if the hook is disabled' do365 VCR.library_hooks.exclusively_enabled :something_else do366 VCR.should_not_receive(:record_http_interaction)367 make_http_request(:get, url)368 end369 end unless other.include?(:not_disableable)370 it 'records the request uri' do371 recorded_interaction.request.uri.should eq(url)372 end373 it 'records the request method' do374 recorded_interaction.request.method.should eq(:post)375 end376 it 'records the request body' do377 recorded_interaction.request.body.should eq("the body")378 end379 it 'records the request headers' do380 headers = downcase_headers(recorded_interaction.request.headers)381 headers.should include('x-http-foo' => ['bar'])382 end383 it 'records the response status code' do384 recorded_interaction.response.status.code.should eq(200)385 end386 it 'records the response status message' do387 recorded_interaction.response.status.message.strip.should eq('OK')388 end unless other.include?(:status_message_not_exposed)389 it 'records the response body' do390 recorded_interaction.response.body.should eq('FOO!')391 end392 it 'records the response headers' do393 headers = downcase_headers(recorded_interaction.response.headers)394 headers.should include('content-type' => ["text/html;charset=utf-8"])395 end396 end397 else398 it 'does not allow real HTTP requests or record them' do399 VCR.should_receive(:record_http_interaction).never400 expect { make_http_request(:get, url) }.to raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)401 end402 end403 end404 [true, false].each do |http_allowed|405 context "when VCR.real_http_connections_allowed? is returning #{http_allowed}" do406 before(:each) { VCR.stub(:real_http_connections_allowed? => http_allowed) }407 test_real_http_request(http_allowed, *other)408 unless http_allowed409 localhost_response = "Localhost response"410 context 'when ignore_hosts is configured to "127.0.0.1", "localhost"' do411 before(:each) do412 VCR.configure { |c| c.ignore_hosts "127.0.0.1", "localhost" }413 end414 %w[ 127.0.0.1 localhost ].each do |localhost_alias|415 it "allows requests to #{localhost_alias}" do416 get_body_string(make_http_request(:get, "http://#{localhost_alias}:#{VCR::SinatraApp.port}/localhost_test")).should eq(localhost_response)417 end418 end419 it 'does not allow requests to 0.0.0.0' do420 expect { make_http_request(:get, "http://0.0.0.0:#{VCR::SinatraApp.port}/localhost_test") }.to raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)421 end422 end423 end424 context 'when some requests are stubbed' do425 let(:interactions) { interactions_from('fake_example_responses.yml') }426 before(:each) do427 stub_requests(interactions, VCR::RequestMatcherRegistry::DEFAULT_MATCHERS)428 end429 it 'gets the stubbed responses when requests are made to http://example.com/foo, and does not record them' do430 VCR.should_receive(:record_http_interaction).never431 get_body_string(make_http_request(:get, 'http://example.com/foo')).should =~ /example\.com get response \d with path=foo/432 end433 it 'rotates through multiple responses for the same request' do434 get_body_string(make_http_request(:get, 'http://example.com/foo')).should eq('example.com get response 1 with path=foo')435 get_body_string(make_http_request(:get, 'http://example.com/foo')).should eq('example.com get response 2 with path=foo')436 end unless other.include?(:does_not_support_rotating_responses)437 it "correctly handles stubbing multiple values for the same header" do438 header = get_header('Set-Cookie', make_http_request(:get, 'http://example.com/two_set_cookie_headers'))439 header = header.split(', ') if header.respond_to?(:split)440 header.should =~ ['bar=bazz', 'foo=bar']441 end442 end443 end444 end445 end446end...

Full Screen

Full Screen

fakeweb_spec.rb

Source:fakeweb_spec.rb Github

copy

Full Screen

...29 it 'records the request body when using #post_form' do30 VCR.should_receive(:record_http_interaction) do |interaction|31 expect(interaction.request.body).to eq("q=ruby")32 end33 uri = URI("http://localhost:#{VCR::SinatraApp.port}/foo")34 Net::HTTP.post_form(uri, 'q' => 'ruby')35 end36 it "does not record headers for which Net::HTTP sets defaults near the end of the real request" do37 VCR.should_receive(:record_http_interaction) do |interaction|38 expect(interaction.request.headers).not_to have_key('content-type')39 expect(interaction.request.headers).not_to have_key('host')40 end41 Net::HTTP.new('localhost', VCR::SinatraApp.port).send_request('POST', '/', '', { 'x-http-user' => 'me' })42 end43 it "records headers for which Net::HTTP usually sets defaults when the user manually sets their values" do44 VCR.should_receive(:record_http_interaction) do |interaction|45 expect(interaction.request.headers['content-type']).to eq(['foo/bar'])46 expect(interaction.request.headers['host']).to eq(['my-example.com'])47 end48 Net::HTTP.new('localhost', VCR::SinatraApp.port).send_request('POST', '/', '', { 'Content-Type' => 'foo/bar', 'Host' => 'my-example.com' })49 end50 def perform_get_with_returning_block51 Net::HTTP.new('localhost', VCR::SinatraApp.port).request(Net::HTTP::Get.new('/', {})) do |response|52 return response53 end54 end55 it 'records the interaction when Net::HTTP#request is called with a block with a return statement' do56 VCR.should_receive(:record_http_interaction).once57 expect(perform_get_with_returning_block.body).to eq("GET to root")58 end59 def make_post_request60 Net::HTTP.new('localhost', VCR::SinatraApp.port).post('/record-and-playback', '')61 end62 it 'records the interaction only once, even when Net::HTTP internally recursively calls #request' do63 VCR.should_receive(:record_http_interaction).once64 make_post_request65 end66 it 'properly returns the response body for a post request when recording, stubbing or ignoring the request' do67 recorded_body = nil68 VCR.use_cassette("new_cassette", :record => :once) do69 recorded_body = make_post_request.body70 expect(recorded_body).to match(/Response \d+/)71 end72 VCR.use_cassette("new_cassette", :record => :once) do73 expect(make_post_request.body).to eq(recorded_body)74 end75 VCR.configuration.ignore_request { |r| true }76 ignored_body = make_post_request.body77 expect(ignored_body).not_to eq(recorded_body)78 expect(ignored_body).to match(/Response \d+/)79 end80 context 'when the same Net::HTTP request object is used twice' do81 let(:uri) { URI("http://localhost:#{VCR::SinatraApp.port}/foo") }82 let(:http) { Net::HTTP.new(uri.host, uri.port) }83 it 'raises an UnhandledHTTPRequestError when using a cassette that only recorded one request' do84 VCR.use_cassette("new_cassette", :record => :once) do85 request = Net::HTTP::Get.new(uri.request_uri)86 http.request(request)87 end88 VCR.use_cassette("new_cassette", :record => :once) do89 request = Net::HTTP::Get.new(uri.request_uri)90 http.request(request)91 http.request(request)92 end93 end94 end95 end96 describe "VCR.configuration.after_library_hooks_loaded hook" do97 let(:run_hook) { $fakeweb_after_loaded_hook.conditionally_invoke }98 context 'when WebMock has been loaded' do99 before(:each) do100 expect(defined?(WebMock)).to be_true101 end102 it 'raises an error since FakeWeb and WebMock cannot both be used simultaneously' do103 expect { run_hook }.to raise_error(ArgumentError, /cannot use both/)104 end105 end106 context 'when WebMock has not been loaded' do107 let!(:orig_webmock_constant) { ::WebMock }108 before(:each) { Object.send(:remove_const, :WebMock) }109 after(:each) { ::WebMock = orig_webmock_constant }110 it 'does not raise an error' do111 run_hook # should not raise an error112 end113 it "warns about FakeWeb deprecation" do114 ::Kernel.should_receive(:warn).with("WARNING: VCR's FakeWeb integration is deprecated and will be removed in VCR 3.0.")115 run_hook116 end117 end118 end119 describe "when a SocketError occurs" do120 before(:each) do121 VCR.configuration.ignore_request { |r| true }122 end123 it_behaves_like "request hooks", :fakeweb, :ignored do124 undef assert_expected_response125 def assert_expected_response(response)126 expect(response).to be_nil127 end128 undef make_request129 def make_request(disabled = false)130 ::Net::HTTP.any_instance.stub(:request_without_vcr).and_raise(SocketError)131 expect {132 ::Net::HTTP.get_response(URI(request_url))133 }.to raise_error(SocketError)134 end135 end136 end137 describe "when VCR is turned off" do138 it 'allows white listed connections' do139 ::FakeWeb.allow_net_connect = %r[localhost]140 VCR.turn_off!141 uri = URI("http://localhost:#{VCR::SinatraApp.port}/foo")142 expect(Net::HTTP.get(uri)).to eq("FOO!")143 end144 end145end...

Full Screen

Full Screen

port

Using AI Code Generation

copy

Full Screen

1VCR.use_cassette('port') do2 VCR.port(1234)3VCR.use_cassette('port') do4 VCR.port(1234)5VCR.use_cassette('port') do6 VCR.port(1234)7VCR.use_cassette('port') do8 VCR.port(1234)9VCR.use_cassette('port') do10 VCR.port(1234)11VCR.use_cassette('port') do12 VCR.port(1234)13VCR.use_cassette('port') do14 VCR.port(1234)15VCR.use_cassette('port') do

Full Screen

Full Screen

port

Using AI Code Generation

copy

Full Screen

1 c.default_cassette_options = { :record => :new_episodes }2 VCR.use_cassette('test') do3 expect(VCR.port).to eq(80)4 c.default_cassette_options = { :record => :new_episodes }5 VCR.use_cassette('test') do6 expect(VCR.port).to eq(8080)7 c.default_cassette_options = { :record => :new_episodes }8 VCR.use_cassette('test') do9 expect(VCR.port).to eq(8080)10 c.default_cassette_options = { :record => :new_episodes }11 VCR.use_cassette('test') do

Full Screen

Full Screen

port

Using AI Code Generation

copy

Full Screen

1VCR.use_cassette("test") do2VCR.use_cassette("test") do3VCR.use_cassette("test") do4VCR.use_cassette("test") do5VCR.use_cassette("test") do

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Vcr_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful