Skip to content

Moved RackMapped class into its own file. #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions lib/webmachine/adapters/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,41 +186,5 @@ def each
end
end # class RequestBody
end # class Rack

# Provides the same functionality as the parent Webmachine::Adapters::Rack
# adapter, but allows the Webmachine application to be hosted at an
# arbitrary path in a parent Rack application (as in Rack `map` or Rails
# routing `mount`)
#
# This functionality is separated out from the parent class to preserve
# backward compatibility in the behaviour of the parent Rack adpater.
#
# To use the adapter in a parent Rack application, map the Webmachine
# application as follows in a rackup file or Rack::Builder:
#
# map '/foo' do
# run SomeotherRackApp
#
# map '/bar' do
# run MyWebmachineApp.adapter
# end
# end
class RackMapped < Rack
protected
def routing_tokens(rack_req)
routing_match = rack_req.path_info.match(Webmachine::Request::ROUTING_PATH_MATCH)
routing_path = routing_match ? routing_match[1] : ""
routing_path.split(SLASH)
end

def base_uri(rack_req)
# rack SCRIPT_NAME env var doesn't end with "/". This causes weird
# behavour when URI.join concatenates URI components in
# Webmachine::Decision::Flow#n11
script_name = rack_req.script_name + SLASH
URI.join(rack_req.base_url, script_name)
end
end

end # module Adapters
end # module Webmachine
42 changes: 42 additions & 0 deletions lib/webmachine/adapters/rack_mapped.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'webmachine/adapters/rack'

module Webmachine
module Adapters
# Provides the same functionality as the parent Webmachine::Adapters::Rack
# adapter, but allows the Webmachine application to be hosted at an
# arbitrary path in a parent Rack application (as in Rack `map` or Rails
# routing `mount`)
#
# This functionality is separated out from the parent class to preserve
# backward compatibility in the behaviour of the parent Rack adpater.
#
# To use the adapter in a parent Rack application, map the Webmachine
# application as follows in a rackup file or Rack::Builder:
#
# map '/foo' do
# run SomeotherRackApp
#
# map '/bar' do
# run MyWebmachineApp.adapter
# end
# end
class RackMapped < Rack

protected

def routing_tokens(rack_req)
routing_match = rack_req.path_info.match(Webmachine::Request::ROUTING_PATH_MATCH)
routing_path = routing_match ? routing_match[1] : ""
routing_path.split(SLASH)
end

def base_uri(rack_req)
# rack SCRIPT_NAME env var doesn't end with "/". This causes weird
# behavour when URI.join concatenates URI components in
# Webmachine::Decision::Flow#n11
script_name = rack_req.script_name + SLASH
URI.join(rack_req.base_url, script_name)
end
end # class RackMapped
end # module Adapters
end # module Webmachine
71 changes: 71 additions & 0 deletions spec/webmachine/adapters/rack_mapped_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require 'webmachine/adapter'
require 'webmachine/adapters/rack_mapped'
require 'spec_helper'
require 'webmachine/spec/adapter_lint'
require 'rack/test'

describe Webmachine::Adapters::RackMapped do
it_should_behave_like :adapter_lint do
it "should set Server header" do
response = client.request(Net::HTTP::Get.new("/test"))
expect(response["Server"]).to match(/Webmachine/)
expect(response["Server"]).to match(/Rack/)
end
end
end

describe Webmachine::Adapters::RackMapped do
class CreateResource < Webmachine::Resource
def allowed_methods
["POST"]
end

def content_types_accepted
[["application/json", :from_json]]
end

def content_types_provided
[["application/json", :to_json]]
end

def post_is_create?
true
end

def create_path
"created_path_here/123"
end

def from_json
response.body = %{ {"foo": "bar"} }
end
end

let(:app) do
Rack::Builder.new do
map '/some/route' do
run(Webmachine::Application.new do |app|
app.add_route(["test"], Test::Resource)
app.add_route(["create_test"], CreateResource)
app.configure do | config |
config.adapter = :RackMapped
end
end.adapter)
end
end
end

context "using Rack::Test" do
include Rack::Test::Methods

it "provides the full request URI" do
rack_response = get "some/route/test", nil, {"HTTP_ACCEPT" => "test/response.request_uri"}
expect(rack_response.body).to eq "http://example.org/some/route/test"
end

it "provides LOCATION header using custom base_uri when creating from POST request" do
rack_response = post "/some/route/create_test", %{{"foo": "bar"}}, {"HTTP_ACCEPT" => "application/json", "CONTENT_TYPE" => "application/json"}
expect(rack_response.headers["Location"]).to eq("http://example.org/some/route/created_path_here/123")
end
end
end
66 changes: 0 additions & 66 deletions spec/webmachine/adapters/rack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@
end
end

describe Webmachine::Adapters::RackMapped do
it_should_behave_like :adapter_lint do
it "should set Server header" do
response = client.request(Net::HTTP::Get.new("/test"))
expect(response["Server"]).to match(/Webmachine/)
expect(response["Server"]).to match(/Rack/)
end
end
end

describe Webmachine::Adapters::Rack::RackResponse do
context "on Rack < 1.5 release" do
before { allow(Rack).to receive_messages(:release => "1.4") }
Expand Down Expand Up @@ -65,59 +55,3 @@
end
end
end

describe Webmachine::Adapters::RackMapped do
class CreateResource < Webmachine::Resource
def allowed_methods
["POST"]
end

def content_types_accepted
[["application/json", :from_json]]
end

def content_types_provided
[["application/json", :to_json]]
end

def post_is_create?
true
end

def create_path
"created_path_here/123"
end

def from_json
response.body = %{ {"foo": "bar"} }
end
end

let(:app) do
Rack::Builder.new do
map '/some/route' do
run(Webmachine::Application.new do |app|
app.add_route(["test"], Test::Resource)
app.add_route(["create_test"], CreateResource)
app.configure do | config |
config.adapter = :RackMapped
end
end.adapter)
end
end
end

context "using Rack::Test" do
include Rack::Test::Methods

it "provides the full request URI" do
rack_response = get "some/route/test", nil, {"HTTP_ACCEPT" => "test/response.request_uri"}
expect(rack_response.body).to eq "http://example.org/some/route/test"
end

it "provides LOCATION header using custom base_uri when creating from POST request" do
rack_response = post "/some/route/create_test", %{{"foo": "bar"}}, {"HTTP_ACCEPT" => "application/json", "CONTENT_TYPE" => "application/json"}
expect(rack_response.headers["Location"]).to eq("http://example.org/some/route/created_path_here/123")
end
end
end