<commit_before>require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

class HttpsInterfaceTestObject
  include VaultedBilling::HttpsInterface

  def initialize(options = {})
    self.test_uri = options[:test_uri]
    self.live_uri = options[:live_uri]
    self.use_test_uri = options[:test]
  end
end

describe VaultedBilling::HttpsInterface do
  it 'uses the live uri by default' do
    HttpsInterfaceTestObject.new(:live_uri => 'https://live.uri').tap do |http|
      http.uri.to_s.should == 'https://live.uri'
    end
  end

  it 'uses the given test uri in test mode' do
    HttpsInterfaceTestObject.new(:test_uri => 'https://test.uri', :test => true).tap do |http|
      http.uri.to_s.should == 'https://test.uri'
    end
  end

  it 'uses the given live uri in live mode' do
    HttpsInterfaceTestObject.new(:live_uri => 'https://live.uri', :test => false).tap do |http|
      http.uri.to_s.should == 'https://live.uri'
    end
  end

  context 'post_data' do
    let(:gateway) { HttpsInterfaceTestObject.new(:live_uri => 'https://1.2.3.4/') }

    before(:each) do
      WebMock.stub_request(:post, 'https://1.2.3.4/')
    end

    after(:each) do
      WebMock.reset_webmock
    end

    it 'posts the given data' do
      gateway.send(:post_data, 'fubar')
      WebMock.should have_requested(:post, 'https://1.2.3.4/').with(:body => 'fubar')
    end

    it 'sets a custom User-Agent header' do
      gateway.send(:post_data, 'fubar')
      WebMock.should have_requested(:post, 'https://1.2.3.4/').with(:headers => {'User-Agent' => "vaulted_billing #{VaultedBilling::Version}"})
    end

    it 'runs the before_post callback before submission' do
      gateway.should_receive(:before_post).with('fubar').and_raise(RuntimeError)
      expect {
        gateway.send(:post_data, 'fubar')
      }.to raise_error(RuntimeError)
      WebMock.should_not have_requested(:any, 'https://1.2.3.4/')
    end

    it 'runs the after_post callback after submission' do
      gateway.should_receive(:after_post).with(an_instance_of(VaultedBilling::HttpsInterface::PostResponse))
      gateway.send(:post_data, 'fubar')
    end

    context 'with Timeout errors' do
      before(:each) do
        WebMock.stub_request(:post, 'https://1.2.3.4/').to_raise(Timeout::Error.new('Test Exception'))
      end

      after(:each) do
        WebMock.reset_webmock
      end

      it 'returns an PostResponse' do
        gateway.send(:post_data, 'fubar').should be_kind_of VaultedBilling::HttpsInterface::PostResponse
      end

      it 'is unsuccessful' do
        gateway.send(:post_data, 'fubar').should_not be_success<commit_msg>Fixed failing test<commit_after>require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

class HttpsInterfaceTestObject
  include VaultedBilling::HttpsInterface

  def initialize(options = {})
    self.test_uri = options[:test_uri]
    self.live_uri = options[:live_uri]
    self.use_test_uri = options[:test]
  end
end

describe VaultedBilling::HttpsInterface do
  it 'uses the live uri by default' do
    HttpsInterfaceTestObject.new(:live_uri => 'https://live.uri').tap do |http|
      http.uri.to_s.should == 'https://live.uri'
    end
  end

  it 'uses the given test uri in test mode' do
    HttpsInterfaceTestObject.new(:test_uri => 'https://test.uri', :test => true).tap do |http|
      http.uri.to_s.should == 'https://test.uri'
    end
  end

  it 'uses the given live uri in live mode' do
    HttpsInterfaceTestObject.new(:live_uri => 'https://live.uri', :test => false).tap do |http|
      http.uri.to_s.should == 'https://live.uri'
    end
  end

  context 'post_data' do
    let(:gateway) { HttpsInterfaceTestObject.new(:live_uri => 'https://1.2.3.4/') }

    before(:each) do
      WebMock.stub_request(:post, 'https://1.2.3.4/')
    end

    after(:each) do
      WebMock.reset_webmock
    end

    it 'posts the given data' do
      gateway.send(:post_data, 'fubar')
      WebMock.should have_requested(:post, 'https://1.2.3.4/').with(:body => 'fubar')
    end

    it 'sets a custom User-Agent header' do
      gateway.send(:post_data, 'fubar')
      WebMock.should have_requested(:post, 'https://1.2.3.4/').with(:headers => {'User-Agent' => "vaulted_billing/#{VaultedBilling::Version}"})
    end

    it 'runs the before_post callback before submission' do
      gateway.should_receive(:before_post).with('fubar').and_raise(RuntimeError)
      expect {
        gateway.send(:post_data, 'fubar')
      }.to raise_error(RuntimeError)
      WebMock.should_not have_requested(:any, 'https://1.2.3.4/')
    end

    it 'runs the after_post callback after submission' do
      gateway.should_receive(:after_post).with(an_instance_of(VaultedBilling::HttpsInterface::PostResponse))
      gateway.send(:post_data, 'fubar')
    end

    context 'with Timeout errors' do
      before(:each) do
        WebMock.stub_request(:post, 'https://1.2.3.4/').to_raise(Timeout::Error.new('Test Exception'))
      end

      after(:each) do
        WebMock.reset_webmock
      end

      it 'returns an PostResponse' do
        gateway.send(:post_data, 'fubar').should be_kind_of VaultedBilling::HttpsInterface::PostResponse
      end

      it 'is unsuccessful' do
        gateway.send(:post_data, 'fubar').should_not be_success
{'fraction_non_alphanumeric': 0.14112521310854328, 'fraction_numerical': 0.009092631180147755, 'mean_word_length': 3.3102040816326532, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 24, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 12, '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': '6883356', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1795, 'n_words': 364}