From 9a5ba148397742deb2e751b8867a7dfad4c4f3a8 Mon Sep 17 00:00:00 2001 From: Thomas Cannon Date: Sun, 4 Aug 2024 00:48:19 -0400 Subject: [PATCH] Add `cable_ready.integration_test_request_encoding` initializer * This has been adapted from https://github.com/hotwired/turbo-rails/blob/9b17a3be3705786d72c3ae77fde5a9b3006555d7/lib/turbo/engine.rb#L119 * In order to streamline development, Rails integration tests should be able to support `as: :cable_ready` to test Cable Ready responses * This is done by setting up a new request encoding that ActionDispatch's integration tests can use. In this case, the encoder is barebones and is largely focused on streamlining how the request `Accept` headers are setup --- lib/cable_ready/engine.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/cable_ready/engine.rb b/lib/cable_ready/engine.rb index 321fae69..f59afd2c 100644 --- a/lib/cable_ready/engine.rb +++ b/lib/cable_ready/engine.rb @@ -49,6 +49,22 @@ class Engine < Rails::Engine end end + initializer "cable_ready.integration_test_request_encoding" do + # standard:disable Lint/ConstantDefinitionInBlock + ActiveSupport.on_load(:action_dispatch_integration_test) do + # Support `as: :cable_ready`. Public `register_encoder` API is a little too strict. + class ActionDispatch::RequestEncoder + class CableReadyStreamEncoder < IdentityEncoder + header = [Mime[:cable_ready], Mime[:html]].join(",") + define_method(:accept_header) { header } + end + + @encoders[:cable_ready] = CableReadyStreamEncoder.new + end + end + # standard:enable Lint/ConstantDefinitionInBlock + end + initializer "cable_ready.importmap", before: "importmap" do |app| if app.config.respond_to?(:importmap) app.config.importmap.paths << Engine.root.join("lib/cable_ready/importmap.rb")