Skip to content

Commit 455cf48

Browse files
authored
Merge pull request rails#52169 from ioquatix/routes-websocket
Add route helper for websockets.
2 parents eaa74ee + 1c988ae commit 455cf48

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

actionpack/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,8 @@
7575

7676
*Cyril Blaecke*
7777

78+
* Add `connect` route helper.
79+
80+
*Samuel Williams*
81+
7882
Please check [7-2-stable](https://github.com/rails/rails/blob/7-2-stable/actionpack/CHANGELOG.md) for previous changes.

actionpack/lib/action_dispatch/routing/mapper.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,17 @@ def options(*path_or_actions, as: DEFAULT, via: nil, to: nil, controller: nil, a
786786
match(*path_or_actions, as:, to:, controller:, action:, on:, defaults:, constraints:, anchor:, format:, path:, **mapping, via: :options, &block)
787787
self
788788
end
789+
790+
# Define a route that recognizes HTTP CONNECT (and GET) requests. More
791+
# specifically this recognizes HTTP/1 protocol upgrade requests and HTTP/2
792+
# CONNECT requests with the protocol pseudo header. For supported arguments,
793+
# see [match](rdoc-ref:Base#match)
794+
#
795+
# connect 'live', to: 'live#index'
796+
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)
797+
match(*path_or_actions, as:, to:, controller:, action:, on:, defaults:, constraints:, anchor:, format:, path:, **mapping, via: [:get, :connect], &block)
798+
self
799+
end
789800
end
790801

791802
# You may wish to organize groups of controllers under a namespace. Most

actionpack/test/dispatch/routing_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,20 @@ def test_openid
356356
assert_equal "openid#login", @response.body
357357
end
358358

359+
def test_websocket
360+
draw do
361+
connect "chat/live", to: "chat#live"
362+
end
363+
364+
# HTTP/1.1 connection upgrade:
365+
get "/chat/live", headers: { "REQUEST_METHOD" => "GET", "HTTP_CONNECTION" => "Upgrade", "HTTP_UPGRADE" => "websocket" }
366+
assert_equal "chat#live", @response.body
367+
368+
# `rack.protocol` connection:
369+
get "/chat/live", headers: { "REQUEST_METHOD" => "CONNECT", "rack.protocol" => "websocket" }
370+
assert_equal "chat#live", @response.body
371+
end
372+
359373
def test_bookmarks
360374
draw do
361375
scope "bookmark", controller: "bookmarks", as: :bookmark do

0 commit comments

Comments
 (0)