class RequestReplay
Constants
- CONTENT_LENGTH
- CONTENT_TYPE
- HEADER_CLENGTH
- HEADER_CTYPE
- HTTP_VERSION
- NEWLINE
- PATH_INFO
- QUERY_STRING
- RACK_ERRORS
- RACK_INPUT
- REQUEST_METHOD
Public Class Methods
new(env, host, options={})
click to toggle source
# File lib/request-replay.rb, line 21 def initialize env, host, options={} @env, (@host, @port), @options = env, host.split(':', 2), options if env[RACK_INPUT] env[RACK_INPUT].rewind @buf = StringIO.new IO.copy_stream(env[RACK_INPUT], @buf) @buf.rewind env[RACK_INPUT].rewind else @buf = nil end end
Public Instance Methods
add_headers()
click to toggle source
# File lib/request-replay.rb, line 34 def add_headers @options[:add_headers] || {} end
capitalize_headers(header)
click to toggle source
# File lib/request-replay.rb, line 91 def capitalize_headers header header.downcase.gsub(/[a-z]+/){ |s| s.capitalize }.tr('_', '-') end
headers()
click to toggle source
# File lib/request-replay.rb, line 72 def headers headers_hash.map{ |name, value| "#{name}: #{value}" }.join(NEWLINE) end
headers_hash()
click to toggle source
# File lib/request-replay.rb, line 81 def headers_hash @headers_hash ||= @env.inject({}){ |r, (k, v)| r[capitalize_headers(k[5..-1])] = v if k.start_with?('HTTP_') r }.merge(HEADER_CTYPE => @env[CONTENT_TYPE ], HEADER_CLENGTH => @env[CONTENT_LENGTH]). merge(add_headers).select{ |_, v| v } end
read_wait()
click to toggle source
# File lib/request-replay.rb, line 38 def read_wait @options[:read_wait] && Float(@options[:read_wait]) end
request()
click to toggle source
# File lib/request-replay.rb, line 68 def request "#{@env[REQUEST_METHOD] || 'GET'} #{request_path} #{HTTP_VERSION}" end
request_path()
click to toggle source
# File lib/request-replay.rb, line 76 def request_path "/#{@env[PATH_INFO]}?#{@env[QUERY_STRING]}". sub(%r{^//}, '/').sub(/\?$/, '') end
sock()
click to toggle source
# File lib/request-replay.rb, line 95 def sock @sock ||= TCPSocket.new(@host, Integer(@port || 80)) end
start() { |sock| ... }
click to toggle source
# File lib/request-replay.rb, line 42 def start write_request write_headers write_payload IO.select([sock], [], [], read_wait) if read_wait yield(sock) if block_given? rescue => e @env[RACK_ERRORS].puts("[#{self.class.name}] Error: #{e.inspect}") if @env[RACK_ERRORS] ensure sock.close end
write_headers()
click to toggle source
# File lib/request-replay.rb, line 59 def write_headers sock.write("#{headers}#{NEWLINE}#{NEWLINE}") end
write_payload()
click to toggle source
# File lib/request-replay.rb, line 63 def write_payload return unless @buf IO.copy_stream(@buf, sock) end
write_request()
click to toggle source
# File lib/request-replay.rb, line 55 def write_request sock.write("#{request}#{NEWLINE}") end