MiniTest::Unit::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
Creates a fake page with URI fake.example and an empty, submittable form.
# File lib/mechanize/test_case.rb, line 53 53: def fake_page agent = @mech 54: uri = URI 'http://fake.example/' 55: html = <html><body><form><input type="submit" value="submit" /></form></body></html> 56: 57: Mechanize::Page.new uri, nil, html, 200, agent 58: end
Is the Encoding constant defined?
# File lib/mechanize/test_case.rb, line 69 69: def have_encoding? 70: Object.const_defined? :Encoding 71: end
Creates a Mechanize::Page with the given body
# File lib/mechanize/test_case.rb, line 76 76: def html_page body 77: uri = URI 'http://example/' 78: Mechanize::Page.new uri, nil, body, 200, @mech 79: end
Runs the block inside a temporary directory
# File lib/mechanize/test_case.rb, line 84 84: def in_tmpdir 85: Dir.mktmpdir do |dir| 86: Dir.chdir dir do 87: yield 88: end 89: end 90: end
Creates a Nokogiri Node element with the given attributes
# File lib/mechanize/test_case.rb, line 95 95: def node element, attributes = {} 96: doc = Nokogiri::HTML::Document.new 97: 98: node = Nokogiri::XML::Node.new element, doc 99: 100: attributes.each do |name, value| 101: node[name] = value 102: end 103: 104: node 105: end
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 111 111: def page uri, content_type = 'text/html', body = '', code = 200 112: uri = URI uri unless URI::Generic === uri 113: 114: Mechanize::Page.new(uri, { 'content-type' => content_type }, body, code, 115: @mech) 116: end
Requests made during this tests
# File lib/mechanize/test_case.rb, line 121 121: def requests 122: REQUESTS 123: end
Creates a clean mechanize instance +@mech+ for use in tests.
# File lib/mechanize/test_case.rb, line 40 40: def setup 41: super 42: 43: REQUESTS.clear 44: @mech = Mechanize.new 45: @ssl_private_key = nil 46: @ssl_certificate = nil 47: end
An X509 certificate. This certificate is the same across all test runs
# File lib/mechanize/test_case.rb, line 142 142: def ssl_certificate 143: @ssl_certificate ||= OpenSSL::X509::Certificate.new -----BEGIN CERTIFICATE-----MIIBQjCB7aADAgECAgEAMA0GCSqGSIb3DQEBBQUAMCoxDzANBgNVBAMMBm5vYm9keTEXMBUGCgmSJomT8ixkARkWB2V4YW1wbGUwIBcNMTExMTAzMjEwODU5WhgPOTk5OTEyMzExMjU5NTlaMCoxDzANBgNVBAMMBm5vYm9keTEXMBUGCgmSJomT8ixkARkWB2V4YW1wbGUwWjANBgkqhkiG9w0BAQEFAANJADBGAkEA8pmEfmP0Ibir91x6pbts4JmmsVZd3xvD5p347EFvBCbhBW1nv1GsbCBEFlSiT1q2qvxGb5IlbrfdhdgyqdTXUQIBATANBgkqhkiG9w0BAQUFAANBAAAB//////////////////////////////////8AMCEwCQYFKw4DAhoFAAQUePiv+QrJxyjtEJNnH5pB9OTWIqA=-----END CERTIFICATE----- 144: end
An SSL private key. This key is the same across all test runs
# File lib/mechanize/test_case.rb, line 128 128: def ssl_private_key 129: @ssl_private_key ||= OpenSSL::PKey::RSA.new -----BEGIN RSA PRIVATE KEY-----MIG7AgEAAkEA8pmEfmP0Ibir91x6pbts4JmmsVZd3xvD5p347EFvBCbhBW1nv1GsbCBEFlSiT1q2qvxGb5IlbrfdhdgyqdTXUQIBAQIBAQIhAPumXslvf6YasXa1hni3p80joKOug2UUgqOLD2GUSO//AiEA9ssY6AFxjHWuwo/+/rkLmkfO2s1Lz3OeUEWq6DiHOK8CAQECAQECIQDt8bc4vS6wh9VXApNSKIpVygtxSFe/IwLeX26n77j6Qg==-----END RSA PRIVATE KEY----- 130: end
Creates a Tempfile with content that is immediately unlinked
# File lib/mechanize/test_case.rb, line 159 159: def tempfile content 160: body_io = Tempfile.new @__name__ 161: body_io.unlink 162: body_io.write content 163: body_io.flush 164: body_io.rewind 165: 166: body_io 167: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.