|
4 | 4 | # Copyright, 2022, by Samuel Williams. |
5 | 5 |
|
6 | 6 | require 'protocol/websocket/json_message' |
| 7 | +require 'protocol/http/middleware/builder' |
7 | 8 |
|
8 | 9 | require 'async/websocket/client' |
9 | 10 | require 'async/websocket/server' |
|
12 | 13 | require 'sus/fixtures/async/http/server_context' |
13 | 14 |
|
14 | 15 | ServerExamples = Sus::Shared('a websocket server') do |
15 | | - include Sus::Fixtures::Async::HTTP::ServerContext |
16 | | - |
17 | | - let(:message) {"Hello World"} |
18 | | - |
19 | | - let(:app) do |
20 | | - Protocol::HTTP::Middleware.for do |request| |
21 | | - Async::WebSocket::Adapters::HTTP.open(request) do |connection| |
22 | | - connection.send_text(message) |
23 | | - connection.close |
24 | | - end or Protocol::HTTP::Response[404, {}, []] |
25 | | - end |
26 | | - end |
27 | | - |
28 | 16 | let(:websocket_client) {Async::WebSocket::Client.open(client_endpoint)} |
29 | 17 |
|
30 | | - it "can establish connection" do |
31 | | - connection = websocket_client.connect(endpoint.authority, "/server") |
32 | | - |
33 | | - begin |
34 | | - expect(connection.read).to be == message |
35 | | - expect(connection.read).to be_nil |
36 | | - expect(connection).to be(:closed?) |
37 | | - ensure |
38 | | - connection.close |
39 | | - end |
40 | | - end |
41 | | - |
42 | | - with "headers" do |
43 | | - let(:headers) {{"foo" => "bar"}} |
| 18 | + with 'generic application' do |
| 19 | + let(:message) {"Hello World"} |
44 | 20 |
|
45 | 21 | let(:app) do |
46 | 22 | Protocol::HTTP::Middleware.for do |request| |
47 | 23 | Async::WebSocket::Adapters::HTTP.open(request) do |connection| |
48 | | - message = Protocol::WebSocket::JSONMessage.generate(request.headers.fields) |
49 | | - message.send(connection) |
50 | | - |
| 24 | + connection.send_text(message) |
51 | 25 | connection.close |
52 | 26 | end or Protocol::HTTP::Response[404, {}, []] |
53 | 27 | end |
54 | 28 | end |
55 | 29 |
|
56 | | - it "can send headers" do |
57 | | - connection = websocket_client.connect(endpoint.authority, "/headers", headers: headers) |
| 30 | + it "can establish connection" do |
| 31 | + connection = websocket_client.connect(endpoint.authority, "/server") |
58 | 32 |
|
59 | 33 | begin |
60 | | - json_message = Protocol::WebSocket::JSONMessage.wrap(connection.read) |
| 34 | + expect(connection.read).to be == message |
| 35 | + expect(connection.read).to be_nil |
| 36 | + expect(connection).to be(:closed?) |
| 37 | + ensure |
| 38 | + connection.close |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + with "headers" do |
| 43 | + let(:headers) {{"foo" => "bar"}} |
| 44 | + |
| 45 | + let(:app) do |
| 46 | + Protocol::HTTP::Middleware.for do |request| |
| 47 | + Async::WebSocket::Adapters::HTTP.open(request) do |connection| |
| 48 | + message = Protocol::WebSocket::JSONMessage.generate(request.headers.fields) |
| 49 | + message.send(connection) |
| 50 | + |
| 51 | + connection.close |
| 52 | + end or Protocol::HTTP::Response[404, {}, []] |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + it "can send headers" do |
| 57 | + connection = websocket_client.connect(endpoint.authority, "/headers", headers: headers) |
61 | 58 |
|
62 | | - expect(json_message.to_h).to have_keys(*headers.keys) |
| 59 | + begin |
| 60 | + json_message = Protocol::WebSocket::JSONMessage.wrap(connection.read) |
| 61 | + |
| 62 | + expect(json_message.to_h).to have_keys(*headers.keys) |
| 63 | + expect(connection.read).to be_nil |
| 64 | + expect(connection).to be(:closed?) |
| 65 | + ensure |
| 66 | + connection.close |
| 67 | + end |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + with 'server middleware' do |
| 73 | + let(:app) do |
| 74 | + Protocol::HTTP::Middleware.build do |
| 75 | + use Async::WebSocket::Server do |connection| |
| 76 | + connection.send_text("Hello World") |
| 77 | + connection.close |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + it "can establish connection" do |
| 83 | + connection = websocket_client.connect(endpoint.authority, "/server") |
| 84 | + |
| 85 | + begin |
| 86 | + expect(connection.read).to be == "Hello World" |
63 | 87 | expect(connection.read).to be_nil |
64 | 88 | expect(connection).to be(:closed?) |
65 | 89 | ensure |
|
69 | 93 | end |
70 | 94 | end |
71 | 95 |
|
72 | | -describe Async::HTTP::Protocol::HTTP1 do |
73 | | - let(:protocol) {subject} |
| 96 | +describe Async::WebSocket::Server do |
| 97 | + include Sus::Fixtures::Async::HTTP::ServerContext |
74 | 98 |
|
75 | | - it_behaves_like ServerExamples |
76 | | -end |
77 | | - |
78 | | -describe Async::HTTP::Protocol::HTTP2 do |
79 | | - let(:protocol) {subject} |
| 99 | + with 'http/1' do |
| 100 | + let(:protocol) {Async::HTTP::Protocol::HTTP1} |
| 101 | + it_behaves_like ServerExamples |
| 102 | + end |
80 | 103 |
|
81 | | - it_behaves_like ServerExamples |
| 104 | + with 'http/2' do |
| 105 | + let(:protocol) {Async::HTTP::Protocol::HTTP2} |
| 106 | + it_behaves_like ServerExamples |
| 107 | + end |
82 | 108 | end |
0 commit comments