Skip to content

Commit

Permalink
adding verb support
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Sep 16, 2008
1 parent 4276399 commit 7130e44
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
2 changes: 2 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
* file:/// urls are now supported
* Added Mechanize::Page#link_with, frame_with for searching for links using
+criteria+.
* Implementing PUT, DELETE, and HEAD requests

* Bug Fixes:
* Fixed an infinite loop when content-length and body length don't match.
* Only setting headers once

=== 0.7.8

Expand Down
37 changes: 37 additions & 0 deletions lib/www/mechanize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,43 @@ def get(options, parameters = [], referer = nil)
yield page if block_given?
page
end

####
# PUT to +url+ with +query_params+, and setting +options+:
#
# put('http://tenderlovemaking.com/', {'q' => 'foo'}, :headers => {})
#
def put(url, query_params = {}, options = {})
options = {
:uri => url,
:headers => {},
:params => query_params,
:verb => :put
}.merge(options)
# fetch the page
page = fetch_page(options)
add_to_history(page)
yield page if block_given?
page
end

####
# DELETE to +url+ with +query_params+, and setting +options+:
#
# delete('http://tenderlovemaking.com/', {'q' => 'foo'}, :headers => {})
#
def delete(url, query_params = {}, options = {})
put(url, query_params, options.merge({:verb => :delete}))
end

####
# HEAD to +url+ with +query_params+, and setting +options+:
#
# head('http://tenderlovemaking.com/', {'q' => 'foo'}, :headers => {})
#
def head(url, query_params = {}, options = {})
put(url, query_params, options.merge({:verb => :head}))
end

# Fetch a file and return the contents of the file.
def get_file(url)
Expand Down
2 changes: 1 addition & 1 deletion lib/www/mechanize/chain/header_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def handle(ctx, params)
end

# Add Referer header to request
unless referer.uri.nil?
if referer && referer.uri
request['Referer'] = referer.uri.to_s
end

Expand Down
1 change: 1 addition & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def do_start
'/http_headers' => HeaderServlet,
'/infinite_redirect' => InfiniteRedirectTest,
'/digest_auth' => DigestAuthServlet,
'/verb' => VerbServlet,
}

PAGE_CACHE = {}
Expand Down
12 changes: 12 additions & 0 deletions test/servlets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
require 'stringio'
require 'base64'

class VerbServlet < WEBrick::HTTPServlet::AbstractServlet
def do_HEAD(req, res)
res.body = "method: HEAD"
end

def method_missing(method, *args, &block)
super unless method.to_s =~ /^do_([A-Z]*)$/
res = args[1]
res.body = "method: #{$1}"
end
end

class BasicAuthServlet < WEBrick::HTTPServlet::AbstractServlet
def do_GET(req,res)
htpd = WEBrick::HTTPAuth::Htpasswd.new('dot.htpasswd')
Expand Down
22 changes: 22 additions & 0 deletions test/test_verbs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))

class VerbsTest < Test::Unit::TestCase
def setup
@agent = WWW::Mechanize.new
end

def test_put
page = @agent.put('http://localhost/verb', { 'q' => 'foo' })
assert_equal('method: PUT', page.body)
end

def test_delete
page = @agent.delete('http://localhost/verb', { 'q' => 'foo' })
assert_equal('method: DELETE', page.body)
end

def test_head
page = @agent.head('http://localhost/verb', { 'q' => 'foo' })
assert_equal('method: HEAD', page.body)
end
end

0 comments on commit 7130e44

Please sign in to comment.