Sha256: 982e371fe5d7b0ae792ca83b3ab30a9b1a19f1f89c632bf6dfde2221ac11f6d6

Contents?: true

Size: 1.31 KB

Versions: 21

Compression:

Stored size: 1.31 KB

Contents

##
# Fake response for dealing with file:/// requests

class Mechanize::FileResponse
  def initialize(file_path)
    @file_path = file_path
  end

  def read_body
    raise Mechanize::ResponseCodeError, self unless File.exist? @file_path

    if directory?
      yield dir_body
    else
      open @file_path, 'rb' do |io|
        yield io.read
      end
    end
  end

  def code
    File.exist?(@file_path) ? 200 : 404
  end

  def content_length
    return dir_body.length if directory?
    File.exist?(@file_path) ? File.stat(@file_path).size : 0
  end

  def each_header; end

  def [](key)
    return nil unless key.downcase == 'content-type'
    return 'text/html' if directory?
    return 'text/html' if ['.html', '.xhtml'].any? { |extn|
      @file_path =~ /#{extn}$/
    }
    nil
  end

  def each
  end

  def get_fields(key)
    []
  end

  def http_version
    '0'
  end

  def message
    File.exist?(@file_path) ? 'OK' : 'Not Found'
  end

  private

  def dir_body
    body = %w[<html><body>]
    body.concat Dir[File.join(@file_path, '*')].map { |f|
      "<a href=\"file://#{f}\">#{File.basename(f)}</a>"
    }
    body << %w[</body></html>]

    body = body.join "\n"
    body.force_encoding Encoding::BINARY if body.respond_to? :force_encoding
    body
  end

  def directory?
    File.directory?(@file_path)
  end
end

Version data entries

21 entries across 21 versions & 6 rubygems

Version Path
frameworks-capybara-0.2.0.rc6 vendor/bundle/ruby/1.8/gems/mechanize-2.3/lib/mechanize/file_response.rb
frameworks-capybara-0.2.0.rc5 vendor/bundle/ruby/1.8/gems/mechanize-2.3/lib/mechanize/file_response.rb
frameworks-capybara-0.2.0.rc4 vendor/bundle/ruby/1.8/gems/mechanize-2.3/lib/mechanize/file_response.rb
frameworks-capybara-0.2.0.rc3 vendor/bundle/ruby/1.8/gems/mechanize-2.3/lib/mechanize/file_response.rb
frameworks-capybara-0.2.0.rc2 vendor/bundle/ruby/1.8/gems/mechanize-2.3/lib/mechanize/file_response.rb
mechanize-2.3 lib/mechanize/file_response.rb
mechanize-2.2.1 lib/mechanize/file_response.rb
mechanize-2.2 lib/mechanize/file_response.rb
mechanize-2.1.1 lib/mechanize/file_response.rb
domo-0.0.5 vendor/bundle/ruby/1.9.1/gems/mechanize-2.1/lib/mechanize/file_response.rb
diamond-mechanize-2.4 lib/mechanize/file_response.rb
diamond-mechanize-2.3 lib/mechanize/file_response.rb
diamond-mechanize-2.1 lib/mechanize/file_response.rb
mechanize-2.1 lib/mechanize/file_response.rb
mechanize-2.1.pre.1 lib/mechanize/file_response.rb
aai10-mechanize-2.0.1.0 lib/mechanize/file_response.rb
neocoin-mechanize-2.0.2 lib/mechanize/file_response.rb
mechanize-2.0.1 lib/mechanize/file_response.rb
mechanize-2.0 lib/mechanize/file_response.rb
mechanize-2.0.pre.2 lib/mechanize/file_response.rb