Class: Mechanize::TestCase
- Inherits:
-
MiniTest::Unit::TestCase
- Object
- MiniTest::Unit::TestCase
- Mechanize::TestCase
- 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. '../../../test', __FILE__
- REQUESTS =
[]
Instance Method Summary collapse
-
#fake_page(agent = @mech) ⇒ Object
Creates a fake page with URI fake.example and an empty, submittable form.
-
#have_encoding? ⇒ Boolean
Is the Encoding constant defined?.
-
#html_page(body) ⇒ Object
Creates a Mechanize::Page with the given
body
. -
#in_tmpdir ⇒ Object
Runs the block inside a temporary directory.
-
#node(element, attributes = {}) ⇒ Object
Creates a Nokogiri Node
element
with the givenattributes
. -
#page(uri, content_type = 'text/html', body = '', code = 200) ⇒ Object
Creates a Mechanize::Page for the given
uri
with the givencontent_type
, responsebody
and HTTP statuscode
. -
#requests ⇒ Object
Requests made during this tests.
-
#setup ⇒ Object
Creates a clean mechanize instance @mech for use in tests.
-
#ssl_certificate ⇒ Object
An X509 certificate.
-
#ssl_private_key ⇒ Object
An SSL private key.
-
#tempfile(content) ⇒ Object
Creates a Tempfile with
content
that is immediately unlinked.
Instance Method Details
#fake_page(agent = @mech) ⇒ Object
Creates a fake page with URI fake.example and an empty, submittable form.
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mechanize/test_case.rb', line 53 def fake_page agent = @mech uri = URI 'http://fake.example/' html = <<-END <html> <body> <form><input type="submit" value="submit" /></form> </body> </html> END Mechanize::Page.new uri, nil, html, 200, agent end |
#have_encoding? ⇒ Boolean
Is the Encoding constant defined?
69 70 71 |
# File 'lib/mechanize/test_case.rb', line 69 def have_encoding? Object.const_defined? :Encoding end |
#html_page(body) ⇒ Object
Creates a Mechanize::Page with the given body
76 77 78 79 |
# 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 ⇒ Object
Runs the block inside a temporary directory
84 85 86 87 88 89 90 |
# File 'lib/mechanize/test_case.rb', line 84 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
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/mechanize/test_case.rb', line 95 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) ⇒ Object
Creates a Mechanize::Page for the given uri
with the given content_type
, response body
and HTTP status code
111 112 113 114 115 116 |
# File 'lib/mechanize/test_case.rb', line 111 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 ⇒ Object
Requests made during this tests
121 122 123 |
# File 'lib/mechanize/test_case.rb', line 121 def requests REQUESTS end |
#setup ⇒ Object
Creates a clean mechanize instance @mech for use in tests.
40 41 42 43 44 45 46 47 |
# 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 ⇒ Object
An X509 certificate. This certificate is the same across all test runs
142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/mechanize/test_case.rb', line 142 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_key ⇒ Object
An SSL private key. This key is the same across all test runs
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/mechanize/test_case.rb', line 128 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
159 160 161 162 163 164 165 166 167 |
# File 'lib/mechanize/test_case.rb', line 159 def tempfile content body_io = Tempfile.new @__name__ body_io.unlink body_io.write content body_io.flush body_io.rewind body_io end |