Skip to content

Commit 10614f8

Browse files
committed
Merge pull request #225 from bethesque/new-file-for-rack-mapped
Moved RackMapped class into its own file.
2 parents c17039f + 940a0b0 commit 10614f8

File tree

4 files changed

+113
-102
lines changed

4 files changed

+113
-102
lines changed

lib/webmachine/adapters/rack.rb

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -186,41 +186,5 @@ def each
186186
end
187187
end # class RequestBody
188188
end # class Rack
189-
190-
# Provides the same functionality as the parent Webmachine::Adapters::Rack
191-
# adapter, but allows the Webmachine application to be hosted at an
192-
# arbitrary path in a parent Rack application (as in Rack `map` or Rails
193-
# routing `mount`)
194-
#
195-
# This functionality is separated out from the parent class to preserve
196-
# backward compatibility in the behaviour of the parent Rack adpater.
197-
#
198-
# To use the adapter in a parent Rack application, map the Webmachine
199-
# application as follows in a rackup file or Rack::Builder:
200-
#
201-
# map '/foo' do
202-
# run SomeotherRackApp
203-
#
204-
# map '/bar' do
205-
# run MyWebmachineApp.adapter
206-
# end
207-
# end
208-
class RackMapped < Rack
209-
protected
210-
def routing_tokens(rack_req)
211-
routing_match = rack_req.path_info.match(Webmachine::Request::ROUTING_PATH_MATCH)
212-
routing_path = routing_match ? routing_match[1] : ""
213-
routing_path.split(SLASH)
214-
end
215-
216-
def base_uri(rack_req)
217-
# rack SCRIPT_NAME env var doesn't end with "/". This causes weird
218-
# behavour when URI.join concatenates URI components in
219-
# Webmachine::Decision::Flow#n11
220-
script_name = rack_req.script_name + SLASH
221-
URI.join(rack_req.base_url, script_name)
222-
end
223-
end
224-
225189
end # module Adapters
226190
end # module Webmachine
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require 'webmachine/adapters/rack'
2+
3+
module Webmachine
4+
module Adapters
5+
# Provides the same functionality as the parent Webmachine::Adapters::Rack
6+
# adapter, but allows the Webmachine application to be hosted at an
7+
# arbitrary path in a parent Rack application (as in Rack `map` or Rails
8+
# routing `mount`)
9+
#
10+
# This functionality is separated out from the parent class to preserve
11+
# backward compatibility in the behaviour of the parent Rack adpater.
12+
#
13+
# To use the adapter in a parent Rack application, map the Webmachine
14+
# application as follows in a rackup file or Rack::Builder:
15+
#
16+
# map '/foo' do
17+
# run SomeotherRackApp
18+
#
19+
# map '/bar' do
20+
# run MyWebmachineApp.adapter
21+
# end
22+
# end
23+
class RackMapped < Rack
24+
25+
protected
26+
27+
def routing_tokens(rack_req)
28+
routing_match = rack_req.path_info.match(Webmachine::Request::ROUTING_PATH_MATCH)
29+
routing_path = routing_match ? routing_match[1] : ""
30+
routing_path.split(SLASH)
31+
end
32+
33+
def base_uri(rack_req)
34+
# rack SCRIPT_NAME env var doesn't end with "/". This causes weird
35+
# behavour when URI.join concatenates URI components in
36+
# Webmachine::Decision::Flow#n11
37+
script_name = rack_req.script_name + SLASH
38+
URI.join(rack_req.base_url, script_name)
39+
end
40+
end # class RackMapped
41+
end # module Adapters
42+
end # module Webmachine
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
require 'webmachine/adapter'
2+
require 'webmachine/adapters/rack_mapped'
3+
require 'spec_helper'
4+
require 'webmachine/spec/adapter_lint'
5+
require 'rack/test'
6+
7+
describe Webmachine::Adapters::RackMapped do
8+
it_should_behave_like :adapter_lint do
9+
it "should set Server header" do
10+
response = client.request(Net::HTTP::Get.new("/test"))
11+
expect(response["Server"]).to match(/Webmachine/)
12+
expect(response["Server"]).to match(/Rack/)
13+
end
14+
end
15+
end
16+
17+
describe Webmachine::Adapters::RackMapped do
18+
class CreateResource < Webmachine::Resource
19+
def allowed_methods
20+
["POST"]
21+
end
22+
23+
def content_types_accepted
24+
[["application/json", :from_json]]
25+
end
26+
27+
def content_types_provided
28+
[["application/json", :to_json]]
29+
end
30+
31+
def post_is_create?
32+
true
33+
end
34+
35+
def create_path
36+
"created_path_here/123"
37+
end
38+
39+
def from_json
40+
response.body = %{ {"foo": "bar"} }
41+
end
42+
end
43+
44+
let(:app) do
45+
Rack::Builder.new do
46+
map '/some/route' do
47+
run(Webmachine::Application.new do |app|
48+
app.add_route(["test"], Test::Resource)
49+
app.add_route(["create_test"], CreateResource)
50+
app.configure do | config |
51+
config.adapter = :RackMapped
52+
end
53+
end.adapter)
54+
end
55+
end
56+
end
57+
58+
context "using Rack::Test" do
59+
include Rack::Test::Methods
60+
61+
it "provides the full request URI" do
62+
rack_response = get "some/route/test", nil, {"HTTP_ACCEPT" => "test/response.request_uri"}
63+
expect(rack_response.body).to eq "http://example.org/some/route/test"
64+
end
65+
66+
it "provides LOCATION header using custom base_uri when creating from POST request" do
67+
rack_response = post "/some/route/create_test", %{{"foo": "bar"}}, {"HTTP_ACCEPT" => "application/json", "CONTENT_TYPE" => "application/json"}
68+
expect(rack_response.headers["Location"]).to eq("http://example.org/some/route/created_path_here/123")
69+
end
70+
end
71+
end

