Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add some information for developers. Fixes #218

  • Loading branch information...
commit 4d16ac9f7eb711f6425f9dc577230222b1bce70d 1 parent c1875ee
@drbrain drbrain authored
Showing with 57 additions and 2 deletions.
  1. +19 −2 README.rdoc
  2. +38 −0 lib/mechanize/test_case.rb
View
21 README.rdoc
@@ -28,8 +28,25 @@ The bug tracker is available here:
== Examples
-If you are just starting, check out the GUIDE.
-Also, check out the EXAMPLES file.
+If you are just starting, check out the GUIDE. Also, check out the EXAMPLES
+file.
+
+== Developers
+
+To run the tests for the first time:
+
+ gem install hoe rake
+ rake newb
+
+This will install all the required dependencies for running the tests. For
+subsequent test runs:
+
+ rake test
+
+You can also use +autotest+ from the ZenTest gem to run tests.
+
+See also Mechanize::TestCase to read about the built-in testing
+infrastructure.
== Authors
View
38 lib/mechanize/test_case.rb
@@ -19,6 +19,9 @@
# 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:
#
@@ -31,6 +34,9 @@ class Mechanize::TestCase < MiniTest::Unit::TestCase
TEST_DIR = File.expand_path '../../../test', __FILE__
REQUESTS = []
+ ##
+ # Creates a clean mechanize instance +@mech+ for use in tests.
+
def setup
super
@@ -40,6 +46,10 @@ def setup
@ssl_certificate = nil
end
+ ##
+ # Creates a fake page with URI http://fake.example and an empty, submittable
+ # form.
+
def fake_page agent = @mech
uri = URI 'http://fake.example/'
html = <<-END
@@ -53,15 +63,24 @@ def fake_page agent = @mech
Mechanize::Page.new uri, nil, html, 200, agent
end
+ ##
+ # Is the Encoding constant defined?
+
def have_encoding?
Object.const_defined? :Encoding
end
+ ##
+ # Creates a Mechanize::Page with the given +body+
+
def html_page body
uri = URI 'http://example/'
Mechanize::Page.new uri, nil, body, 200, @mech
end
+ ##
+ # Runs the block inside a temporary directory
+
def in_tmpdir
Dir.mktmpdir do |dir|
Dir.chdir dir do
@@ -70,6 +89,9 @@ def in_tmpdir
end
end
+ ##
+ # Creates a Nokogiri Node +element+ with the given +attributes+
+
def node element, attributes = {}
doc = Nokogiri::HTML::Document.new
@@ -82,6 +104,10 @@ def node element, attributes = {}
node
end
+ ##
+ # Creates a Mechanize::Page for the given +uri+ with the given
+ # +content_type+, response +body+ and HTTP status +code+
+
def page uri, content_type = 'text/html', body = '', code = 200
uri = URI uri unless URI::Generic === uri
@@ -89,10 +115,16 @@ def page uri, content_type = 'text/html', body = '', code = 200
@mech)
end
+ ##
+ # Requests made during this tests
+
def requests
REQUESTS
end
+ ##
+ # An SSL private key. This key is the same across all test runs
+
def ssl_private_key
@ssl_private_key ||= OpenSSL::PKey::RSA.new <<-KEY
-----BEGIN RSA PRIVATE KEY-----
@@ -104,6 +136,9 @@ def ssl_private_key
KEY
end
+ ##
+ # An X509 certificate. This certificate is the same across all test runs
+
def ssl_certificate
@ssl_certificate ||= OpenSSL::X509::Certificate.new <<-CERT
-----BEGIN CERTIFICATE-----
@@ -118,6 +153,9 @@ def ssl_certificate
CERT
end
+ ##
+ # Creates a Tempfile with +content+ that is immediately unlinked
+
def tempfile content
body_io = Tempfile.new @__name__
body_io.unlink
Please sign in to comment.
Something went wrong with that request. Please try again.