<commit_before>module WebMock
  
  class Response
    attr_reader :options

    def initialize(options = {})
      if options.is_a?(IO) || options.is_a?(String) 
          self.options = read_raw_response(options)
        else
          self.options = options
      end
      @options[:headers] = Util::Headers.normalize_headers(@options[:headers]) unless @options[:headers].is_a?(Proc)
    end

    def headers
      @options[:headers]
    end

    def body
      return '' unless @options.has_key?(:body)
      stringify_body!
      @options[:body]
    end

    def status
      @options.has_key?(:status) ? @options[:status] : 200
    end

    def raise_error_if_any
      raise @options[:exception].new('Exception from WebMock') if @options.has_key?(:exception)
    end

    def options=(options)
      @options = options
      stringify_body!
    end

    def dup
      dup_response = super
      dup_response.options = options.dup
      dup_response
    end

    def ==(other)
      options == other.options
    end
    
    private 

    def stringify_body!
      if @options[:body].is_a?(IO)
        io = @options[:body]
        @options[:body] = io.read
        io.close
      end
    end
    
    def read_raw_response(raw_response)
      if raw_response.is_a?(IO)
        string = raw_response.read 
        raw_response.close
        raw_response = string
      end        
      socket = Net::BufferedIO.new(raw_response)
      response = Net::HTTPResponse.read_new(socket)
      transfer_encoding = response.delete('transfer-encoding') #chunks were already read by curl
      response.reading_body(socket, true) {}
      
      options = {}
      options[:headers] = response.to_hash
      options[:headers]['transfer-encoding'] = transfer_encoding if transfer_encoding
      options[:body] = response.read_body
      options      
    end

  end
end<commit_msg>Added compatibility for reading recorded raw responses in Ruby 1.9.2 preview1<commit_after>#compatibility with Ruby 1.9.2 preview1 to allow reading raw responses
class StringIO
  alias_method :read_nonblock, :sysread
end

module WebMock
  
  class Response
    attr_reader :options

    def initialize(options = {})
      if options.is_a?(IO) || options.is_a?(String) 
          self.options = read_raw_response(options)
        else
          self.options = options
      end
      @options[:headers] = Util::Headers.normalize_headers(@options[:headers]) unless @options[:headers].is_a?(Proc)
    end

    def headers
      @options[:headers]
    end

    def body
      return '' unless @options.has_key?(:body)
      stringify_body!
      @options[:body]
    end

    def status
      @options.has_key?(:status) ? @options[:status] : 200
    end

    def raise_error_if_any
      raise @options[:exception].new('Exception from WebMock') if @options.has_key?(:exception)
    end

    def options=(options)
      @options = options
      stringify_body!
    end

    def dup
      dup_response = super
      dup_response.options = options.dup
      dup_response
    end

    def ==(other)
      options == other.options
    end
    
    private 

    def stringify_body!
      if @options[:body].is_a?(IO)
        io = @options[:body]
        @options[:body] = io.read
        io.close
      end
    end
    
    def read_raw_response(raw_response)
      if raw_response.is_a?(IO)
        string = raw_response.read 
        raw_response.close
        raw_response = string
      end        
      socket = Net::BufferedIO.new(raw_response)    
      response = Net::HTTPResponse.read_new(socket)
      transfer_encoding = response.delete('transfer-encoding') #chunks were already read by curl
      response.reading_body(socket, true) {}
      
      options = {}
      options[:headers] = response.to_hash
      options[:headers]['transfer-encoding'] = transfer_encoding if transfer_encoding
      options[:body] = response.read_body
      options      
    end

  end
end
{'fraction_non_alphanumeric': 0.09668438370032903, 'fraction_numerical': 0.003543406732472792, 'mean_word_length': 2.460595446584939, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2607595', 'n_tokens_mistral': 1174, 'n_tokens_neox': 1145, 'n_words': 259}