<commit_before>require 'pacto/rspec'

describe 'pacto/rspec' do
  let(:contract_path) { 'spec/integration/data/simple_contract.json' }
  let(:strict_contract_path) { 'spec/integration/data/strict_contract.json' }

  before :all do
    WebMock.allow_net_connect!
    @server = Pacto::Server::Dummy.new 8000, '/hello', '{"message": "Hello World!"}'
    @server.start
  end

  after :all do
    @server.terminate
  end

  def expect_to_raise(message_pattern = nil, &blk)
    expect { blk.call }.to raise_error(RSpec::Expectations::ExpectationNotMetError, message_pattern)
  end

  def json_response(url)
    response = Faraday.get(url) do |req|
      req.headers = {'Accept' => 'application/json' }
    end
    MultiJson.load(response.body)
  end

  context 'successful validations' do
    let(:contracts) do
      Pacto.load_contracts 'spec/integration/data/', 'http://dummyprovider.com'
    end

    before(:each) do
      Pacto.configure do |c|
        c.strict_matchers = false
        c.register_hook Pacto::Hooks::ERBHook.new
      end

      contracts.stub_all(:device_id => 42)
      Pacto.validate!

      Faraday.get('http://dummyprovider.com/hello') do |req|
        req.headers = {'Accept' => 'application/json' }
      end
    end

    it 'performs successful assertions' do
      # High level assertions
      expect(Pacto).to_not have_unmatched_requests
      expect(Pacto).to_not have_failed_validations

      # Increasingly strict assertions
      expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/hello')
      expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/hello').with(:headers => {'Accept' => 'application/json'})
      expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/hello').against_contract(/simple_contract.json/)
    end

    it 'supports negative assertions' do
      expect(Pacto).to_not have_validated(:get, 'http://dummyprovider.com/strict')
      Faraday.get('http://dummyprovider.com/strict') do |req|
        req.headers = {'Accept' => 'application/json' }
      end
      expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/strict')
    end

    it 'raises useful error messages' do
      # High level error messages
      expect_to_raise(/Expected Pacto to have not matched all requests to a Contract, but all requests were matched/) { expect(Pacto).to have_unmatched_requests }
      expect_to_raise(/Expected Pacto to have found validation problems, but none were found/) { expect(Pacto).to have_failed_validations }

      unmatched_url = 'http://localhost:8000/404'
      Faraday.get unmatched_url
      expect_to_raise(/the following requests were not matched.*#{Regexp.quote unmatched_url}/m) { expect(Pacto).to_not have_unmatched_requests }

      # Expected failures
      expect_to_raise(/no matching request was received/) { expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/hello').with(:headers => {'Accept' => 'text/plain'}) }
      # No support for with accepting a block
      # expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/hello').with { |req| req.body == "abc" }
      expect_to_raise(/but it was validated against/) { expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/hello').against_contract(/strict_contract.json/) }
      expect_to_raise(/but it was validated against/) { expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/hello').against_contract('simple_contract.json') }
      expect_to_raise(/but no matching request was received/) { expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/strict') }
    end

    it 'displays Contract validation problems' do
      contracts.stub_all(:device_id => 1.5)
      Faraday.get('http://dummyprovider.com/strict') do |req|
        req.headers = {'Accept' => 'application/json' }
      end
      expect_to_raise(/validation errors were found:/) { expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/strict') }

      expect_to_raise(/but the following issues were found:/) { expect(Pacto).to_not have_failed_validations }
    end
  end<commit_msg>Check for contract file in rspec messages<commit_after>require 'pacto/rspec'

describe 'pacto/rspec' do
  let(:contract_path) { 'spec/integration/data/simple_contract.json' }
  let(:strict_contract_path) { 'spec/integration/data/strict_contract.json' }

  before :all do
    WebMock.allow_net_connect!
    @server = Pacto::Server::Dummy.new 8000, '/hello', '{"message": "Hello World!"}'
    @server.start
  end

  after :all do
    @server.terminate
  end

  def expect_to_raise(message_pattern = nil, &blk)
    expect { blk.call }.to raise_error(RSpec::Expectations::ExpectationNotMetError, message_pattern)
  end

  def json_response(url)
    response = Faraday.get(url) do |req|
      req.headers = {'Accept' => 'application/json' }
    end
    MultiJson.load(response.body)
  end

  def play_bad_response
    contracts.stub_all(:device_id => 1.5)
    Faraday.get('http://dummyprovider.com/strict') do |req|
      req.headers = {'Accept' => 'application/json' }
    end
  end

  context 'successful validations' do
    let(:contracts) do
      Pacto.load_contracts 'spec/integration/data/', 'http://dummyprovider.com'
    end

    before(:each) do
      Pacto.configure do |c|
        c.strict_matchers = false
        c.register_hook Pacto::Hooks::ERBHook.new
      end

      contracts.stub_all(:device_id => 42)
      Pacto.validate!

      Faraday.get('http://dummyprovider.com/hello') do |req|
        req.headers = {'Accept' => 'application/json' }
      end
    end

    it 'performs successful assertions' do
      # High level assertions
      expect(Pacto).to_not have_unmatched_requests
      expect(Pacto).to_not have_failed_validations

      # Increasingly strict assertions
      expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/hello')
      expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/hello').with(:headers => {'Accept' => 'application/json'})
      expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/hello').against_contract(/simple_contract.json/)
    end

    it 'supports negative assertions' do
      expect(Pacto).to_not have_validated(:get, 'http://dummyprovider.com/strict')
      Faraday.get('http://dummyprovider.com/strict') do |req|
        req.headers = {'Accept' => 'application/json' }
      end
      expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/strict')
    end

    it 'raises useful error messages' do
      # High level error messages
      expect_to_raise(/Expected Pacto to have not matched all requests to a Contract, but all requests were matched/) { expect(Pacto).to have_unmatched_requests }
      expect_to_raise(/Expected Pacto to have found validation problems, but none were found/) { expect(Pacto).to have_failed_validations }

      unmatched_url = 'http://localhost:8000/404'
      Faraday.get unmatched_url
      expect_to_raise(/the following requests were not matched.*#{Regexp.quote unmatched_url}/m) { expect(Pacto).to_not have_unmatched_requests }

      # Expected failures
      expect_to_raise(/no matching request was received/) { expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/hello').with(:headers => {'Accept' => 'text/plain'}) }
      # No support for with accepting a block
      # expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/hello').with { |req| req.body == "abc" }
      expect_to_raise(/but it was validated against/) { expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/hello').against_contract(/strict_contract.json/) }
      expect_to_raise(/but it was validated against/) { expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/hello').against_contract('simple_contract.json') }
      expect_to_raise(/but no matching request was received/) { expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/strict') }
    end

    it 'displays Contract validation problems' do
      play_bad_response
      expect_to_raise(/validation errors were found:/) { expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/strict') }
      expect_to_raise(/but the following issues were found:/) { expect(Pacto).to_not have_failed_validations }
    end

    it 'display the Contract file' do
      play_bad_response
      schema_file_uri = Addressable::URI.convert_path(File.absolute_path strict_contract_path).to_s
      expect_to_raise(/in schema #{schema_file_uri}/) { expect(Pacto).to have_validated(:get, 'http://dummyprovider.com/strict') }
    end
  end
{'fraction_non_alphanumeric': 0.128598174584601, 'fraction_numerical': 0.0035104142288790077, 'mean_word_length': 3.780201342281879, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 23, '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': '4415841', 'n_tokens_mistral': 2724, 'n_tokens_neox': 2640, 'n_words': 552}