Skip to content

Commit 5cf3b78

Browse files
Copilotjoelhawksley
andcommitted
Add protocol parameter to with_request_url for https support
Co-authored-by: joelhawksley <1940294+joelhawksley@users.noreply.github.com>
1 parent 7bddd9b commit 5cf3b78

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

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 [String] 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) 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/test_helper_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,32 @@ 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
5280
end

0 commit comments

Comments
 (0)