Created
December 5, 2014 23:50
-
-
Save calebwoods/ab7c0cce8c52f58ea033 to your computer and use it in GitHub Desktop.
Used to access external services in Rails tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create in config/initializers | |
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 | |
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