Skip to content

Commit

Permalink
Remove meta-programming around mongrel handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
sandro committed Jun 12, 2010
1 parent ddbc2d7 commit 472f2d8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 59 deletions.
96 changes: 41 additions & 55 deletions features/steps/mongrel_helper.rb
Original file line number Diff line number Diff line change
@@ -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

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
module BasicAuthentication
def self.extended(base)
base.custom_headers["WWW-Authenticate"] = 'Basic Realm="Super Secret Page"'
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
def process(request, response)
if authorized?(request)
super
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
end
def authorized?(request)
request.params["HTTP_AUTHORIZATION"] == "Basic " + Base64.encode64("#{@username}:#{@password}").strip
end
handler.extend(m)
end

def add_digest_authentication_to(handler)
m = Module.new do
attr_writer :username, :password

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
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 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
def process(request, response)
if authorized?(request)
super
else
reply_with(response, 401, "Incorrect. You have 20 seconds to comply.")
end
end

def authorized?(request)
request.params["HTTP_AUTHORIZATION"] =~ /Digest.*uri=/
end
def authorized?(request)
request.params["HTTP_AUTHORIZATION"] =~ /Digest.*uri=/
end
handler.extend(m)
end

def new_mongrel_redirector(target_url, relative_path = false)
Expand Down
8 changes: 4 additions & 4 deletions features/steps/remote_service_steps.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -30,11 +30,11 @@
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|
Expand Down

0 comments on commit 472f2d8

Please sign in to comment.