From 472f2d8d0a5227ab59f21fc1372f09430dbbb0bd Mon Sep 17 00:00:00 2001 From: Sandro Turriate Date: Tue, 8 Jun 2010 17:16:09 -0400 Subject: [PATCH] Remove meta-programming around mongrel handlers --- features/steps/mongrel_helper.rb | 100 +++++++++++-------------- features/steps/remote_service_steps.rb | 8 +- 2 files changed, 47 insertions(+), 61 deletions(-) diff --git a/features/steps/mongrel_helper.rb b/features/steps/mongrel_helper.rb index 24d98d6..31a76e6 100644 --- a/features/steps/mongrel_helper.rb +++ b/features/steps/mongrel_helper.rb @@ -1,75 +1,61 @@ -def basic_mongrel_handler - Class.new(Mongrel::HttpHandler) do - attr_writer :content_type, :response_body, :response_code, :preprocessor +class BasicMongrelHandler < Mongrel::HttpHandler + attr_accessor :content_type, :custom_headers, :response_body, :response_code, :preprocessor, :username, :password - def initialize - @content_type = "text/html" - @response_body = "" - @response_code = 200 - @custom_headers = {} - end + def initialize + @content_type = "text/html" + @response_body = "" + @response_code = 200 + @custom_headers = {} + end - def process(request, response) - instance_eval &@preprocessor if @preprocessor - reply_with(response, @response_code, @response_body) - end + def process(request, response) + instance_eval &preprocessor if preprocessor + reply_with(response, response_code, response_body) + end - def reply_with(response, code, response_body) - response.start(code) do |head, body| - head["Content-Type"] = @content_type - @custom_headers.each { |k,v| head[k] = v } - body.write(response_body) - end + def reply_with(response, code, response_body) + response.start(code) do |head, body| + head["Content-Type"] = content_type + custom_headers.each { |k,v| head[k] = v } + body.write(response_body) end end end -def new_mongrel_handler - basic_mongrel_handler.new -end +module BasicAuthentication + def self.extended(base) + base.custom_headers["WWW-Authenticate"] = 'Basic Realm="Super Secret Page"' + end -def add_basic_authentication_to(handler) - m = Module.new do - attr_writer :username, :password - - def self.extended(base) - base.instance_eval { @custom_headers["WWW-Authenticate"] = 'Basic Realm="Super Secret Page"' } - base.class_eval { alias_method_chain :process, :basic_authentication } - end - - def process_with_basic_authentication(request, response) - if authorized?(request) then process_without_basic_authentication(request, response) - else reply_with(response, 401, "Incorrect. You have 20 seconds to comply.") - end - end - - def authorized?(request) - request.params["HTTP_AUTHORIZATION"] == "Basic " + Base64.encode64("#{@username}:#{@password}").strip + def process(request, response) + if authorized?(request) + super + else + reply_with(response, 401, "Incorrect. You have 20 seconds to comply.") end end - handler.extend(m) + + def authorized?(request) + request.params["HTTP_AUTHORIZATION"] == "Basic " + Base64.encode64("#{@username}:#{@password}").strip + end end -def add_digest_authentication_to(handler) - m = Module.new do - attr_writer :username, :password +module DigestAuthentication + def self.extended(base) + base.custom_headers["WWW-Authenticate"] = 'Digest realm="testrealm@host.com",qop="auth,auth-int",nonce="nonce",opaque="opaque"' + end - def self.extended(base) - base.instance_eval { @custom_headers["WWW-Authenticate"] = 'Digest realm="testrealm@host.com",qop="auth,auth-int",nonce="nonce",opaque="opaque"' } - base.class_eval { alias_method_chain :process, :digest_authentication } - end - - def process_with_digest_authentication(request, response) - if authorized?(request) then process_without_digest_authentication(request, response) - else reply_with(response, 401, "Incorrect. You have 20 seconds to comply.") - end - end - - def authorized?(request) - request.params["HTTP_AUTHORIZATION"] =~ /Digest.*uri=/ + def process(request, response) + if authorized?(request) + super + else + reply_with(response, 401, "Incorrect. You have 20 seconds to comply.") end end - handler.extend(m) + + def authorized?(request) + request.params["HTTP_AUTHORIZATION"] =~ /Digest.*uri=/ + end end def new_mongrel_redirector(target_url, relative_path = false) diff --git a/features/steps/remote_service_steps.rb b/features/steps/remote_service_steps.rb index 999616c..d20d3dd 100644 --- a/features/steps/remote_service_steps.rb +++ b/features/steps/remote_service_steps.rb @@ -1,10 +1,10 @@ Given /a remote service that returns '(.*)'/ do |response_body| - @handler = new_mongrel_handler + @handler = BasicMongrelHandler.new Given "the response from the service has a body of '#{response_body}'" end Given /a remote service that returns a (\d+) status code/ do |code| - @handler = new_mongrel_handler + @handler = BasicMongrelHandler.new @handler.response_code = code end @@ -30,11 +30,11 @@ Given /the url '(.*)' redirects to '(.*)'/ do |redirection_url, target_url| end Given /that service is protected by Basic Authentication/ do - add_basic_authentication_to @handler + @handler.extend BasicAuthentication end Given /that service is protected by Digest Authentication/ do - add_digest_authentication_to @handler + @handler.extend DigestAuthentication end Given /that service requires the username '(.*)' with the password '(.*)'/ do |username, password|