<commit_before>shared_examples_for "an http stubbing adapter" do
  subject { described_class }

  describe '#request_uri' do
    it 'returns the uri for the given http request' do
      net_http = Net::HTTP.new('example.com', 80)
      request = Net::HTTP::Get.new('/foo/bar')
      subject.request_uri(net_http, request).should == 'http://example.com:80/foo/bar'
    end
  end

  describe "#with_http_connections_allowed_set_to" do
    it 'sets http_connections_allowed for the duration of the block to the provided value' do
      [true, false].each do |expected|
        yielded_value = :not_set
        subject.with_http_connections_allowed_set_to(expected) { yielded_value = subject.http_connections_allowed? }
        yielded_value.should == expected
      end
    end

    it 'returns the value returned by the block' do
      subject.with_http_connections_allowed_set_to(true) { :return_value }.should == :return_value
    end

    it 'reverts http_connections_allowed when the block completes' do
      [true, false].each do |expected|
        subject.http_connections_allowed = expected
        subject.with_http_connections_allowed_set_to(true) { }
        subject.http_connections_allowed?.should == expected
      end
    end

    it 'reverts http_connections_allowed when the block completes, even if an error is raised' do
      [true, false].each do |expected|
        subject.http_connections_allowed = expected
        lambda { subject.with_http_connections_allowed_set_to(true) { raise RuntimeError } }.should raise_error(RuntimeError)
        subject.http_connections_allowed?.should == expected
      end
    end
  end
end

shared_examples_for "an http stubbing adapter that supports Net::HTTP" do
  context "using Net::HTTP" do
    def get_body_string(response); response.body; end

    def make_http_request(method, url, body = {})
      case method
        when :get
          Net::HTTP.get_response(URI.parse(url))
        when :post
          Net::HTTP.post_form(URI.parse(url), body)
      end
    end

    it_should_behave_like 'an http stubbing adapter that supports some HTTP library'
  end
end

shared_examples_for "an http stubbing adapter that supports some HTTP library" do
  subject { described_class }

  NET_CONNECT_NOT_ALLOWED_ERROR = [StandardError, /You can use VCR to automatically record this request and replay it later/] unless defined?(NET_CONNECT_NOT_ALLOWED_ERROR)

  def self.test_real_http_request(http_allowed)
    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/
      end<commit_msg>Ensure VCR.http_stubbing_adapter returns the adapter being tested.<commit_after>share_as :HttpStubbingAdapterStubbed do
  before(:each) { VCR.stub!(:http_stubbing_adapter).and_return(subject) }
end

shared_examples_for "an http stubbing adapter" do
  include HttpStubbingAdapterStubbed
  subject { described_class }

  describe '#request_uri' do
    it 'returns the uri for the given http request' do
      net_http = Net::HTTP.new('example.com', 80)
      request = Net::HTTP::Get.new('/foo/bar')
      subject.request_uri(net_http, request).should == 'http://example.com:80/foo/bar'
    end
  end

  describe "#with_http_connections_allowed_set_to" do
    it 'sets http_connections_allowed for the duration of the block to the provided value' do
      [true, false].each do |expected|
        yielded_value = :not_set
        subject.with_http_connections_allowed_set_to(expected) { yielded_value = subject.http_connections_allowed? }
        yielded_value.should == expected
      end
    end

    it 'returns the value returned by the block' do
      subject.with_http_connections_allowed_set_to(true) { :return_value }.should == :return_value
    end

    it 'reverts http_connections_allowed when the block completes' do
      [true, false].each do |expected|
        subject.http_connections_allowed = expected
        subject.with_http_connections_allowed_set_to(true) { }
        subject.http_connections_allowed?.should == expected
      end
    end

    it 'reverts http_connections_allowed when the block completes, even if an error is raised' do
      [true, false].each do |expected|
        subject.http_connections_allowed = expected
        lambda { subject.with_http_connections_allowed_set_to(true) { raise RuntimeError } }.should raise_error(RuntimeError)
        subject.http_connections_allowed?.should == expected
      end
    end
  end
end

shared_examples_for "an http stubbing adapter that supports Net::HTTP" do
  context "using Net::HTTP" do
    def get_body_string(response); response.body; end

    def make_http_request(method, url, body = {})
      case method
        when :get
          Net::HTTP.get_response(URI.parse(url))
        when :post
          Net::HTTP.post_form(URI.parse(url), body)
      end
    end

    it_should_behave_like 'an http stubbing adapter that supports some HTTP library'
  end
end

shared_examples_for "an http stubbing adapter that supports some HTTP library" do
  include HttpStubbingAdapterStubbed
  subject { described_class }

  NET_CONNECT_NOT_ALLOWED_ERROR = [StandardError, /You can use VCR to automatically record this request and replay it later/] unless defined?(NET_CONNECT_NOT_ALLOWED_ERROR)

  def self.test_real_http_request(http_allowed)
    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/
      end
{'fraction_non_alphanumeric': 0.07846649946255822, 'fraction_numerical': 0.0014331780723754925, 'mean_word_length': 3.6602671118530883, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6423120', 'n_tokens_mistral': 1662, 'n_tokens_neox': 1613, 'n_words': 455}