Sha256: 46ba4e8c5ec569e56492362241e29ce329f9b746c1a6137d34069e416abcd28a

Contents?: true

Size: 1.75 KB

Versions: 6

Compression:

Stored size: 1.75 KB

Contents

# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.

module FlakyProxy
  class Rule
    class ActionBuilder
      def initialize(rule)
        @actions = []
        @rule = rule
      end

      def pass
        @rule.actions << [:pass]
      end

      def respond(response_spec)
        @rule.actions << [:respond, response_spec]
      end

      def delay(amount)
        @rule.actions << [:delay, amount]
      end

      def close
        @rule.actions << [:close]
      end
    end

    attr_reader :actions

    def initialize(url_regex=nil, &blk)
      @url_regex = url_regex
      @actions = []
      ActionBuilder.new(self).instance_eval(&blk)
    end

    def match?(request)
      @url_regex.nil? || @url_regex.match(request.request_url)
    end

    def next_action
      if @actions.size > 1
        @actions.shift
      else
        @actions.last
      end
    end

    def relay(request, connection)
      request.relay_to(connection.server_socket)
      response = Response.read_from(connection.server_socket)
      response.relay_to(connection.client_socket)
    end

    def evaluate(request, connection)
      action, *params = next_action
      FlakyProxy.logger.info("    [#{action.upcase}] #{request.request_method} #{request.request_url}")
      case action
      when :pass
        relay(request, connection)
      when :respond
        response_spec = params.first
        response = Response.build(response_spec)
        response.relay_to(connection.client_socket)
      when :delay
        amount = params.first
        sleep(amount)
        relay(request, connection)
      when :close
        connection.client_socket.close
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
newrelic_rpm-3.7.2.195 test/flaky_proxy/lib/flaky_proxy/rule.rb
newrelic_rpm-3.7.2.192 test/flaky_proxy/lib/flaky_proxy/rule.rb
newrelic_rpm-3.7.2.190.beta test/flaky_proxy/lib/flaky_proxy/rule.rb
newrelic_rpm-3.7.1.188 test/flaky_proxy/lib/flaky_proxy/rule.rb
newrelic_rpm-3.7.1.182 test/flaky_proxy/lib/flaky_proxy/rule.rb
newrelic_rpm-3.7.1.180 test/flaky_proxy/lib/flaky_proxy/rule.rb