-
-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: monkeypatch webmachine to make the rack env available on the re…
…quest
- Loading branch information
Showing
7 changed files
with
92 additions
and
109 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
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 |
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
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 |