mirror of
https://github.com/jnunemaker/httparty
synced 2023-03-27 23:23:07 -04:00
Remove meta-programming around mongrel handlers
This commit is contained in:
parent
ddbc2d70b4
commit
472f2d8d0a
2 changed files with 47 additions and 61 deletions
|
@ -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)
|
||||
|
|
|
@ -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|
|
||||
|
|
Loading…
Reference in a new issue