Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

make subsequent requests stick to the initial hostname/port and protocol

  • Loading branch information...
commit 1906eddf1e979af2c325520c254a571b93717663 1 parent be5426e
Sven Fuchs authored
29 lib/steam/request.rb
View
@@ -40,34 +40,45 @@ def env
super.merge(headers)
end
- def method=(method)
- @env['REQUEST_METHOD'] = method
+ def url=(url)
+ url = URI.parse(url)
+ self.path = url.path
+ self.scheme = url.scheme if url.scheme
+ self.host = url.host if url.host
+ self.port = url.port if url.port
+ self.query = url.query if url.query
end
+ alias :uri :url
+ alias :uri= :url=
- def uri=(uri)
- uri = URI.parse(uri)
- self.path = uri.path
- self.query = uri.query if uri.query
- self.scheme = uri.scheme if uri.scheme
- self.port = uri.port if uri.port
+ def method=(method)
+ @env.merge!(
+ 'REQUEST_METHOD' => method
+ )
end
- alias url= uri=
def scheme=(scheme)
@env.merge!(
'rack.url_scheme' => scheme,
'rack.test.scheme' => scheme
)
+ self.port = nil # reset the port so we switch according to the protocol
end
+ alias :protocol :scheme
+ alias :protocol= :scheme=
def host=(host)
+ host, self.port = host.split(':')
@env.merge!(
+ 'HTTP_HOST' => host,
'rack.test.host' => host
)
end
def port=(port)
+ port ||= protocol == 'https' ? 443 : 80
@env.merge!(
+ 'SERVER_PORT' => port,
'rack.test.port' => port
)
end
152 test/browser/html_unit/actions_test.rb
View
@@ -4,6 +4,8 @@
class HtmlUnitActionsTest < Test::Unit::TestCase
include Steam, HtmlFakes
+ attr_reader :browser
+
def setup
mock = Steam::Connection::Mock.new
static = Steam::Connection::Static.new(:root => FIXTURES_PATH)
@@ -12,172 +14,172 @@ def setup
test "click_on clicks on an element" do
perform :get, 'http://localhost:3000/', html
-
+
assert_response_contains('LINK') do
mock :get, 'http://localhost:3000/link', 'LINK'
- @browser.click_on('link')
+ browser.click_on('link')
end
end
- test "click_on clicks a button" do
+ test "click_on clicks a button" do
perform :get, 'http://localhost:3000/', html(:fields => :text)
-
+
assert_response_contains('FORM') do
mock :get, 'http://localhost:3000/form?field=', 'FORM'
- @browser.click_on(:button, 'button')
+ browser.click_on(:button, 'button')
end
end
-
- test "click_link clicks a link" do
+
+ test "click_link clicks a link" do
perform :get, 'http://localhost:3000/', html
-
+
assert_response_contains('LINK') do
mock :get, 'http://localhost:3000/link', 'LINK'
- @browser.click_link('link')
+ browser.click_link('link')
end
end
-
+
test "click_button clicks a button" do
perform :get, 'http://localhost:3000/', html(:fields => :text)
-
+
assert_response_contains('FORM') do
mock :get, 'http://localhost:3000/form?field=', 'FORM'
- @browser.click_button('button')
+ browser.click_button('button')
end
end
-
+
test "fill_in fills in a text input" do
perform :get, 'http://localhost:3000/', html(:fields => :text)
-
+
assert_response_contains('FIELD') do
mock :get, 'http://localhost:3000/form?field=text', 'FIELD'
- @browser.fill_in('field', :with => 'text')
- @browser.click_button('button')
+ browser.fill_in('field', :with => 'text')
+ browser.click_button('button')
end
end
-
+
test "fill_in fills in a textarea" do
perform :get, 'http://localhost:3000/', html(:fields => :textarea)
-
+
assert_response_contains('TEXTAREA') do
mock :get, 'http://localhost:3000/form?textarea=text', 'TEXTAREA'
- @browser.fill_in('textarea', :with => 'text')
- @browser.click_button('button')
+ browser.fill_in('textarea', :with => 'text')
+ browser.click_button('button')
end
end
-
+
test "check checks a checkbox" do
perform :get, 'http://localhost:3000/', html(:fields => :checkbox)
-
+
assert_response_contains('CHECKED') do
mock :get, 'http://localhost:3000/form?checkbox=1', 'CHECKED'
- @browser.check('checkbox')
- @browser.click_button('button')
+ browser.check('checkbox')
+ browser.click_button('button')
end
end
-
+
test "uncheck unchecks a checkbox" do
perform :get, 'http://localhost:3000/', html(:fields => :checkbox)
-
+
assert_response_contains('FORM') do
mock :get, 'http://localhost:3000/form', 'FORM'
- @browser.check('checkbox')
- @browser.uncheck('checkbox')
- @browser.click_button('button')
+ browser.check('checkbox')
+ browser.uncheck('checkbox')
+ browser.click_button('button')
end
end
-
+
test "choose activates a radio button" do
perform :get, 'http://localhost:3000/', html(:fields => :radio)
-
+
assert_response_contains('RADIO') do
mock :get, 'http://localhost:3000/form?radio=radio', 'RADIO'
- @browser.choose('radio')
- @browser.click_button('button')
+ browser.choose('radio')
+ browser.click_button('button')
end
end
-
+
test "select selects an option from a select box" do
perform :get, 'http://localhost:3000/', html(:fields => :select)
-
+
assert_response_contains('SELECT') do
mock :get, 'http://localhost:3000/form?select=foo', 'SELECT'
- @browser.select('foo', :from => 'select')
- @browser.click_button('button')
+ browser.select('foo', :from => 'select')
+ browser.click_button('button')
end
end
-
+
test "set_hidden_field sets a value to a hidden field" do
perform :get, 'http://localhost:3000/', html(:fields => :hidden)
-
+
assert_response_contains('SELECT') do
mock :get, 'http://localhost:3000/form?hidden=foo', 'SELECT'
- @browser.set_hidden_field('hidden', :to => 'foo')
- @browser.click_button('button')
+ browser.set_hidden_field('hidden', :to => 'foo')
+ browser.click_button('button')
end
end
-
+
test "attach_file sets a filename to a file field" do
perform :get, 'http://localhost:3000/', html(:fields => :file)
-
+
assert_response_contains('FILE') do
mock :get, 'http://localhost:3000/form?file=rails.png', 'FILE'
- @browser.attach_file('file', "#{TEST_ROOT}/fixtures/rails.png")
- @browser.click_button('button')
+ browser.attach_file('file', "#{TEST_ROOT}/fixtures/rails.png")
+ browser.click_button('button')
end
end
-
+
test "submit_form submits a form" do
perform :get, 'http://localhost:3000/', html(:fields => :text)
-
+
assert_response_contains('FORM') do
mock :get, 'http://localhost:3000/form?field=', 'FORM'
- @browser.submit_form('form')
+ browser.submit_form('form')
end
end
-
+
test "drag drags an draggable and drop drops the draggable onto a droppable" do
perform :get, 'http://localhost:3000/', html(:scripts => [:jquery, :jquery_ui, :drag])
-
- @browser.drag('link')
- @browser.drop('form')
- assert_equal 'DROPPED!', @browser.page.getTitleText
+
+ browser.drag('link')
+ browser.drop('form')
+ assert_equal 'DROPPED!', browser.page.getTitleText
end
-
+
test "drag_and_drop drags a draggable and drops it onto a droppable" do
perform :get, 'http://localhost:3000/', html(:scripts => [:jquery, :jquery_ui, :drag])
-
- @browser.drag_and_drop('link', :to => 'form')
- assert_equal 'DROPPED!', @browser.page.getTitleText
+
+ browser.drag_and_drop('link', :to => 'form')
+ assert_equal 'DROPPED!', browser.page.getTitleText
end
-
+
test "hover triggers a mouseOver event on an element" do
perform :get, 'http://localhost:3000/', html(:scripts => [:jquery, :jquery_ui, :hover])
-
- @browser.hover('paragraph')
- assert_equal 'HOVERED!', @browser.page.getTitleText
+
+ browser.hover('paragraph')
+ assert_equal 'HOVERED!', browser.page.getTitleText
end
-
+
test "focus triggers a focus event on an element" do
perform :get, 'http://localhost:3000/', html(:fields => :text, :scripts => [:jquery, :jquery_ui, :focus])
-
- @browser.focus('field')
- assert_equal 'FOCUSED!', @browser.page.getTitleText
+
+ browser.focus('field')
+ assert_equal 'FOCUSED!', browser.page.getTitleText
end
-
+
test "blur triggers a blur event on an element" do
perform :get, 'http://localhost:3000/', html(:fields => :text, :scripts => [:jquery, :jquery_ui, :blur])
-
- @browser.focus('field')
- @browser.blur('field')
- assert_equal 'BLURRED!', @browser.page.getTitleText
+
+ browser.focus('field')
+ browser.blur('field')
+ assert_equal 'BLURRED!', browser.page.getTitleText
end
-
+
test "double_click doubleclicks an element" do
perform :get, 'http://localhost:3000/', html(:scripts => [:jquery, :jquery_ui, :double_click])
-
- @browser.double_click('paragraph')
- assert_equal 'DOUBLE CLICKED!', @browser.page.getTitleText
+
+ browser.double_click('paragraph')
+ assert_equal 'DOUBLE CLICKED!', browser.page.getTitleText
end
-
+
end
14 test/browser/html_unit/javascript_test.rb
View
@@ -4,6 +4,8 @@
class HtmlUnitJavascriptTest < Test::Unit::TestCase
include Steam, HtmlFakes
+ attr_reader :browser
+
def setup
@connection = Steam::Connection::Mock.new
static = Steam::Connection::Static.new(:root => FIXTURES_PATH)
@@ -17,7 +19,7 @@ def setup
<script src="/javascripts/jquery.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
- $('div:not([id])').each(function() {
+ $('div:not([id])').each(function() {
document.title = $(this).html();
});
});
@@ -29,9 +31,9 @@ def setup
</body>
</html>
html
-
+
perform(:get, 'http://localhost:3000/', html)
- assert_equal 'bar', @browser.page.getTitleText
+ assert_equal 'bar', browser.page.getTitleText
end
test "jquery: div[id*=bar] selector" do
@@ -41,7 +43,7 @@ def setup
<script src="/javascripts/jquery.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
- $('div[id*=bar]').each(function() {
+ $('div[id*=bar]').each(function() {
document.title = $(this).html();
});
});
@@ -52,9 +54,9 @@ def setup
</body>
</html>
html
-
+
perform(:get, 'http://localhost:3000/', html)
- assert_equal 'foobar', @browser.page.getTitleText
+ assert_equal 'foobar', browser.page.getTitleText
end
end
116 test/browser/html_unit/rails_actions_test.rb
View
@@ -4,6 +4,8 @@
class HtmlUnitRailsActionsTest < Test::Unit::TestCase
include Steam, HtmlFakes
+ attr_reader :browser
+
def setup
connection = Steam::Connection::Mock.new
@browser = Steam::Browser::HtmlUnit.new(connection)
@@ -17,134 +19,134 @@ def test_select_date
perform :get, 'http://localhost:3000/', html(:fields => :date)
assert_response_contains('DATE') do
- @browser.select_date('7 November 2009', :from => 'event_date')
- @browser.submit_form('form')
+ browser.select_date('7 November 2009', :from => 'event_date')
+ browser.submit_form('form')
end
end
def test_select_date_works_with_date_object
perform :get, 'http://localhost:3000/', html(:fields => :date)
-
+
assert_response_contains('DATE') do
- @browser.select_date(Date.parse('7 November 2009'), :from => 'event_date')
- @browser.submit_form('form')
+ browser.select_date(Date.parse('7 November 2009'), :from => 'event_date')
+ browser.submit_form('form')
end
end
-
+
def test_select_date_works_with_label
perform :get, 'http://localhost:3000/', html(:fields => :date)
-
+
assert_response_contains('DATE') do
- @browser.select_date('7 November 2009', :from => 'Date')
- @browser.submit_form('form')
+ browser.select_date('7 November 2009', :from => 'Date')
+ browser.submit_form('form')
end
end
-
+
def test_select_date_works_without_from_option
perform :get, 'http://localhost:3000/', html(:fields => :date)
-
+
assert_response_contains('DATE') do
- @browser.select_date('7 November 2009')
- @browser.submit_form('form')
+ browser.select_date('7 November 2009')
+ browser.submit_form('form')
end
end
-
+
def test_select_date_works_with_id_prefix_option
perform :get, 'http://localhost:3000/', html(:fields => :date)
-
+
assert_response_contains('DATE') do
- @browser.select_date('7 November 2009', :id_prefix => 'event_date')
- @browser.submit_form('form')
+ browser.select_date('7 November 2009', :id_prefix => 'event_date')
+ browser.submit_form('form')
end
end
-
+
def test_select_datetime
perform :get, 'http://localhost:3000/', html(:fields => :datetime)
-
+
assert_response_contains('DATETIME') do
- @browser.select_datetime('7 November 2009, 19:00', :from => 'event_datetime')
- @browser.submit_form('form')
+ browser.select_datetime('7 November 2009, 19:00', :from => 'event_datetime')
+ browser.submit_form('form')
end
end
-
+
def test_select_datetime_works_with_datetime_object
perform :get, 'http://localhost:3000/', html(:fields => :datetime)
-
+
assert_response_contains('DATETIME') do
- @browser.select_datetime(Time.parse('7 November 2009, 19:00'), :from => 'event_datetime')
- @browser.submit_form('form')
+ browser.select_datetime(Time.parse('7 November 2009, 19:00'), :from => 'event_datetime')
+ browser.submit_form('form')
end
end
-
+
def test_select_datetime_works_with_label
perform :get, 'http://localhost:3000/', html(:fields => :datetime)
-
+
assert_response_contains('DATETIME') do
- @browser.select_datetime('7 November 2009, 19:00', :from => 'Datetime')
- @browser.submit_form('form')
+ browser.select_datetime('7 November 2009, 19:00', :from => 'Datetime')
+ browser.submit_form('form')
end
end
-
+
def test_select_datetime_works_without_from_option
perform :get, 'http://localhost:3000/', html(:fields => :datetime)
-
+
assert_response_contains('DATETIME') do
- @browser.select_datetime('7 November 2009, 19:00')
- @browser.submit_form('form')
+ browser.select_datetime('7 November 2009, 19:00')
+ browser.submit_form('form')
end
end
-
+
def test_select_datetime_works_with_id_prefix_option
perform :get, 'http://localhost:3000/', html(:fields => :datetime)
-
+
assert_response_contains('DATETIME') do
- @browser.select_datetime('7 November 2009, 19:00', :id_prefix => 'event_datetime')
- @browser.submit_form('form')
+ browser.select_datetime('7 November 2009, 19:00', :id_prefix => 'event_datetime')
+ browser.submit_form('form')
end
end
-
+
def test_select_time
perform :get, 'http://localhost:3000/', html(:fields => :time)
-
+
assert_response_contains('TIME') do
- @browser.select_time('19:00', :from => 'event_time')
- @browser.submit_form('form')
+ browser.select_time('19:00', :from => 'event_time')
+ browser.submit_form('form')
end
end
-
+
def test_select_time_works_with_time_object
perform :get, 'http://localhost:3000/', html(:fields => :time)
-
+
assert_response_contains('TIME') do
- @browser.select_time(Time.parse('19:00'), :from => 'event_time')
- @browser.submit_form('form')
+ browser.select_time(Time.parse('19:00'), :from => 'event_time')
+ browser.submit_form('form')
end
end
-
+
def test_select_time_works_with_label
perform :get, 'http://localhost:3000/', html(:fields => :time)
-
+
assert_response_contains('TIME') do
- @browser.select_time('19:00', :from => 'Time')
- @browser.submit_form('form')
+ browser.select_time('19:00', :from => 'Time')
+ browser.submit_form('form')
end
end
-
+
def test_select_time_works_without_from_option
perform :get, 'http://localhost:3000/', html(:fields => :time)
-
+
assert_response_contains('TIME') do
- @browser.select_time('19:00')
- @browser.submit_form('form')
+ browser.select_time('19:00')
+ browser.submit_form('form')
end
end
-
+
def test_select_time_works_with_id_prefix_option
perform :get, 'http://localhost:3000/', html(:fields => :time)
-
+
assert_response_contains('TIME') do
- @browser.select_time('19:00', :id_prefix => 'event_time')
- @browser.submit_form('form')
+ browser.select_time('19:00', :id_prefix => 'event_time')
+ browser.submit_form('form')
end
end
end
81 test/browser/html_unit_test.rb
View
@@ -2,51 +2,76 @@
require 'fixtures/html_fakes'
require 'locator'
-module HtmlUnitTests
+class HtmlUnitTest < Test::Unit::TestCase
include Steam
- def init
+ attr_reader :browser
+
+ def setup
@connection = Connection::Mock.new
@browser = Browser::HtmlUnit.new(@connection)
- perform(:get, 'http://localhost:3000/', '<div id="foo"><div id="bar"><a id="buz" href="">bar!</a></div></div>')
+ perform(:get, 'http://test.host/', '<div id="foo"><div id="bar"><a id="buz" href="">bar!</a></div></div>')
end
def locate(*args, &block)
- @browser.locate(*args, &block)
+ browser.locate(*args, &block)
end
def within(*args, &block)
- @browser.within(*args, &block)
+ browser.within(*args, &block)
+ end
+
+ test 'subsequent requests stick to the initial hostname' do
+ mock(:get, 'http://www.example.com/host', 'www.example.com')
+ mock(:get, 'http://example.com/host', 'example.com')
+
+ browser.request.port = nil
+ browser.request.host = 'www.example.com'
+ assert_match /www.example.com/, browser.get('/host').last.body
+ browser.request.host = 'example.com'
+ assert_match /example.com/, browser.get('/host').last.body
+ end
+
+ test 'subsequent requests stick to the initial protocol' do
+ mock(:get, 'https://test.host/protocol', 'https')
+ mock(:get, 'http://test.host/protocol', 'http')
+
+ browser.request.protocol = 'https'
+ assert_match /https/, browser.get('/protocol').last.body
+ browser.request.protocol = 'http'
+ assert_match /http/, browser.get('/protocol').last.body
end
test 'using an alert handler' do
- @browser.set_handler(:alert) { |page, message| @alerted = message == 'FOO!' }
- @browser.execute('alert("FOO!");')
+ browser.set_handler(:alert) { |page, message| @alerted = message == 'FOO!' }
+ browser.execute('alert("FOO!");')
assert @alerted
end
test 'using an confirm handler' do
- @browser.set_handler(:alert) { |page, message| @alerted = true }
- @browser.set_handler(:confirm) { |page, message| message == 'bar?' }
+ browser.set_handler(:alert) { |page, message| @alerted = true }
+ browser.set_handler(:confirm) { |page, message| message == 'bar?' }
- @browser.execute('if(confirm("foo?")) alert("");')
+ browser.execute('if(confirm("foo?")) alert("");')
assert !@alerted
- @browser.execute('if(confirm("bar?")) alert("");')
+ browser.execute('if(confirm("bar?")) alert("");')
assert @alerted
end
test 'adding a cookie' do
- @browser.add_cookie('foo', 'bar')
- assert_equal 'bar', @browser.get_cookie('foo')
+ browser.add_cookie('foo', 'bar')
+ assert_equal 'bar', browser.get_cookie('foo')
end
test 'clearing cookies' do
- @browser.add_cookie('foo', 'bar')
- @browser.clear_cookies
- assert_equal nil, @browser.get_cookie('foo')
+ browser.add_cookie('foo', 'bar')
+ browser.clear_cookies
+ assert_equal nil, browser.get_cookie('foo')
end
+ # LOCATE
+
test 'locate with node type' do
element = locate(:a)
assert_equal 'a', element.name
@@ -72,6 +97,8 @@ def within(*args, &block)
assert_equal 'a', element.name
end
+ # WITHIN
+
test 'within with node type' do
element = within(:div) { within(:div) { locate(:a) } }
assert_equal 'a', element.name
@@ -102,25 +129,3 @@ def within(*args, &block)
assert_equal 'a', element.name
end
end
-
-class HtmlUnitWithNokogiriAdapterTest < Test::Unit::TestCase
- include Steam, HtmlUnitTests
-
- def setup
- init
- end
-end
-
-# class HtmlUnitWithHtmlUnitAdapterTest < Test::Unit::TestCase
-# include Steam, HtmlUnitTests
-#
-# def setup
-# @old_adapter, Locator::Dom.adapter = Locator::Dom.adapter, Locator::Dom::Htmlunit
-# init
-# end
-#
-# def teardown
-# Locator::Dom.adapter = @old_adapter
-# end
-# end
-
1  test/test_helper.rb
View
@@ -52,6 +52,7 @@ def mock(method, url, response)
def perform(method, url, response)
mock(method, url, response)
@status, @headers, @response = @browser.send(method, url)
+ [@status, @headers, @response]
end
def assert_response_contains(text, options = {})
Please sign in to comment.
Something went wrong with that request. Please try again.