Class: Mechanize::TestCase

### Home Classes Methods

### In Files

- lib/mechanize/test_case.rb

### Parent

Minitest::Test

### Methods

- #cookie_jar
- #fake_page
- #have_encoding?
- #html_page
- #in_tmpdir
- #node
- #page
- #requests
- #setup
- #ssl_certificate
- #ssl_private_key
- #tempfile

### Files

- EXAMPLES.rdoc
- GUIDE.rdoc
- LICENSE.rdoc
- Manifest.txt
- README.rdoc

### Class/Module Index

Quicksearch

- Mechanize
- Mechanize::ChunkedTerminationError
- Mechanize::ContentTypeError
- Mechanize::Cookie
- Mechanize::CookieCMethods
- Mechanize::CookieDeprecated
- Mechanize::CookieIMethods
- Mechanize::CookieJar
- Mechanize::CookieJarIMethods
- Mechanize::DirectorySaver
- Mechanize::Download
- Mechanize::ElementMatcher
- Mechanize::ElementNotFoundError
- Mechanize::Error
- Mechanize::File
- Mechanize::FileConnection
- Mechanize::FileRequest
- Mechanize::FileResponse
- Mechanize::FileSaver
- Mechanize::Form
- Mechanize::Form::Button
- Mechanize::Form::CheckBox
- Mechanize::Form::Field
- Mechanize::Form::FileUpload
- Mechanize::Form::Hidden
- Mechanize::Form::ImageButton
- Mechanize::Form::Keygen
- Mechanize::Form::MultiSelectList
- Mechanize::Form::Option
- Mechanize::Form::RadioButton
- Mechanize::Form::Reset
- Mechanize::Form::SelectList
- Mechanize::Form::Submit
- Mechanize::Form::Text
- Mechanize::Form::Textarea
- Mechanize::HTTP
- Mechanize::HTTP::Agent
- Mechanize::HTTP::AuthChallenge
- Mechanize::HTTP::AuthRealm
- Mechanize::HTTP::AuthStore
- Mechanize::HTTP::ContentDispositionParser
- Mechanize::HTTP::WWWAuthenticateParser
- Mechanize::Headers
- Mechanize::History
- Mechanize::Image
- Mechanize::Page
- Mechanize::Page::Base
- Mechanize::Page::Frame
- Mechanize::Page::Image
- Mechanize::Page::Label
- Mechanize::Page::Link
- Mechanize::Page::MetaRefresh
- Mechanize::Parser
- Mechanize::PluggableParser
- Mechanize::Prependable
- Mechanize::RedirectLimitReachedError
- Mechanize::RedirectNotGetOrHeadError
- Mechanize::ResponseCodeError
- Mechanize::ResponseReadError
- Mechanize::RobotsDisallowedError
- Mechanize::TestCase
- Mechanize::UnauthorizedError
- Mechanize::UnsupportedSchemeError
- Mechanize::Util
- Mechanize::XmlFile
- HTTP
- HTTP::CookieJar

No matching classes.

# Mechanize::TestCase

A generic test case for testing mechanize. Using a subclass of Mechanize::TestCase for your tests will create an isolated mechanize instance that won't pollute your filesystem or other tests.

Once Mechanize::TestCase is loaded no HTTP requests will be made outside mechanize itself. All requests are handled via WEBrick servlets.

Mechanize uses WEBrick servlets to test some functionality. You can run other HTTP clients against the servlets using:

    ruby -rmechanize/test_case/server -e0

Which will launch a test server at localhost:8000

### Constants

REQUESTS
TEST_DIR

### Public Instance Methods

cookie_jar(str, uri = URI('http://example')) click to toggle source

Creates a Mechanize::CookieJar by parsing the given `str`

    # File lib/mechanize/test_case.rb, line 84
    def cookie_jar str, uri = URI('http://example')
      jar = Mechanize::CookieJar.new

      jar.parse str, uri

      jar
    end

fake_page(agent = @mech) click to toggle source

Creates a fake page with URI fake.example and an empty, submittable form.

    # File lib/mechanize/test_case.rb, line 53
    def fake_page agent = @mech
      uri = URI 'http://fake.example/'
      html = <html><body><form><input type="submit" value="submit" /></form></body></html>

      Mechanize::Page.new uri, nil, html, 200, agent
    end

have_encoding?() click to toggle source

Is the Encoding constant defined?

    # File lib/mechanize/test_case.rb, line 69
    def have_encoding?
      Object.const_defined? :Encoding
    end

