Skip to content

Instantly share code, notes, and snippets.

@garybernhardt
Created August 17, 2011 18:33
Show Gist options
  • Save garybernhardt/1152263 to your computer and use it in GitHub Desktop.
Save garybernhardt/1152263 to your computer and use it in GitHub Desktop.
module Rack
module Utils
def parse_nested_query(qs, d = nil)
params = ActiveSupport::OrderedHash.new
(qs || '').split(d ? /[#{d}] */n : DEFAULT_SEP).each do |p|
k, v = unescape(p).split('=', 2)
normalize_params(params, k, v)
end
return params
end
end
end
module Rack
module Test
class Session
alias_method :old_env_for, :env_for
def env_for(path, env)
if env[:params] && env[:params].empty?
env[:params] = ActiveSupport::OrderedHash.new
end
old_env_for(path, env)
end
end
end
end
class BraintreeTestApp
def initialize(app)
@app = app
end
def call(env)
@env = env
config = Braintree::Configuration.instantiate
if request.path =~ /\/merchants\/#{config.merchant_id}\/transparent_redirect_requests$/
#proxy post to braintree
uri = URI.parse(config.protocol + "://" + config.server + ":" +
config.port.to_s + request.path)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
res = http.post(uri.path, request.body.read)
if res.code == "303"
header_hash = res.header.to_hash
header_hash["location"].first.gsub!("http://localhost:3000/", "http://www.example.com/")
[303, {"location" => header_hash["location"].first}, ""]
else
raise "unexpected response from Braintree: expected a 303"
end
else
@app.call(env)
end
end
def request
@request = Rack::Request.new(@env)
end
end
if Rails.env.test?
::Rails.application.config.middleware.use BraintreeTestApp
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment