Merge pull request #52169 from ioquatix/routes-websocket

Add route helper for websockets.
This commit is contained in:
Rafael Mendonça França 2024-08-14 14:32:47 -03:00 committed by GitHub
commit 455cf48c7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 0 deletions

View File

@ -75,4 +75,8 @@
*Cyril Blaecke*
* Add `connect` route helper.
*Samuel Williams*
Please check [7-2-stable](https://github.com/rails/rails/blob/7-2-stable/actionpack/CHANGELOG.md) for previous changes.

View File

@ -786,6 +786,17 @@ module ActionDispatch
match(*path_or_actions, as:, to:, controller:, action:, on:, defaults:, constraints:, anchor:, format:, path:, **mapping, via: :options, &block)
self
end
# Define a route that recognizes HTTP CONNECT (and GET) requests. More
# specifically this recognizes HTTP/1 protocol upgrade requests and HTTP/2
# CONNECT requests with the protocol pseudo header. For supported arguments,
# see [match](rdoc-ref:Base#match)
#
# connect 'live', to: 'live#index'
def connect(*path_or_actions, as: DEFAULT, via: nil, to: nil, controller: nil, action: nil, on: nil, defaults: nil, constraints: nil, anchor: false, format: false, path: nil, internal: nil, **mapping, &block)
match(*path_or_actions, as:, to:, controller:, action:, on:, defaults:, constraints:, anchor:, format:, path:, **mapping, via: [:get, :connect], &block)
self
end
end
# You may wish to organize groups of controllers under a namespace. Most

View File

@ -356,6 +356,20 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
assert_equal "openid#login", @response.body
end
def test_websocket
draw do
connect "chat/live", to: "chat#live"
end
# HTTP/1.1 connection upgrade:
get "/chat/live", headers: { "REQUEST_METHOD" => "GET", "HTTP_CONNECTION" => "Upgrade", "HTTP_UPGRADE" => "websocket" }
assert_equal "chat#live", @response.body
# `rack.protocol` connection:
get "/chat/live", headers: { "REQUEST_METHOD" => "CONNECT", "rack.protocol" => "websocket" }
assert_equal "chat#live", @response.body
end
def test_bookmarks
draw do
scope "bookmark", controller: "bookmarks", as: :bookmark do