Skip to content

Commit

Permalink
Add host option to with_request_url test helper (#1773)
Browse files Browse the repository at this point in the history
- This allows testing components that use path helpers that are
 constrained by a subdomain in routes.rb.
  • Loading branch information
pbstriker38 authored Jun 15, 2023
1 parent 724e368 commit 5decd07
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 3 deletions.
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: 5

## main

* Allow Setting host when using the `with_request_url` test helper.

*Daniel Alfaro*

## 3.2.0

* Fix viewcomponent.org Axe violations.
Expand Down
10 changes: 9 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ with_controller_class(UsersController) do
end
```

### `#with_request_url(path)`
### `#with_request_url(path, host: nil)`

Set the URL of the current request (such as when using request-dependent path helpers):

Expand All @@ -342,6 +342,14 @@ with_request_url("/users/42") do
end
```

To use a specific host, pass the host param:

```ruby
with_request_url("/users/42", host: "app.example.com") do
render_inline(MyComponent.new)
end
```

### `#with_variant(variant)`

Set the Action Pack request variant for the given block:
Expand Down
20 changes: 20 additions & 0 deletions docs/guide/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,26 @@ class ExampleComponentTest < ViewComponent::TestCase
end
```

## Setting `request.host`

Since 3.3.0
{: .label }

Rails routes that have a subdomain constraint require `request.host` to be set correctly.

To set `request.host` for a test case, use `with_request_url` from `ViewComponent::TestHelpers`:

```ruby
class ExampleComponentTest < ViewComponent::TestCase
def test_with_request_url
with_request_url "/products/42", host: "app.example.com" do
render_inline ExampleComponent.new # contains i.e. `products_path` that is constrained to the 'app' subdomain
assert_link "Products", href: "/products"
end
end
end
```

### Query parameters

Since 2.41.0
Expand Down
6 changes: 5 additions & 1 deletion lib/view_component/test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,24 @@ def with_controller_class(klass)
# ```
#
# @param path [String] The path to set for the current request.
def with_request_url(path)
# @param host [String] The host to set for the current request.
def with_request_url(path, host: nil)
old_request_host = vc_test_request.host
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_controller = defined?(@vc_test_controller) && @vc_test_controller

path, query = path.split("?", 2)
vc_test_request.host = host if host
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", Rack::Utils.parse_nested_query(query))
vc_test_request.set_header(Rack::QUERY_STRING, query)
yield
ensure
vc_test_request.host = old_request_host
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
2 changes: 1 addition & 1 deletion test/sandbox/app/components/url_for_component.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= url_for({ key: "value" }.merge(request.query_parameters)) %>
<%= url_for(only_path: @only_path, params: { key: "value" }.merge(request.query_parameters)) %>
3 changes: 3 additions & 0 deletions test/sandbox/app/components/url_for_component.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true

class UrlForComponent < ViewComponent::Base
def initialize(only_path: true)
@only_path = only_path
end
end
16 changes: 16 additions & 0 deletions test/sandbox/test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,22 @@ def test_with_request_url_with_query_parameters
end
end

def test_with_request_url_with_host
with_request_url "/", host: "app.example.com" do
render_inline UrlForComponent.new(only_path: false)
assert_text "http://app.example.com/?key=value"
end

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

with_request_url "/products", host: "app.example.com" do
assert_equal "app.example.com", vc_test_request.host
end
end

def test_components_share_helpers_state
PartialHelper::State.reset

Expand Down

0 comments on commit 5decd07

Please sign in to comment.