Sha256: 9aa52432870a28309b8cbf23ae97b89c31c4b3f8d78fce126b3eb12b0c4945af

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

require 'nitro/adapter/webrick'

class Webrick

# VCR functionality, typically used for proxy based regression
# testing.

class VCR

  class << self
  
  # Enables session recording. The recorded data can be used 
  # for automatic app testing by means of the playback mode.
  
  def record(filename = 'session.yaml')
    Logger.info "Recording application server session to '#{filename}'."
    
    require 'facets/core/file/self/create'

    $record_session = []
    $last_record_time = Time.now

    Nitro::WebrickAdapter.class_eval %{   
      def do_GET(req, res)
        record_context(req, res)
        handle(req, res)
      end
      alias_method :do_POST, :do_GET

      def record_context(req, res)
        delta = Time.now - $last_record_time
        $last_record_time = Time.now
        $record_session << [delta, req, res]
      end          
    }

    at_exit do
      File.create(filename, YAML.dump($record_session))
    end
  end
  
  # Playback a recorded session. Typically used for testing.
  
  def playback(filename = 'session.yaml')
    Logger.info "Playing back application server session from '#{filename}'."
    
    $playback_session = YAML.load_file(filename)
    $playback_exception_count = 0
        
    WEBrick::HTTPServer.class_eval %{
      def start(&block)
        run(nil)
      end
      
      def run(sock)
        while true
          delta, req, res = $playback_session.shift
          
          if delta
            sleep(delta)
            begin
              service(req, res)
            rescue Object => ex
              $playback_exception_count += 1
              p '---', ex                
            end
          else
            return
          end
        end
      end
    }
    
    at_exit do
      puts "\n\n"
      puts "Playback raised #$playback_exception_count exceptions.\n"
      puts "\n"
    end
  end

  end
  
end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nitro-0.41.0 lib/nitro/adapter/webrick/vcr.rb
nitro-0.40.0 lib/nitro/adapter/webrick/vcr.rb