Skip to content

Commit

Permalink
feat: monkeypatch webmachine to make the rack env available on the re…
Browse files Browse the repository at this point in the history
…quest
  • Loading branch information
bethesque committed Apr 24, 2019
1 parent 672f0ee commit 40be97d
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 109 deletions.
1 change: 1 addition & 0 deletions lib/pact_broker/api.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'webmachine/adapters/rack_mapped'
require 'webmachine/rack_adapter_monkey_patch'
require 'pact_broker/api/resources'
require 'pact_broker/feature_toggle'

Expand Down
3 changes: 1 addition & 2 deletions lib/pact_broker/api/resources/error_handler.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'webmachine/convert_request_to_rack_env'
require 'pact_broker/configuration'
require 'securerandom'

Expand Down Expand Up @@ -49,7 +48,7 @@ def self.response_body_hash e, error_reference
def self.report e, error_reference, request
PactBroker.configuration.api_error_reporters.each do | error_notifier |
begin
error_notifier.call(e, env: Webmachine::ConvertRequestToRackEnv.call(request), error_reference: error_reference)
error_notifier.call(e, env: request.env, error_reference: error_reference)
rescue StandardError => e
log_error(e, "Error executing api_error_reporter")
end
Expand Down
37 changes: 0 additions & 37 deletions lib/webmachine/convert_request_to_rack_env.rb

This file was deleted.

39 changes: 39 additions & 0 deletions lib/webmachine/rack_adapter_monkey_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'webmachine/request'
require 'webmachine/adapters/rack'

# Monkey patch to make the Rack env available on the Webmachine Request object

module Webmachine
class RackRequest < Webmachine::Request
attr_reader :env

def initialize(method, uri, headers, body, routing_tokens, base_uri, env)
super(method, uri, headers, body, routing_tokens, base_uri)
@env = env
end
end
end


unless Webmachine::Adapters::Rack.private_instance_methods.include?(:build_webmachine_request)
raise "Webmachine::Adapters::Rack no longer has the private instance method #build_webmachine_request - rack env monkey patch won't work"
end

module Webmachine
module Adapters
class Rack < Adapter
private

def build_webmachine_request(rack_req, headers)
Webmachine::RackRequest.new(rack_req.request_method,
rack_req.url,
headers,
RequestBody.new(rack_req),
routing_tokens(rack_req),
base_uri(rack_req),
rack_req.env
)
end
end
end
end
3 changes: 1 addition & 2 deletions spec/lib/pact_broker/api/resources/error_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ module Resources
let(:error) { StandardError.new('test error') }
let(:thing) { double('thing', call: nil, another_call: nil) }
let(:options) { { env: env, error_reference: "bYWfnyWPlf" } }
let(:request) { double('request' ) }
let(:request) { double('request', env: env ) }
let(:response) { double('response', :body= => nil) }
let(:env) { double('env') }

subject { ErrorHandler.call(error, request, response) }

before do
allow(Webmachine::ConvertRequestToRackEnv).to receive(:call).and_return(env)
PactBroker.configuration.add_api_error_reporter do | error, options |
thing.call(error, options)
end
Expand Down
68 changes: 0 additions & 68 deletions spec/lib/webmachine/convert_request_to_rack_env_spec.rb

This file was deleted.

50 changes: 50 additions & 0 deletions spec/lib/webmachine/rack_adapter_monkey_patch_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'webmachine'
require 'webmachine/adapters/rack_mapped'
require 'webmachine/rack_adapter_monkey_patch'
require 'rack/test'

module Webmachine
module Adapters
class TestResource < Webmachine::Resource
def allowed_methods
["POST"]
end

def process_post
response.body = request.env['FOO']
true
end
end

describe Rack do
include ::Rack::Test::Methods

let(:app) do
pact_api = Webmachine::Application.new do |app|
app.routes do
add(['test'], TestResource)
end
end

pact_api.configure do |config|
config.adapter = :RackMapped
end

pact_api.adapter
end

let(:rack_env) do
{
'FOO' => 'foo'
}
end

subject { post("/test", nil, rack_env) }

it "passes the rack env through on the request" do
expect(subject.status).to eq 200
expect(subject.body).to eq 'foo'
end
end
end
end

0 comments on commit 40be97d

Please sign in to comment.