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

describe Postcode::API do
  context "without a valid API key" do
    before do
      body = '{"success":false,"error":{"code":401,"message":"A valid \'Api-Key\' needs to be provided in the \'Headers\' in order to use this API."}}'
      stub_request(:get, "http://api.postcodeapi.nu/5041EB").
        with(:headers => {'Api-Key'=>'invalid'}).
        to_return(:status => 401, :body => body, :headers => {"Content-Type" => "application/json"})
    end

    subject(:api) { Postcode::API.new("invalid") }

    it 'returns an error response' do
      result = api.postcode('5041EB')

      result.success.should be_false
      result.error.code.should eq(401)
    end
  end

  context "with a valid API key" do
    context "with postcode only" do
      before do
        body = '{"success":true,"resource":{"street":"Wilhelminapark","postcode":"5041EB","town":"Tilburg","latitude":51.9401,"longitude":5.61531}}'
        stub_request(:get, "http://api.postcodeapi.nu/5041EB").
          with(:headers => {'Api-Key'=>'valid'}).
          to_return(:status => 401, :body => body, :headers => {"Content-Type" => "application/json"})
      end

      subject(:api) { Postcode::API.new("valid") }

      it 'returns an error response' do
        result = api.postcode('5041EB')

        result.success.should be_true
        result.resource.street.should eq("Wilhelminapark")
        result.resource.postcode.should eq("5041EB")
        result.resource.town.should eq("Tilburg")
        result.resource.latitude.should eq(51.9401)
        result.resource.longitude.should eq(5.61531)
      end
    end

    context "with postcode and house number" do
      before do<commit_msg>Add failing test for sanitation<commit_after>require File.expand_path('../../spec_helper', __FILE__)

describe Postcode::API do
  context "without a valid API key" do
    before do
      body = '{"success":false,"error":{"code":401,"message":"A valid \'Api-Key\' needs to be provided in the \'Headers\' in order to use this API."}}'
      stub_request(:get, "http://api.postcodeapi.nu/5041EB").
        with(:headers => {'Api-Key'=>'invalid'}).
        to_return(:status => 401, :body => body, :headers => {"Content-Type" => "application/json"})
    end

    subject(:api) { Postcode::API.new("invalid") }

    it 'returns an error response' do
      result = api.postcode('5041EB')

      result.success.should be_false
      result.error.code.should eq(401)
    end
  end

  context "with a valid API key" do
    context "with postcode only" do
      before do
        body = '{"success":true,"resource":{"street":"Wilhelminapark","postcode":"5041EB","town":"Tilburg","latitude":51.9401,"longitude":5.61531}}'
        stub_request(:get, "http://api.postcodeapi.nu/5041EB").
          with(:headers => {'Api-Key'=>'valid'}).
          to_return(:status => 401, :body => body, :headers => {"Content-Type" => "application/json"})
      end

      subject(:api) { Postcode::API.new("valid") }

      it 'returns an error response' do
        result = api.postcode('5041EB')

        result.success.should be_true
        result.resource.street.should eq("Wilhelminapark")
        result.resource.postcode.should eq("5041EB")
        result.resource.town.should eq("Tilburg")
        result.resource.latitude.should eq(51.9401)
        result.resource.longitude.should eq(5.61531)
      end

      it 'sanitizes input before lookup' do
        result = api.postcode('5041 eb')

        result.success.should be_true
        result.resource.postcode.should eq("5041EB")
      end
    end

    context "with postcode and house number" do
      before do
{'fraction_non_alphanumeric': 0.15799457994579946, 'fraction_numerical': 0.034688346883468835, 'mean_word_length': 3.064977973568282, 'pattern_counts': {'":': 22, '<': 3, '<?xml version=': 0, '>': 27, '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': '4124231', 'n_tokens_mistral': 1222, 'n_tokens_neox': 1112, 'n_words': 259}