Add support for Basic Auth
This commit is contained in:
parent
e2c70b8e91
commit
17cf56eb5e
@ -2,6 +2,7 @@
|
||||
|
||||
* Major enhancements
|
||||
|
||||
* Added #basic_auth(user, pass) to support HTTP Basic Auth (Aslak Hellesøy)
|
||||
* Added support for Sinatra and Rack (Aslak Hellesøy)
|
||||
* Added #within for manipulating the current page within a selector scope
|
||||
* Add support for simulating SSL requests (Luke Melia)
|
||||
|
@ -12,6 +12,7 @@ module Webrat
|
||||
def initialize
|
||||
@http_method = :get
|
||||
@data = {}
|
||||
@default_headers = {}
|
||||
end
|
||||
|
||||
# Saves the page out to RAILS_ROOT/tmp/ and opens it in the default
|
||||
@ -52,12 +53,23 @@ module Webrat
|
||||
File.expand_path(".")
|
||||
end
|
||||
|
||||
def basic_auth(user, pass)
|
||||
@default_headers['HTTP_AUTHORIZATION'] = "Basic " + ["#{user}:#{pass}"].pack("m*")
|
||||
end
|
||||
|
||||
def headers
|
||||
@default_headers.dup
|
||||
end
|
||||
|
||||
def request_page(url, http_method, data)
|
||||
debug_log "REQUESTING PAGE: #{http_method.to_s.upcase} #{url} with #{data.inspect}"
|
||||
if @current_url
|
||||
send "#{http_method}", url, data || {}, {"HTTP_REFERER" => @current_url}
|
||||
else
|
||||
h = headers
|
||||
h['HTTP_REFERER'] = @current_url if @current_url
|
||||
|
||||
debug_log "REQUESTING PAGE: #{http_method.to_s.upcase} #{url} with #{data.inspect} and HTTP headers #{h.inspect}"
|
||||
if h.empty?
|
||||
send "#{http_method}", url, data || {}
|
||||
else
|
||||
send "#{http_method}", url, data || {}, h
|
||||
end
|
||||
|
||||
save_and_open_page if exception_caught?
|
||||
|
@ -7,9 +7,10 @@ module Webrat
|
||||
include Sinatra::Test::Methods
|
||||
|
||||
%w(get head post put delete).each do |verb|
|
||||
define_method(verb) do |*args|
|
||||
url, data, headers = *args
|
||||
self.__send__("#{verb}_it", url, data)
|
||||
define_method(verb) do |*args| # (path, data, headers = nil)
|
||||
path, data, headers = *args
|
||||
params = data.merge({:env => headers || {}})
|
||||
self.__send__("#{verb}_it", path, params)
|
||||
follow! while @response.redirect?
|
||||
end
|
||||
end
|
||||
|
23
spec/api/basic_auth_spec.rb
Normal file
23
spec/api/basic_auth_spec.rb
Normal file
@ -0,0 +1,23 @@
|
||||
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
||||
|
||||
describe "Basic Auth HTTP headers" do
|
||||
before do
|
||||
@session = Webrat::TestSession.new
|
||||
@session.basic_auth('user', 'secret')
|
||||
end
|
||||
|
||||
it "should be present in visits" do
|
||||
@session.should_receive(:get).with("/", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"})
|
||||
@session.visits("/")
|
||||
end
|
||||
|
||||
it "should be present in form submits" do
|
||||
@session.response_body = <<-EOS
|
||||
<form method="post" action="/form1">
|
||||
<input type="submit" />
|
||||
</form>
|
||||
EOS
|
||||
@session.should_receive(:post).with("/form1", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"})
|
||||
@session.clicks_button
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user