html_page(body) click to toggle source

Creates a Mechanize::Page with the given `body`

    # File lib/mechanize/test_case.rb, line 76
    def html_page body
      uri = URI 'http://example/'
      Mechanize::Page.new uri, nil, body, 200, @mech
    end

in_tmpdir() click to toggle source

Runs the block inside a temporary directory

    # File lib/mechanize/test_case.rb, line 95
    def in_tmpdir
      Dir.mktmpdir do |dir|
        Dir.chdir dir do
          yield
        end
      end
    end

node(element, attributes = {}) click to toggle source

Creates a Nokogiri Node `element` with the given `attributes`

    # File lib/mechanize/test_case.rb, line 106
    def node element, attributes = {}
      doc = Nokogiri::HTML::Document.new

      node = Nokogiri::XML::Node.new element, doc

      attributes.each do |name, value|
        node[name] = value
      end

      node
    end

page(uri, content_type = 'text/html', body = '', code = 200) click to toggle source

Creates a Mechanize::Page for the given `uri` with the given `content_type`, response `body` and HTTP status `code`

    # File lib/mechanize/test_case.rb, line 122
    def page uri, content_type = 'text/html', body = '', code = 200
      uri = URI uri unless URI::Generic === uri

      Mechanize::Page.new(uri, { 'content-type' => content_type }, body, code,
                          @mech)
    end

requests() click to toggle source

Requests made during this tests

    # File lib/mechanize/test_case.rb, line 132
    def requests
      REQUESTS
    end

setup() click to toggle source

Creates a clean mechanize instance +@mech+ for use in tests.

    # File lib/mechanize/test_case.rb, line 40
    def setup
      super

      REQUESTS.clear
      @mech = Mechanize.new
      @ssl_private_key = nil
      @ssl_certificate = nil
    end

ssl_certificate() click to toggle source

An X509 certificate. This certificate is the same across all test runs

    # File lib/mechanize/test_case.rb, line 153
    def ssl_certificate
      @ssl_certificate ||= OpenSSL::X509::Certificate.new -----BEGIN CERTIFICATE-----MIIBQjCB7aADAgECAgEAMA0GCSqGSIb3DQEBBQUAMCoxDzANBgNVBAMMBm5vYm9keTEXMBUGCgmSJomT8ixkARkWB2V4YW1wbGUwIBcNMTExMTAzMjEwODU5WhgPOTk5OTEyMzExMjU5NTlaMCoxDzANBgNVBAMMBm5vYm9keTEXMBUGCgmSJomT8ixkARkWB2V4YW1wbGUwWjANBgkqhkiG9w0BAQEFAANJADBGAkEA8pmEfmP0Ibir91x6pbts4JmmsVZd3xvD5p347EFvBCbhBW1nv1GsbCBEFlSiT1q2qvxGb5IlbrfdhdgyqdTXUQIBATANBgkqhkiG9w0BAQUFAANBAAAB//////////////////////////////////8AMCEwCQYFKw4DAhoFAAQUePiv+QrJxyjtEJNnH5pB9OTWIqA=-----END CERTIFICATE-----
    end

ssl_private_key() click to toggle source

An SSL private key. This key is the same across all test runs

    # File lib/mechanize/test_case.rb, line 139
    def ssl_private_key
      @ssl_private_key ||= OpenSSL::PKey::RSA.new -----BEGIN RSA PRIVATE KEY-----MIG7AgEAAkEA8pmEfmP0Ibir91x6pbts4JmmsVZd3xvD5p347EFvBCbhBW1nv1GsbCBEFlSiT1q2qvxGb5IlbrfdhdgyqdTXUQIBAQIBAQIhAPumXslvf6YasXa1hni3p80joKOug2UUgqOLD2GUSO//AiEA9ssY6AFxjHWuwo/+/rkLmkfO2s1Lz3OeUEWq6DiHOK8CAQECAQECIQDt8bc4vS6wh9VXApNSKIpVygtxSFe/IwLeX26n77j6Qg==-----END RSA PRIVATE KEY-----
    end

tempfile(content) click to toggle source

Creates a Tempfile with `content` that is immediately unlinked

    # File lib/mechanize/test_case.rb, line 170
    def tempfile content
      body_io = Tempfile.new @NAME
      body_io.unlink
      body_io.write content
      body_io.flush
      body_io.rewind

      body_io
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.