Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ nav_order: 6

## main

* Add `protocol` parameter to `with_request_url` test helper to enable testing with HTTPS protocol.

*Joel Hawksley*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😂 if only Copilot would be vain enough to credit itself!


## 4.3.0

* Fix load order issues for 3rd-party template handlers.
Expand Down
14 changes: 13 additions & 1 deletion lib/view_component/test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,24 +196,35 @@ def with_format(*formats)
# end
# ```
#
# To specify a protocol, pass the protocol param:
#
# ```ruby
# with_request_url("/users/42", protocol: :https) do
# render_inline(MyComponent.new)
# end
# ```
#
# @param full_path [String] The path to set for the current request.
# @param host [String] The host to set for the current request.
# @param method [String] The request method to set for the current request.
def with_request_url(full_path, host: nil, method: nil)
# @param protocol [Symbol] The protocol to set for the current request (e.g., `:http` or `:https`).
def with_request_url(full_path, host: nil, method: nil, protocol: nil)
old_request_host = vc_test_request.host
old_request_method = vc_test_request.request_method
old_request_path_info = vc_test_request.path_info
old_request_path_parameters = vc_test_request.path_parameters
old_request_query_parameters = vc_test_request.query_parameters
old_request_query_string = vc_test_request.query_string
old_request_format = vc_test_request.format.symbol
old_request_scheme = vc_test_request.scheme
old_controller = defined?(@vc_test_controller) && @vc_test_controller

path, query = full_path.split("?", 2)
vc_test_request.instance_variable_set(:@fullpath, full_path)
vc_test_request.instance_variable_set(:@original_fullpath, full_path)
vc_test_request.host = host if host
vc_test_request.request_method = method if method
vc_test_request.set_header(Rack::RACK_URL_SCHEME, protocol.to_s) if protocol
vc_test_request.path_info = path
vc_test_request.path_parameters = Rails.application.routes.recognize_path_with_request(vc_test_request, path, {})
vc_test_request.set_header("action_dispatch.request.query_parameters",
Expand All @@ -223,6 +234,7 @@ def with_request_url(full_path, host: nil, method: nil)
ensure
vc_test_request.host = old_request_host
vc_test_request.request_method = old_request_method
vc_test_request.set_header(Rack::RACK_URL_SCHEME, old_request_scheme)
vc_test_request.path_info = old_request_path_info
vc_test_request.path_parameters = old_request_path_parameters
vc_test_request.set_header("action_dispatch.request.query_parameters", old_request_query_parameters)
Expand Down
7 changes: 7 additions & 0 deletions test/sandbox/app/components/protocol_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class ProtocolComponent < ViewComponent::Base
def call
content_tag(:div, "Protocol: #{request.scheme}, SSL: #{request.ssl?}", class: "protocol")
end
end
17 changes: 17 additions & 0 deletions test/sandbox/test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,23 @@ def test_with_request_url_with_host
end
end

def test_with_request_url_with_https_protocol
with_request_url "/", protocol: :https do
render_inline UrlForComponent.new(only_path: false)
assert_text "https://test.host/?key=value"
end

with_request_url "/products", protocol: :https, host: "secure.example.com" do
render_inline UrlForComponent.new(only_path: false)
assert_text "https://secure.example.com/products?key=value"
end

with_request_url "/products", protocol: :https do
assert_equal "https", vc_test_request.scheme
assert vc_test_request.ssl?
end
end

def test_components_share_helpers_state
PartialHelper::State.reset

Expand Down
36 changes: 36 additions & 0 deletions test/sandbox/test/test_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,40 @@ def test_vc_test_view_context_is_shared_reference
render_inline(CustomFormBuilderComponent.new(builder: builder)) { "Label content" }
assert_selector("label[for=foo]", text: "Label content")
end

def test_with_request_url_specifying_http_protocol
with_request_url "/products", protocol: :http do
render_inline(ProtocolComponent.new)
end

assert_selector(".protocol", text: "Protocol: http, SSL: false")
end

def test_with_request_url_specifying_https_protocol
with_request_url "/products", protocol: :https do
render_inline(ProtocolComponent.new)
end

assert_selector(".protocol", text: "Protocol: https, SSL: true")
end

def test_with_request_url_restores_original_protocol
# Store original protocol
original_scheme = vc_test_request.scheme

with_request_url "/products", protocol: :https do
assert_equal "https", vc_test_request.scheme
end

# Verify original protocol is restored
assert_equal original_scheme, vc_test_request.scheme
end

def test_with_request_url_with_protocol_and_host
with_request_url "/products", protocol: :https, host: "secure.example.com" do
render_inline(ProtocolComponent.new)
end

assert_selector(".protocol", text: "Protocol: https, SSL: true")
end
end
Loading