Class: Mechanize::TestCase

Inherits:
Minitest::Test
  • Object
show all
Defined in:
lib/mechanize/test_case.rb

Overview

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

Constant Summary collapse

TEST_DIR =
File.expand_path '../../../test', __FILE__
REQUESTS =
[]

Instance Method Summary collapse

Instance Method Details

Creates a Mechanize::CookieJar by parsing the given str


90
91
92
93
94
# File 'lib/mechanize/test_case.rb', line 90

def cookie_jar str, uri = URI('http://example')
  Mechanize::CookieJar.new.tap do |jar|
    jar.parse str, uri
  end
end

#fake_page(agent = @mech) ⇒ Object

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


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mechanize/test_case.rb', line 59

def fake_page agent = @mech
  uri = URI 'http://fake.example/'
  html = String.new(<<~END)
    <html>
    <body>
    <form><input type="submit" value="submit" /></form>
    </body>
    </html>
  END

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

#file_contents_without_cr(path) ⇒ Object

Return the contents of the file without Windows carriage returns


187
188
189
# File 'lib/mechanize/test_case.rb', line 187

def file_contents_without_cr(path)
  File.read(path).gsub(/\r\n/, "\n")
end

#have_encoding?Boolean

Is the Encoding constant defined?

Returns:

  • (Boolean)

75
76
77
# File 'lib/mechanize/test_case.rb', line 75

def have_encoding?
  Object.const_defined? :Encoding
end

#html_page(body) ⇒ Object

Creates a Mechanize::Page with the given body


82
83
84
85
# File 'lib/mechanize/test_case.rb', line 82

def html_page body
  uri = URI 'http://example/'
  Mechanize::Page.new uri, nil, body, 200, @mech
end

#in_tmpdirObject

Runs the block inside a temporary directory


99
100
101
102
103
104
105
# File 'lib/mechanize/test_case.rb', line 99

def in_tmpdir
  Dir.mktmpdir do |dir|
    Dir.chdir dir do
      yield
    end
  end
end

#node(element, attributes = {}) ⇒ Object

Creates a Nokogiri Node element with the given attributes


110
111
112
113
114
115
116
# File 'lib/mechanize/test_case.rb', line 110

def node element, attributes = {}
  Nokogiri::XML::Node.new(element, Nokogiri::HTML::Document.new).tap do |node|
    attributes.each do |name, value|
      node[name] = value
    end
  end
end

#page(uri, content_type = 'text/html', body = String.new, code = 200) ⇒ Object

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


122
123
124
125
126
127
# File 'lib/mechanize/test_case.rb', line 122

def page uri, content_type = 'text/html', body = String.new, code = 200
  uri = URI uri unless URI::Generic === uri

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

#requestsObject

Requests made during this tests


132
133
134
# File 'lib/mechanize/test_case.rb', line 132

def requests
  REQUESTS
end

#setupObject

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


46
47
48
49
50
51
52
53
# File 'lib/mechanize/test_case.rb', line 46

def setup
  super

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

#ssl_certificateObject

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


153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/mechanize/test_case.rb', line 153

def ssl_certificate
  @ssl_certificate ||= OpenSSL::X509::Certificate.new <<-CERT
-----BEGIN CERTIFICATE-----
MIIBQjCB7aADAgECAgEAMA0GCSqGSIb3DQEBBQUAMCoxDzANBgNVBAMMBm5vYm9k
eTEXMBUGCgmSJomT8ixkARkWB2V4YW1wbGUwIBcNMTExMTAzMjEwODU5WhgPOTk5
OTEyMzExMjU5NTlaMCoxDzANBgNVBAMMBm5vYm9keTEXMBUGCgmSJomT8ixkARkW
B2V4YW1wbGUwWjANBgkqhkiG9w0BAQEFAANJADBGAkEA8pmEfmP0Ibir91x6pbts
4JmmsVZd3xvD5p347EFvBCbhBW1nv1GsbCBEFlSiT1q2qvxGb5IlbrfdhdgyqdTX
UQIBATANBgkqhkiG9w0BAQUFAANBAAAB////////////////////////////////
//8AMCEwCQYFKw4DAhoFAAQUePiv+QrJxyjtEJNnH5pB9OTWIqA=
-----END CERTIFICATE-----
  CERT
end

#ssl_private_keyObject

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


139
140
141
142
143
144
145
146
147
148
# File 'lib/mechanize/test_case.rb', line 139

def ssl_private_key
  @ssl_private_key ||= OpenSSL::PKey::RSA.new <<-KEY
-----BEGIN RSA PRIVATE KEY-----
MIG7AgEAAkEA8pmEfmP0Ibir91x6pbts4JmmsVZd3xvD5p347EFvBCbhBW1nv1Gs
bCBEFlSiT1q2qvxGb5IlbrfdhdgyqdTXUQIBAQIBAQIhAPumXslvf6YasXa1hni3
p80joKOug2UUgqOLD2GUSO//AiEA9ssY6AFxjHWuwo/+/rkLmkfO2s1Lz3OeUEWq
6DiHOK8CAQECAQECIQDt8bc4vS6wh9VXApNSKIpVygtxSFe/IwLeX26n77j6Qg==
-----END RSA PRIVATE KEY-----
  KEY
end

#tempfile(content) ⇒ Object

Creates a Tempfile with content that is immediately unlinked


170
171
172
173
174
175
176
177
# File 'lib/mechanize/test_case.rb', line 170

def tempfile content
  Tempfile.new(@NAME).tap do |body_io|
    body_io.unlink
    body_io.write content
    body_io.flush
    body_io.rewind
  end
end

#windows?Boolean

Returns true if the current platform is a Windows platform

Returns:

  • (Boolean)

181
182
183
# File 'lib/mechanize/test_case.rb', line 181

def windows?
  ::RUBY_PLATFORM =~ /mingw|mswin/
end