Skip to content

Commit 4241a14

Browse files
Fix request.path inside with_request_url helper (#1463)
* Fix request.path inside with_request_url helper `#with_request_url` is great. It was started in #1058, but later #1221 introduced a tiny regression in the parsing of segments. When using `#with_request_url` including a query string, the `#path` helper would now contain the query string segment, when it should not. This led to `#fullpath` having the query string twice. This commit fixes the parsing and amends the test cases to cover this scenario. * Apply suggestions from code review Co-authored-by: Joel Hawksley <joelhawksley@github.com>
1 parent f469381 commit 4241a14

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ title: Changelog
99

1010
## main
1111

12+
* Fix bug where `#with_request_url`, when used with query string, set the incorrect `request.path` and `request.fullpath`.
13+
14+
*Franz Liedke*
15+
1216
* Add link to [ViewComponentAttributes](https://github.com/amba-Health/view_component_attributes) in Resources section of docs.
1317

1418
*Romaric Pascal*

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ ViewComponent is built by over a hundred members of the community, including:
137137
<img src="https://avatars.githubusercontent.com/elia?s=64" alt="elia" width="32" />
138138
<img src="https://avatars.githubusercontent.com/franco?s=64" alt="franco" width="32" />
139139
<img src="https://avatars.githubusercontent.com/franks921?s=64" alt="franks921" width="32" />
140+
<img src="https://avatars.githubusercontent.com/franzliedke?s=64" alt="franzliedke" width="32" />
140141
<img src="https://avatars.githubusercontent.com/fsateler?s=64" alt="fsateler" width="32" />
141142
<img src="https://avatars.githubusercontent.com/fugufish?s=64" alt="fugufish" width="32" />
142143
<img src="https://avatars.githubusercontent.com/g13ydson?s=64" alt="g13ydson" width="32" />

lib/view_component/test_helpers.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,11 @@ def with_request_url(path)
190190
old_request_query_string = request.query_string
191191
old_controller = defined?(@controller) && @controller
192192

193+
path, query = path.split("?", 2)
193194
request.path_info = path
194195
request.path_parameters = Rails.application.routes.recognize_path(path)
195-
request.set_header("action_dispatch.request.query_parameters", Rack::Utils.parse_nested_query(path.split("?")[1]))
196-
request.set_header(Rack::QUERY_STRING, path.split("?")[1])
196+
request.set_header("action_dispatch.request.query_parameters", Rack::Utils.parse_nested_query(query))
197+
request.set_header(Rack::QUERY_STRING, query)
197198
yield
198199
ensure
199200
request.path_info = old_request_path_info

test/sandbox/test/rendering_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,9 @@ def test_with_request_url_with_query_parameters
935935
end
936936

937937
with_request_url "/products?mykey=myvalue&otherkey=othervalue" do
938+
assert_equal "/products", request.path
938939
assert_equal "mykey=myvalue&otherkey=othervalue", request.query_string
940+
assert_equal "/products?mykey=myvalue&otherkey=othervalue", request.fullpath
939941
end
940942

941943
with_request_url "/products?mykey[mynestedkey]=myvalue" do

0 commit comments

Comments
 (0)