Skip to content

Commit 70c6a79

Browse files
authored
Merge pull request #2550 from ViewComponent/copilot/add-set-protocol-for-url
Add protocol parameter to with_request_url test helper
2 parents f2bf04a + a366203 commit 70c6a79

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 6
1010

1111
## main
1212

13+
* Add `protocol` parameter to `with_request_url` test helper to enable testing with HTTPS protocol.
14+
15+
*Joel Hawksley*
16+
1317
## 4.3.0
1418

1519
* Fix load order issues for 3rd-party template handlers.

lib/view_component/test_helpers.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,24 +196,35 @@ def with_format(*formats)
196196
# end
197197
# ```
198198
#
199+
# To specify a protocol, pass the protocol param:
200+
#
201+
# ```ruby
202+
# with_request_url("/users/42", protocol: :https) do
203+
# render_inline(MyComponent.new)
204+
# end
205+
# ```
206+
#
199207
# @param full_path [String] The path to set for the current request.
200208
# @param host [String] The host to set for the current request.
201209
# @param method [String] The request method to set for the current request.
202-
def with_request_url(full_path, host: nil, method: nil)
210+
# @param protocol [Symbol] The protocol to set for the current request (e.g., `:http` or `:https`).
211+
def with_request_url(full_path, host: nil, method: nil, protocol: nil)
203212
old_request_host = vc_test_request.host
204213
old_request_method = vc_test_request.request_method
205214
old_request_path_info = vc_test_request.path_info
206215
old_request_path_parameters = vc_test_request.path_parameters
207216
old_request_query_parameters = vc_test_request.query_parameters
208217
old_request_query_string = vc_test_request.query_string
209218
old_request_format = vc_test_request.format.symbol
219+
old_request_scheme = vc_test_request.scheme
210220
old_controller = defined?(@vc_test_controller) && @vc_test_controller
211221

212222
path, query = full_path.split("?", 2)
213223
vc_test_request.instance_variable_set(:@fullpath, full_path)
214224
vc_test_request.instance_variable_set(:@original_fullpath, full_path)
215225
vc_test_request.host = host if host
216226
vc_test_request.request_method = method if method
227+
vc_test_request.set_header(Rack::RACK_URL_SCHEME, protocol.to_s) if protocol
217228
vc_test_request.path_info = path
218229
vc_test_request.path_parameters = Rails.application.routes.recognize_path_with_request(vc_test_request, path, {})
219230
vc_test_request.set_header("action_dispatch.request.query_parameters",
@@ -223,6 +234,7 @@ def with_request_url(full_path, host: nil, method: nil)
223234
ensure
224235
vc_test_request.host = old_request_host
225236
vc_test_request.request_method = old_request_method
237+
vc_test_request.set_header(Rack::RACK_URL_SCHEME, old_request_scheme)
226238
vc_test_request.path_info = old_request_path_info
227239
vc_test_request.path_parameters = old_request_path_parameters
228240
vc_test_request.set_header("action_dispatch.request.query_parameters", old_request_query_parameters)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class ProtocolComponent < ViewComponent::Base
4+
def call
5+
content_tag(:div, "Protocol: #{request.scheme}, SSL: #{request.ssl?}", class: "protocol")
6+
end
7+
end

test/sandbox/test/rendering_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,23 @@ def test_with_request_url_with_host
964964
end
965965
end
966966

967+
def test_with_request_url_with_https_protocol
968+
with_request_url "/", protocol: :https do
969+
render_inline UrlForComponent.new(only_path: false)
970+
assert_text "https://test.host/?key=value"
971+
end
972+
973+
with_request_url "/products", protocol: :https, host: "secure.example.com" do
974+
render_inline UrlForComponent.new(only_path: false)
975+
assert_text "https://secure.example.com/products?key=value"
976+
end
977+
978+
with_request_url "/products", protocol: :https do
979+
assert_equal "https", vc_test_request.scheme
980+
assert vc_test_request.ssl?
981+
end
982+
end
983+
967984
def test_components_share_helpers_state
968985
PartialHelper::State.reset
969986

test/sandbox/test/test_helper_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,40 @@ def test_vc_test_view_context_is_shared_reference
4949
render_inline(CustomFormBuilderComponent.new(builder: builder)) { "Label content" }
5050
assert_selector("label[for=foo]", text: "Label content")
5151
end
52+
53+
def test_with_request_url_specifying_http_protocol
54+
with_request_url "/products", protocol: :http do
55+
render_inline(ProtocolComponent.new)
56+
end
57+
58+
assert_selector(".protocol", text: "Protocol: http, SSL: false")
59+
end
60+
61+
def test_with_request_url_specifying_https_protocol
62+
with_request_url "/products", protocol: :https do
63+
render_inline(ProtocolComponent.new)
64+
end
65+
66+
assert_selector(".protocol", text: "Protocol: https, SSL: true")
67+
end
68+
69+
def test_with_request_url_restores_original_protocol
70+
# Store original protocol
71+
original_scheme = vc_test_request.scheme
72+
73+
with_request_url "/products", protocol: :https do
74+
assert_equal "https", vc_test_request.scheme
75+
end
76+
77+
# Verify original protocol is restored
78+
assert_equal original_scheme, vc_test_request.scheme
79+
end
80+
81+
def test_with_request_url_with_protocol_and_host
82+
with_request_url "/products", protocol: :https, host: "secure.example.com" do
83+
render_inline(ProtocolComponent.new)
84+
end
85+
86+
assert_selector(".protocol", text: "Protocol: https, SSL: true")
87+
end
5288
end

0 commit comments

Comments
 (0)