Permalink
Browse files

can't really get drb support working

  • Loading branch information...
1 parent 3045516 commit c14ca5fea08c8bda0a779a8a24b3e867935a0263 Sven Fuchs committed Feb 21, 2010
View
5 TODO
@@ -2,4 +2,7 @@ http://htmlunit.sourceforge.net/logging.html
http://htmlunit.sourceforge.net/faq.html#MemoryLeak
HtmlUnit appears to be leaking memory; what's the deal?
-Make sure (a) that you are using the latest version of HtmlUnit, and (b) that you are calling WebClient.closeAllWindows() when you are finished with your WebClient instance.
+Make sure (a) that you are using the latest version of HtmlUnit, and (b) that you are calling WebClient.closeAllWindows() when you are finished with your WebClient instance.
+
+
+http://github.com/sklemm/jrequire/blob/master/lib/java.rb
View
@@ -10,9 +10,10 @@ module Browser
class << self
def create(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
+ type = args.shift if args.first.is_a?(Symbol)
connection = args.pop
- type = args.pop || :html_unit
+ type ||= :html_unit
type = const_get(type.to_s.camelize)
type = type.const_get('Drb') if options[:daemon]
@@ -40,9 +40,17 @@ def initialize(connection)
def getResponse(request)
# FIXME preserve original scheme, host + port
- url = request.getUrl.toString.dup
method = request.getHttpMethod.toString.dup
- env = Request.env_for(url, :method => method)
+ url = request.getUrl.toString.dup
+
+ body = request.getRequestBody
+ body = body.toString.dup if body
+
+ params = request.getRequestParameters
+ params = Hash[*params.toArray.map { |e| [e.name, e.value] }.flatten]
+
+ input = body ? body : Rack::Utils.build_nested_query(params)
+ env = Request.env_for(url, :method => method, :input => input)
status, headers, response = connection.call(env)
response.body.close if response.body.respond_to?(:close)
View
@@ -1,7 +1,9 @@
module Steam
module Connection
- autoload :Mock, 'steam/connection/mock'
- autoload :Rails, 'steam/connection/rails'
- autoload :Static, 'steam/connection/static'
+ autoload :Mock, 'steam/connection/mock'
+ autoload :NetHttp, 'steam/connection/net_http'
+ autoload :OpenUri, 'steam/connection/open_uri'
+ autoload :Rails, 'steam/connection/rails'
+ autoload :Static, 'steam/connection/static'
end
end
@@ -20,21 +20,26 @@ def initialize
def call(env)
request = Rack::Request.new(env)
- response(request.request_method, request.url).to_a
+ response = response(request.request_method, request.url)
+ response = response.call if response.is_a?(Proc)
+ response.to_a
end
def response(method, url)
responses(method)[CGI::unescape(url)] || Rack::Response.new('', 404)
end
- def mock(method, url, response, headers = {})
- responses(method)[url] = case response
- when Rack::Response
- response
+ def mock(*args)
+ headers = args.last.is_a?(Hash) ? args.pop : {}
+ response, url, method = *args.reverse
+
+ responses(method || :get)[url] = case response
when Array
Rack::Response.new(*response)
when String
Rack::Response.new(response, 200, headers)
+ else
+ response
end
end
@@ -0,0 +1,42 @@
+# This connection proxies requests using open-uri.
+#
+
+require 'net/http'
+require 'uri'
+
+Net::HTTPResponse.class_eval do
+ attr_reader :code # crap
+end
+
+module Steam
+ module Connection
+ class NetHttp
+ def call(env)
+ request = Rack::Request.new(env)
+ response = send(request.request_method.downcase, request)
+ response = follow_redirect(response) if response.status.to_s[0, 1] == '3'
+ response.to_a
+ end
+
+ def get(request)
+ url = URI.parse(request.url)
+ response = Net::HTTP.start(url.host, url.port) { |http| http.get(url.path) }
+ Rack::Response.new(response.body, response.code, response.to_hash)
+ end
+
+ def post(request)
+ url = URI.parse(request.url)
+ params = Rack::Utils.parse_query(Rack::Utils.build_nested_query(request.params))
+ response = Net::HTTP.post_form(url, params)
+ Rack::Response.new(response.body, response.code, response.to_hash)
+ end
+
+ def follow_redirect(response)
+ location = response.headers['location'].join
+ env = Steam::Request.env_for(location)
+ request = Rack::Request.new(env)
+ get(request)
+ end
+ end
+ end
+end
@@ -0,0 +1,24 @@
+# This connection proxies requests using open-uri.
+#
+
+require 'open-uri'
+
+module Steam
+ module Connection
+ class OpenUri
+ def call(env)
+ request = Rack::Request.new(env)
+ Rack::Response.new(*open(request.url)).to_a
+ end
+
+ def open(uri)
+ headers = {}
+ body = Kernel.open(uri) { |f| headers = f.meta; f.read }
+ [body, 200, headers]
+ rescue OpenURI::HTTPError => e
+ status = e.message.split(' ').first
+ ['', status, headers]
+ end
+ end
+ end
+end
View
@@ -0,0 +1,3 @@
+use Rack::Lock
+use Rack::Static, :root => File.expand_path("../fixtures/public", __FILE__), :urls => ['/']
+run lambda { |*nothing| }
@@ -3,6 +3,7 @@
<title></title>
<script src="/jquery-1.4.1.js" type="text/javascript"></script>
<script src="/jquery-ui-1.8rc1.js" type="text/javascript"></script>
+ <script src="/title.js" type="text/javascript"></script>
<script>
(function($) {
@@ -0,0 +1,8 @@
+<html>
+ <head>
+ <title></title>
+ <script src="/jquery-1.4.1.js" type="text/javascript"></script>
+ <script src="/jquery-ui-1.8rc1.js" type="text/javascript"></script>
+ <script src="/title.js" type="text/javascript"></script>
+ </head>
+</html>
@@ -0,0 +1 @@
+document.title = 'FOO'
View
@@ -1,15 +1,55 @@
$: << File.expand_path("../../../lib", __FILE__)
require 'steam'
+require 'drb'
include Steam
-root = File.expand_path("../../fixtures/public", __FILE__)
-connection = Connection::Static.new(:root => root, :urls => %w(/ /javascripts /stylesheets))
+# # using a static connection works /w drb
+# root = File.expand_path("../../fixtures/public", __FILE__)
+# connection = Connection::Static.new(:root => root, :urls => ['/'])
+#
+# # using a mocked connection works /w drb
+# connection = Connection::Mock.new
+# connection.mock 'http://localhost:3000/short.html', <<-html
+# <html>
+# <head>
+# <script src="/title.js" type="text/javascript"></script>
+# <script src="/jquery-ui-1.8rc1.js" type="text/javascript"></script>
+# </head>
+# </html>
+# html
+# connection.mock 'http://localhost:3000/title.js', "document.title = 'FOO'"
+# connection.mock 'http://localhost:3000/jquery-ui-1.8rc1.js', File.read('test/fixtures/public/jquery-ui-1.8rc1.js')
+# # Using the native default connection, i.e. connecting over the network, does
+# # not work /w drb when the first response has to retrieve additional assets,
+# # like a js file. (There's a config.ru file in test/, so one can rackup the
+# # files in test/fixtures/public.)
+connection = nil
-browser = Steam::Browser.create(:html_unit, connection, :daemon => true)
-browser.request('/index.html')
-puts browser.response.status
+# Interestingly, this works in all situations, except that it does not implement
+# a full REST interface and I don't want to implement a cookie jar and everything
+# connection = Connection::OpenUri.new
-# @browser = Browser::HtmlUnit.new(connection)
+
+# url = 'http://localhost:3000/short.html'
+url = 'http://localhost:3000'
+
+def request_without_drb(browser, url)
+ browser.request(url)
+ browser.response
+end
+
+def request_with_drb(browser, url)
+ DRb.start_service(Steam.config[:drb_uri], browser)
+ drb = DRbObject.new(nil, Steam.config[:drb_uri])
+ drb.request(url)
+ drb.response
+end
+
+browser = Steam::Browser.create(:html_unit, connection)
+response = request_without_drb(browser, url)
+# response = request_with_drb(browser, url)
+
+puts response.body

0 comments on commit c14ca5f

Please sign in to comment.