<commit_before>require 'rack/translating_proxy'

module CybersourceryTesting
  class TranslatingProxy < Rack::TranslatingProxy
    alias :super_call :call

    def initialize(app)
      @app = app
    end

    def call(env)
      if proxy?(env)
        request = Rack::Request.new(env)
        set_referrer(request)
        check_for_cryptic_cybersource_errors(request)
        maybe_use_vcr(env)
      else
        @app.call(env)
      end
    end

    def proxy?(env)
      # The browser keeps requesting favicon.ico, which throws errors when the request is forwarded
      # to the Cybersource server. So ony forward POST requests.
      env['REQUEST_METHOD'] == 'POST'
    end

    def set_referrer(request)
      # We are making @referrer an instance variable for convenience. We need it since the port of
      # the test server can change with every test run. Conceptually, http_referrer should be part
      # of env, but env is not an instance variable. We would have to rewrite multiple method
      # signatures and a bunch of calls in the parent translating_proxy.rb if we put the referrer in
      # env (we need it in request_mapping(), which is at the end of a chain of calls).
      @referrer = request.referrer ? URI(request.referrer) : nil
    end

    def check_for_cryptic_cybersource_errors(request)
      request.params.each do |k,v|
        if v.class != String
          raise "You are attempting to pass a value that is not a String to Cybersource. This will cause Cybersource to throw a generic server error. You passed: #{k}: #{v.to_s}"
        end

        if v.length >= 700
          raise "You are attempting to pass a value to Cybersource that is 700 characters or greater. This will cause Cybersource to throw a generic server error. You passed: #{k}: #{v}"
        end
      end
    end

    def maybe_use_vcr(env)
      # TODO: This *almost* works properly. Using Shotgun lets us sidestep the problem.
      #
      # VCR will work fine on the first test run. But without Shotgun, then the Sinatra server gets
      # into a strange state. Subsequent test runs will hang, and canceling Sinatra isn't sufficient
      # to stop it (you need to enter an explicit kill command). This seems to have to do with
      # running VCR in the context of this middleware, which is not the environment for which it was
      # designed.
      if ENV['CYBERSOURCERY_USE_VCR_IN_TESTS']
        VCR.use_cassette(
          'cybersourcery',
          record: :new_episodes,
          match_requests_on: CybersourceryTesting::Vcr.match_requests_on
        ) do
          super_call(env)
        end
      else
        super_call(env)
      end
    end

    def target_host
      ENV['CYBERSOURCERY_SOP_TEST_URL']<commit_msg>it's only the signed_field_names where the 700 character limit comes into play!<commit_after>require 'rack/translating_proxy'

module CybersourceryTesting
  class TranslatingProxy < Rack::TranslatingProxy
    alias :super_call :call

    def initialize(app)
      @app = app
    end

    def call(env)
      if proxy?(env)
        request = Rack::Request.new(env)
        set_referrer(request)
        check_for_cryptic_cybersource_errors(request)
        maybe_use_vcr(env)
      else
        @app.call(env)
      end
    end

    def proxy?(env)
      # The browser keeps requesting favicon.ico, which throws errors when the request is forwarded
      # to the Cybersource server. So ony forward POST requests.
      env['REQUEST_METHOD'] == 'POST'
    end

    def set_referrer(request)
      # We are making @referrer an instance variable for convenience. We need it since the port of
      # the test server can change with every test run. Conceptually, http_referrer should be part
      # of env, but env is not an instance variable. We would have to rewrite multiple method
      # signatures and a bunch of calls in the parent translating_proxy.rb if we put the referrer in
      # env (we need it in request_mapping(), which is at the end of a chain of calls).
      @referrer = request.referrer ? URI(request.referrer) : nil
    end

    def check_for_cryptic_cybersource_errors(request)
      request.params.each do |k,v|
        if v.class != String
          raise "You are attempting to pass a value that is not a String to Cybersource. This will cause Cybersource to throw a generic server error. You passed: #{k}: #{v.to_s}"
        end

        if k == 'signed_field_names' && v.length >= 700
          raise "You are attempting to pass a signed_fields value to Cybersource that is 700 characters or greater. This will cause Cybersource to throw a generic server error. You passed: #{k}: #{v}"
        end
      end
    end

    def maybe_use_vcr(env)
      # TODO: This *almost* works properly. Using Shotgun lets us sidestep the problem.
      #
      # VCR will work fine on the first test run. But without Shotgun, then the Sinatra server gets
      # into a strange state. Subsequent test runs will hang, and canceling Sinatra isn't sufficient
      # to stop it (you need to enter an explicit kill command). This seems to have to do with
      # running VCR in the context of this middleware, which is not the environment for which it was
      # designed.
      if ENV['CYBERSOURCERY_USE_VCR_IN_TESTS']
        VCR.use_cassette(
          'cybersourcery',
          record: :new_episodes,
          match_requests_on: CybersourceryTesting::Vcr.match_requests_on
        ) do
          super_call(env)
        end
      else
        super_call(env)
      end
    end

    def target_host
      ENV['CYBERSOURCERY_SOP_TEST_URL']
{'fraction_non_alphanumeric': 0.061505453245127836, 'fraction_numerical': 0.0026819238333631325, 'mean_word_length': 2.6730137885751803, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '362333', 'n_tokens_mistral': 1637, 'n_tokens_neox': 1586, 'n_words': 639}