Skip to content

Commit 33c52f4

Browse files
committed
specify explicit base_uri from rack SCRIPT_NAME when creating request for RackMapped
1 parent 1ba2367 commit 33c52f4

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

lib/webmachine/adapters/rack.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,18 @@ def routing_tokens(rack_req)
100100
nil # no-op for default, un-mapped rack adapter
101101
end
102102

103+
def base_uri(rack_req)
104+
nil # no-op for default, un-mapped rack adapter
105+
end
106+
103107
private
104108
def build_webmachine_request(rack_req, headers)
105109
Webmachine::Request.new(rack_req.request_method,
106110
rack_req.url,
107111
headers,
108112
RequestBody.new(rack_req),
109-
routing_tokens(rack_req)
113+
routing_tokens(rack_req),
114+
base_uri(rack_req)
110115
)
111116
end
112117

@@ -207,6 +212,14 @@ def routing_tokens(rack_req)
207212
routing_path = routing_match ? routing_match[1] : ""
208213
routing_path.split(SLASH)
209214
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
210223
end
211224

212225
end # module Adapters

spec/webmachine/adapters/rack_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,38 @@
6767
end
6868

6969
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+
7096
let(:app) do
7197
Rack::Builder.new do
7298
map '/some/route' do
7399
run(Webmachine::Application.new do |app|
74100
app.add_route(["test"], Test::Resource)
101+
app.add_route(["create_test"], CreateResource)
75102
app.configure do | config |
76103
config.adapter = :RackMapped
77104
end
@@ -87,5 +114,10 @@
87114
rack_response = get "some/route/test", nil, {"HTTP_ACCEPT" => "test/response.request_uri"}
88115
expect(rack_response.body).to eq "http://example.org/some/route/test"
89116
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
90122
end
91123
end

0 commit comments

Comments
 (0)