Skip to content

Commit

Permalink
Extend test to interleave WebSocket with regular requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Sep 4, 2024
1 parent 8e455cc commit b1d9c42
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
6 changes: 5 additions & 1 deletion examples/websockets/config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ require 'async/websocket'
require 'async/websocket/adapters/rack'

class App
def handle_normal_request(env)
[200, {'content-type' => 'text/plain'}, ["Hello World"]]
end

def call(env)
Async::WebSocket::Adapters::Rack.open(env) do |connection|
message = Protocol::WebSocket::TextMessage.generate({body: "Hello World"})
Expand All @@ -12,7 +16,7 @@ class App
while message = connection.read
connection.write(message)
end
end or [400, {}, []]
end or handle_normal_request(env)
end
end

Expand Down
18 changes: 14 additions & 4 deletions test/falcon/command/virtual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,21 @@ def around

it "can upgrade to websocket" do
Sync do
Async::WebSocket::Client.connect(host_endpoint) do |connection|
message = Protocol::WebSocket::TextMessage.generate({body: "Hello World"})
2.times do
# Normal request:
request = Protocol::HTTP::Request.new("https", "websockets.localhost", "GET", "/index")
response = secure_client.call(request)

connection.write(message)
expect(connection.read).to be == message
expect(response).to be(:success?)
expect(response.read).to be == "Hello World"

# WebSocket request:
Async::WebSocket::Client.connect(host_endpoint) do |connection|
message = Protocol::WebSocket::TextMessage.generate({body: "Hello World"})

connection.write(message)
expect(connection.read).to be == message
end
end
end
end
Expand Down

0 comments on commit b1d9c42

Please sign in to comment.