spec/webmachine/adapters/rack_spec.rb

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,6 @@
1414
end
1515
end
1616

17-
describe Webmachine::Adapters::RackMapped do
18-
it_should_behave_like :adapter_lint do
19-
it "should set Server header" do
20-
response = client.request(Net::HTTP::Get.new("/test"))
21-
expect(response["Server"]).to match(/Webmachine/)
22-
expect(response["Server"]).to match(/Rack/)
23-
end
24-
end
25-
end
26-
2717
describe Webmachine::Adapters::Rack::RackResponse do
2818
context "on Rack < 1.5 release" do
2919
before { allow(Rack).to receive_messages(:release => "1.4") }
@@ -65,59 +55,3 @@
6555
end
6656
end
6757
end
68-
69-
describe Webmachine::Adapters::RackMapped do
70-
class CreateResource < Webmachine::Resource
71-
def allowed_methods
72-
["POST"]
73-
end
74-
75-
def content_types_accepted
76-
[["application/json", :from_json]]
77-
end
78-
79-
def content_types_provided
80-
[["application/json", :to_json]]
81-
end
82-
83-
def post_is_create?
84-
true
85-
end
86-
87-
def create_path
88-
"created_path_here/123"
89-
end
90-
91-
def from_json
92-
response.body = %{ {"foo": "bar"} }
93-
end
94-
end
95-
96-
let(:app) do
97-
Rack::Builder.new do
98-
map '/some/route' do
99-
run(Webmachine::Application.new do |app|
100-
app.add_route(["test"], Test::Resource)
101-
app.add_route(["create_test"], CreateResource)
102-
app.configure do | config |
103-
config.adapter = :RackMapped
104-
end
105-
end.adapter)
106-
end
107-
end
108-
end
109-
110-
context "using Rack::Test" do
111-
include Rack::Test::Methods
112-
113-
it "provides the full request URI" do
114-
rack_response = get "some/route/test", nil, {"HTTP_ACCEPT" => "test/response.request_uri"}
115-
expect(rack_response.body).to eq "http://example.org/some/route/test"
116-
end
117-
118-
it "provides LOCATION header using custom base_uri when creating from POST request" do
119-
rack_response = post "/some/route/create_test", %{{"foo": "bar"}}, {"HTTP_ACCEPT" => "application/json", "CONTENT_TYPE" => "application/json"}
120-
expect(rack_response.headers["Location"]).to eq("http://example.org/some/route/created_path_here/123")
121-
end
122-
end
123-
end

0 commit comments

Comments
 (0)