Skip to content

Commit 1508345

Browse files
committed
Update body spec to use server context.
1 parent e24c1bc commit 1508345

File tree

2 files changed

+65
-71
lines changed

2 files changed

+65
-71
lines changed

spec/async/http/body_spec.rb

Lines changed: 58 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,109 +11,100 @@
1111

1212
require 'async/io/ssl_socket'
1313

14-
require 'async/rspec/reactor'
14+
require_relative 'server_context'
1515

1616
require 'localhost/authority'
1717

1818
RSpec.shared_examples Async::HTTP::Body do
1919
let(:client) {Async::HTTP::Client.new(client_endpoint, protocol: described_class)}
2020

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+
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
2933
end
3034

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!")
3144
output.close
3245
end
3346

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"
4651
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
5552
end
5653

57-
it "can stream response" do
58-
notification = Async::Notification.new
54+
context "with streaming server" do
55+
let(:notification) {Async::Notification.new}
5956

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
6768
end
6869

69-
body.close
70+
Protocol::HTTP::Response[200, {}, body]
7071
end
71-
72-
Protocol::HTTP::Response[200, {}, body]
7372
end
7473

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("/")
8876

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
9488
end
9589
end
9690

9791
RSpec.describe Async::HTTP::Protocol::HTTP1 do
98-
include_context Async::RSpec::Reactor
99-
100-
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}
92+
include_context Async::HTTP::Server
10393

10494
it_should_behave_like Async::HTTP::Body
10595
end
10696

10797
RSpec.describe Async::HTTP::Protocol::HTTPS do
108-
include_context Async::RSpec::Reactor
98+
include_context Async::HTTP::Server
99+
109100
let(:authority) {Localhost::Authority.new}
110101

111102
let(:server_context) {authority.server_context}
112103
let(:client_context) {authority.client_context}
113104

114105
# 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)}
106+
let(:server_endpoint) {Async::HTTP::Endpoint.parse("https://localhost:9299", ssl_context: server_context, reuse_port: true)}
107+
let(:client_endpoint) {Async::HTTP::Endpoint.parse("https://localhost:9299", ssl_context: client_context, reuse_port: true)}
117108

118109
it_should_behave_like Async::HTTP::Body
119110
end

spec/async/http/server_context.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
let(:protocol) {described_class}
1515
let(:endpoint) {Async::HTTP::Endpoint.parse('http://127.0.0.1:9294', timeout: 0.8, reuse_port: true, protocol: protocol)}
1616

17+
let(:server_endpoint) {endpoint}
18+
let(:client_endpoint) {endpoint}
19+
1720
let(:retries) {1}
1821

1922
let(:server) do
@@ -24,17 +27,17 @@
2427

2528
before do
2629
# We bind the endpoint before running the server so that we know incoming connections will be accepted:
27-
@bound_endpoint = Async::IO::SharedEndpoint.bound(endpoint)
30+
@bound_endpoint = Async::IO::SharedEndpoint.bound(server_endpoint)
2831

2932
# I feel a dedicated class might be better than this hack:
30-
allow(@bound_endpoint).to receive(:protocol).and_return(endpoint.protocol)
31-
allow(@bound_endpoint).to receive(:scheme).and_return(endpoint.scheme)
33+
allow(@bound_endpoint).to receive(:protocol).and_return(server_endpoint.protocol)
34+
allow(@bound_endpoint).to receive(:scheme).and_return(server_endpoint.scheme)
3235

3336
@server_task = Async do
3437
server.run
3538
end
3639

37-
@client = Async::HTTP::Client.new(endpoint, protocol: endpoint.protocol, retries: retries)
40+
@client = Async::HTTP::Client.new(client_endpoint, protocol: endpoint.protocol, retries: retries)
3841
end
3942

4043
after do

0 commit comments

Comments
 (0)