Repository navigation
Register the Turbo Stream test encoder once - #760
Merged
brunoprietog merged 2 commits intoNov 22, 2026
Merged
Conversation
Since the `action_dispatch_integration_test` hook is invoked for each integration test, the whole `TurboStreamEncoder` is (re)defined many times. Ideally, I think, `@encoders[:turbo_stream]` should be set once, but since `RequestEncoder` does not expose straightforward API to check if an encoder is registered, we'd have to use something like ```ruby ActionDispatch::RequestEncoder::IdentityEncoder === ActionDispatch::RequestEncoder.encoder(:turbo_stream) ``` which is not handy. NB: requiring AS is not necessary -- we can use the self-alias trick instead
Contributor
Author
|
any thoughts on this? using method redefinition is also safer since it ensures that redefined method actually exists (that's not the case right now) |
viralpraxis
commented
Nov 27, 2025
| class TurboStreamEncoder < IdentityEncoder | ||
| header = [ Mime[:turbo_stream], Mime[:html] ].join(",") | ||
| define_method(:accept_header) { header } | ||
| redefine_method(:accept_header) { header } |
Contributor
Author
There was a problem hiding this comment.
PORO alternative (https://bugs.ruby-lang.org/issues/13574):
alias accept_header accept_header
define_method(:accept_header) { header }accept_header method
brunoprietog
approved these changes
Nov 22, 2026
brunoprietog
left a comment
Contributor
There was a problem hiding this comment.
Thanks @viralpraxis!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since the
action_dispatch_integration_testhook is invoked for each integration test, the wholeTurboStreamEncoderis (re)defined many times.Ideally, I think,
@encoders[:turbo_stream]should be set once, but sinceRequestEncoderdoes not expose straightforward API to check if an encoder is registered, we'd have to use something likewhich is not handy.
NB: requiring AS is not necessary -- we can use the self-alias trick instead
Edit:
Register the shared Turbo Stream test encoder once using Active Support’s run_once option. This prevents accept_header redefinition warnings when the integration test hook runs multiple times.