|
11 | 11 |
|
12 | 12 | require 'async/io/ssl_socket'
|
13 | 13 |
|
14 |
| -require 'async/rspec/reactor' |
| 14 | +require_relative 'server_context' |
15 | 15 |
|
16 | 16 | require 'localhost/authority'
|
17 | 17 |
|
18 | 18 | RSpec.shared_examples Async::HTTP::Body do
|
19 | 19 | let(:client) {Async::HTTP::Client.new(client_endpoint, protocol: described_class)}
|
20 | 20 |
|
21 |
| - it "can stream requests" do |
22 |
| - server = Async::HTTP::Server.for(server_endpoint, protocol: described_class) do |request| |
23 |
| - input = request.body |
24 |
| - output = Async::HTTP::Body::Writable.new |
25 |
| - |
26 |
| - Async::Task.current.async do |task| |
27 |
| - input.each do |chunk| |
28 |
| - output.write(chunk.reverse) |
| 21 | + context 'with echo server' do |
| 22 | + let(:server) do |
| 23 | + server = Async::HTTP::Server.for(server_endpoint, protocol: described_class) do |request| |
| 24 | + input = request.body |
| 25 | + output = Async::HTTP::Body::Writable.new |
| 26 | + |
| 27 | + Async::Task.current.async do |task| |
| 28 | + input.each do |chunk| |
| 29 | + output.write(chunk.reverse) |
| 30 | + end |
| 31 | + |
| 32 | + output.close |
29 | 33 | end
|
30 | 34 |
|
| 35 | + Protocol::HTTP::Response[200, [], output] |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + it "can stream requests" do |
| 40 | + output = Async::HTTP::Body::Writable.new |
| 41 | + |
| 42 | + reactor.async do |task| |
| 43 | + output.write("Hello World!") |
31 | 44 | output.close
|
32 | 45 | end
|
33 | 46 |
|
34 |
| - Protocol::HTTP::Response[200, [], output] |
35 |
| - end |
36 |
| - |
37 |
| - server_task = reactor.async do |
38 |
| - server.run |
39 |
| - end |
40 |
| - |
41 |
| - output = Async::HTTP::Body::Writable.new |
42 |
| - |
43 |
| - reactor.async do |task| |
44 |
| - output.write("Hello World!") |
45 |
| - output.close |
| 47 | + response = client.post("/", {}, output) |
| 48 | + |
| 49 | + expect(response).to be_success |
| 50 | + expect(response.read).to be == "!dlroW olleH" |
46 | 51 | end
|
47 |
| - |
48 |
| - response = client.post("/", {}, output) |
49 |
| - |
50 |
| - expect(response).to be_success |
51 |
| - expect(response.read).to be == "!dlroW olleH" |
52 |
| - |
53 |
| - server_task.stop |
54 |
| - client.close |
55 | 52 | end
|
56 | 53 |
|
57 |
| - it "can stream response" do |
58 |
| - notification = Async::Notification.new |
| 54 | + context "with streaming server" do |
| 55 | + let(:notification) {Async::Notification.new} |
59 | 56 |
|
60 |
| - server = Async::HTTP::Server.for(server_endpoint, protocol: described_class) do |request| |
61 |
| - body = Async::HTTP::Body::Writable.new |
62 |
| - |
63 |
| - Async::Task.current.async do |task| |
64 |
| - 10.times do |i| |
65 |
| - body.write("#{i}") |
66 |
| - notification.wait |
| 57 | + let(:server) do |
| 58 | + Async::HTTP::Server.for(server_endpoint, protocol: described_class) do |request| |
| 59 | + body = Async::HTTP::Body::Writable.new |
| 60 | + |
| 61 | + Async::Task.current.async do |task| |
| 62 | + 10.times do |i| |
| 63 | + body.write("#{i}") |
| 64 | + notification.wait |
| 65 | + end |
| 66 | + |
| 67 | + body.close |
67 | 68 | end
|
68 | 69 |
|
69 |
| - body.close |
| 70 | + Protocol::HTTP::Response[200, {}, body] |
70 | 71 | end
|
71 |
| - |
72 |
| - Protocol::HTTP::Response[200, {}, body] |
73 | 72 | end
|
74 | 73 |
|
75 |
| - server_task = reactor.async do |
76 |
| - server.run |
77 |
| - end |
78 |
| - |
79 |
| - response = client.get("/") |
80 |
| - |
81 |
| - expect(response).to be_success |
82 |
| - |
83 |
| - j = 0 |
84 |
| - # This validates interleaving |
85 |
| - response.body.each do |line| |
86 |
| - expect(line.to_i).to be == j |
87 |
| - j += 1 |
| 74 | + it "can stream response" do |
| 75 | + response = client.get("/") |
88 | 76 |
|
89 |
| - notification.signal |
90 |
| - end |
91 |
| - |
92 |
| - server_task.stop |
93 |
| - client.close |
| 77 | + expect(response).to be_success |
| 78 | + |
| 79 | + j = 0 |
| 80 | + # This validates interleaving |
| 81 | + response.body.each do |line| |
| 82 | + expect(line.to_i).to be == j |
| 83 | + j += 1 |
| 84 | + |
| 85 | + notification.signal |
| 86 | + end |
| 87 | + end |
94 | 88 | end
|
95 | 89 | end
|
96 | 90 |
|
97 | 91 | RSpec.describe Async::HTTP::Protocol::HTTP1 do
|
98 |
| - include_context Async::RSpec::Reactor |
| 92 | + include_context Async::HTTP::Server |
99 | 93 |
|
100 | 94 | let(:endpoint) {Async::HTTP::Endpoint.parse('http://127.0.0.1:9296', reuse_port: true)}
|
101 |
| - let(:client_endpoint) {endpoint} |
102 |
| - let(:server_endpoint) {endpoint} |
103 | 95 |
|
104 | 96 | it_should_behave_like Async::HTTP::Body
|
105 | 97 | end
|
106 | 98 |
|
107 | 99 | RSpec.describe Async::HTTP::Protocol::HTTPS do
|
108 |
| - include_context Async::RSpec::Reactor |
| 100 | + include_context Async::HTTP::Server |
| 101 | + |
109 | 102 | let(:authority) {Localhost::Authority.new}
|
110 | 103 |
|
111 | 104 | let(:server_context) {authority.server_context}
|
112 | 105 | let(:client_context) {authority.client_context}
|
113 | 106 |
|
114 | 107 | # Shared port for localhost network tests.
|
115 |
| - let(:server_endpoint) {Async::HTTP::Endpoint.parse("https://localhost:9296", ssl_context: server_context)} |
116 |
| - let(:client_endpoint) {Async::HTTP::Endpoint.parse("https://localhost:9296", ssl_context: client_context)} |
| 108 | + let(:server_endpoint) {Async::HTTP::Endpoint.parse("https://localhost:9296", ssl_context: server_context, reuse_port: true)} |
| 109 | + let(:client_endpoint) {Async::HTTP::Endpoint.parse("https://localhost:9296", ssl_context: client_context, reuse_port: true)} |
117 | 110 |
|
118 | 111 | it_should_behave_like Async::HTTP::Body
|
119 | 112 | end
|
0 commit comments