Skip to content

Commit 06aba96

Browse files
committed
Migrate tests from rspec -> sus.
1 parent f9450cb commit 06aba96

File tree

7 files changed

+113
-180
lines changed

7 files changed

+113
-180
lines changed

gems.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
gem "covered"
2929
gem "sus"
3030
gem "sus-fixtures-async"
31+
gem "sus-fixtures-async-http", "~> 0.5"
32+
gem "sus-fixtures-openssl"
3133

3234
gem "bake"
3335
gem "bake-test"

test/async/http/proxy.rb

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,34 @@
99
require 'async/http/protocol'
1010
require 'async/http/body/hijack'
1111

12-
require_relative 'server_context'
12+
require 'sus/fixtures/async/http'
1313

14-
RSpec.shared_examples_for Async::HTTP::Proxy do
15-
include_context Async::HTTP::Server
14+
AProxy = Sus::Shared("a proxy") do
15+
include Sus::Fixtures::Async::HTTP::ServerContext
1616

17-
describe '.proxied_endpoint' do
17+
let(:protocol) {subject}
18+
19+
with '.proxied_endpoint' do
1820
it "can construct valid endpoint" do
1921
endpoint = Async::HTTP::Endpoint.parse("http://www.codeotaku.com")
2022
proxied_endpoint = client.proxied_endpoint(endpoint)
2123

22-
expect(proxied_endpoint).to be_kind_of(Async::HTTP::Endpoint)
24+
expect(proxied_endpoint).to be_a(Async::HTTP::Endpoint)
2325
end
2426
end
2527

26-
describe '.proxied_client' do
28+
with '.proxied_client' do
2729
it "can construct valid client" do
2830
endpoint = Async::HTTP::Endpoint.parse("http://www.codeotaku.com")
2931
proxied_client = client.proxied_client(endpoint)
3032

31-
expect(proxied_client).to be_kind_of(Async::HTTP::Client)
33+
expect(proxied_client).to be_a(Async::HTTP::Client)
3234
end
3335
end
3436

35-
context 'CONNECT' do
36-
let(:server) do
37-
Async::HTTP::Server.for(@bound_endpoint) do |request|
37+
with 'CONNECT' do
38+
let(:app) do
39+
Protocol::HTTP::Middleware.for do |request|
3840
Async::HTTP::Body::Hijack.response(request, 200, {}) do |stream|
3941
chunk = stream.read
4042
stream.close_read
@@ -52,7 +54,7 @@
5254

5355
response = client.connect("127.0.0.1:1234", [], input)
5456

55-
expect(response).to be_success
57+
expect(response).to be(:success?)
5658

5759
input.write(data)
5860
input.close
@@ -61,9 +63,9 @@
6163
end
6264
end
6365

64-
context 'echo server' do
65-
let(:server) do
66-
Async::HTTP::Server.for(@bound_endpoint) do |request|
66+
with 'echo server' do
67+
let(:app) do
68+
Protocol::HTTP::Middleware.for do |request|
6769
expect(request.path).to be == "localhost:1"
6870

6971
Async::HTTP::Body::Hijack.response(request, 200, {}) do |stream|
@@ -81,7 +83,7 @@
8183

8284
it "can connect to remote system using block" do
8385
proxy = Async::HTTP::Proxy.tcp(client, "localhost", 1)
84-
expect(proxy.client.pool).to be_empty
86+
expect(proxy.client.pool).to be(:empty?)
8587

8688
proxy.connect do |peer|
8789
stream = Async::IO::Stream.new(peer)
@@ -93,12 +95,12 @@
9395
end
9496

9597
proxy.close
96-
expect(proxy.client.pool).to be_empty
98+
expect(proxy.client.pool).to be(:empty?)
9799
end
98100

99101
it "can connect to remote system" do
100102
proxy = Async::HTTP::Proxy.tcp(client, "localhost", 1)
101-
expect(proxy.client.pool).to be_empty
103+
expect(proxy.client.pool).to be(:empty?)
102104

103105
stream = Async::IO::Stream.new(proxy.connect)
104106

@@ -110,13 +112,13 @@
110112
stream.close
111113
proxy.close
112114

113-
expect(proxy.client.pool).to be_empty
115+
expect(proxy.client.pool).to be(:empty?)
114116
end
115117
end
116118

117-
context 'proxied client' do
118-
let(:server) do
119-
Async::HTTP::Server.for(@bound_endpoint) do |request|
119+
with 'proxied client' do
120+
let(:app) do
121+
Protocol::HTTP::Middleware.for do |request|
120122
expect(request.method).to be == "CONNECT"
121123

122124
unless authorization_lambda.call(request)
@@ -174,16 +176,16 @@
174176
proxy_client = client.proxied_client(endpoint)
175177

176178
response = proxy_client.get("/search")
177-
expect(response).to_not be_failure
179+
expect(response).not.to be(:failure?)
178180

179181
# The response would be a redirect:
180-
expect(response).to be_redirection
182+
expect(response).to be(:redirection?)
181183
response.finish
182184

183185
# The proxy.connnect response is not being released correctly - after pipe is done:
184-
expect(proxy_client.pool).to_not be_empty
186+
expect(proxy_client.pool).not.to be(:empty?)
185187
proxy_client.close
186-
expect(proxy_client.pool).to be_empty
188+
expect(proxy_client.pool).to be(:empty?)
187189

188190
pp client
189191
end
@@ -194,18 +196,18 @@
194196

195197
response = proxy_client.get("/search")
196198

197-
expect(response).to_not be_failure
198-
expect(response.read).to_not be_empty
199+
expect(response).not.to be(:failure?)
200+
expect(response.read).not.to be(:empty?)
199201

200202
proxy_client.close
201203
end
202204

203-
context 'authorization header required' do
205+
with 'authorization header required' do
204206
let(:authorization_lambda) do
205207
->(request) {request.headers['proxy-authorization'] == 'supersecretpassword' }
206208
end
207209

208-
context 'request includes headers' do
210+
with 'request includes headers' do
209211
let(:headers) { [['Proxy-Authorization', 'supersecretpassword']] }
210212

211213
it 'succeeds' do
@@ -214,22 +216,22 @@
214216

215217
response = proxy_client.get('/search')
216218

217-
expect(response).to_not be_failure
218-
expect(response.read).to_not be_empty
219+
expect(response).not.to be(:failure?)
220+
expect(response.read).not.to be(:empty?)
219221

220222
proxy_client.close
221223
end
222224
end
223225

224-
context 'request does not include headers' do
226+
with 'request does not include headers' do
225227
it 'does not succeed' do
226228
endpoint = Async::HTTP::Endpoint.parse("https://www.google.com")
227229
proxy_client = client.proxied_client(endpoint)
228230

229231
expect do
230232
# Why is this response not 407? Because the response should come from the proxied connection, but that connection failed to be established. Because of that, there is no response. If we respond here with 407, it would be indistinguisable from the remote server returning 407. That would be an odd case, but none-the-less a valid one.
231233
response = proxy_client.get('/search')
232-
end.to raise_error(Async::HTTP::Proxy::ConnectFailure)
234+
end.to raise_exception(Async::HTTP::Proxy::ConnectFailure)
233235

234236
proxy_client.close
235237
end
@@ -238,14 +240,14 @@
238240
end
239241
end
240242

241-
RSpec.describe Async::HTTP::Protocol::HTTP10 do
242-
it_behaves_like Async::HTTP::Proxy
243+
describe Async::HTTP::Protocol::HTTP10 do
244+
it_behaves_like AProxy
243245
end
244246

245-
RSpec.describe Async::HTTP::Protocol::HTTP11 do
246-
it_behaves_like Async::HTTP::Proxy
247+
describe Async::HTTP::Protocol::HTTP11 do
248+
it_behaves_like AProxy
247249
end
248250

249-
RSpec.describe Async::HTTP::Protocol::HTTP2 do
250-
it_behaves_like Async::HTTP::Proxy
251+
describe Async::HTTP::Protocol::HTTP2 do
252+
it_behaves_like AProxy
251253
end

test/async/http/relative_location.rb

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@
44
# Copyright, 2018-2023, by Samuel Williams.
55
# Copyright, 2019-2020, by Brian Morearty.
66

7-
require_relative 'server_context'
8-
97
require 'async/http/relative_location'
108
require 'async/http/server'
119

12-
RSpec.describe Async::HTTP::RelativeLocation do
13-
include_context Async::HTTP::Server
14-
let(:protocol) {Async::HTTP::Protocol::HTTP1}
10+
require 'sus/fixtures/async/http'
11+
12+
describe Async::HTTP::RelativeLocation do
13+
include Sus::Fixtures::Async::HTTP::ServerContext
1514

16-
subject {described_class.new(@client, 1)}
15+
let(:relative_location) {subject.new(@client, 1)}
1716

18-
context 'server redirections' do
19-
context '301' do
20-
let(:server) do
21-
Async::HTTP::Server.for(@bound_endpoint) do |request|
17+
with 'server redirections' do
18+
with '301' do
19+
let(:app) do
20+
Protocol::HTTP::Middleware.for do |request|
2221
case request.path
2322
when '/home'
2423
Protocol::HTTP::Response[301, {'location' => '/'}, []]
@@ -31,30 +30,30 @@
3130
end
3231

3332
it 'should redirect POST to GET' do
34-
response = subject.post('/')
33+
response = relative_location.post('/')
3534

36-
expect(response).to be_success
35+
expect(response).to be(:success?)
3736
expect(response.read).to be == "GET"
3837
end
3938

40-
context 'limiting redirects' do
39+
with 'limiting redirects' do
4140
it 'should allow the maximum number of redirects' do
42-
response = subject.get('/')
41+
response = relative_location.get('/')
4342
response.finish
44-
expect(response).to be_success
43+
expect(response).to be(:success?)
4544
end
4645

4746
it 'should fail with maximum redirects' do
4847
expect{
49-
response = subject.get('/home')
50-
}.to raise_error(Async::HTTP::TooManyRedirects, /maximum/)
48+
response = relative_location.get('/home')
49+
}.to raise_exception(Async::HTTP::TooManyRedirects, message: be =~ /maximum/)
5150
end
5251
end
5352
end
5453

55-
context '302' do
56-
let(:server) do
57-
Async::HTTP::Server.for(@bound_endpoint) do |request|
54+
with '302' do
55+
let(:app) do
56+
Protocol::HTTP::Middleware.for do |request|
5857
case request.path
5958
when '/'
6059
Protocol::HTTP::Response[302, {'location' => '/index.html'}, []]
@@ -65,16 +64,16 @@
6564
end
6665

6766
it 'should redirect POST to GET' do
68-
response = subject.post('/')
67+
response = relative_location.post('/')
6968

70-
expect(response).to be_success
69+
expect(response).to be(:success?)
7170
expect(response.read).to be == "GET"
7271
end
7372
end
7473

75-
context '307' do
76-
let(:server) do
77-
Async::HTTP::Server.for(@bound_endpoint) do |request|
74+
with '307' do
75+
let(:app) do
76+
Protocol::HTTP::Middleware.for do |request|
7877
case request.path
7978
when '/'
8079
Protocol::HTTP::Response[307, {'location' => '/index.html'}, []]
@@ -85,16 +84,16 @@
8584
end
8685

8786
it 'should redirect with same method' do
88-
response = subject.post('/')
87+
response = relative_location.post('/')
8988

90-
expect(response).to be_success
89+
expect(response).to be(:success?)
9190
expect(response.read).to be == "POST"
9291
end
9392
end
9493

95-
context '308' do
96-
let(:server) do
97-
Async::HTTP::Server.for(@bound_endpoint) do |request|
94+
with '308' do
95+
let(:app) do
96+
Protocol::HTTP::Middleware.for do |request|
9897
case request.path
9998
when '/'
10099
Protocol::HTTP::Response[308, {'location' => '/index.html'}, []]
@@ -105,9 +104,9 @@
105104
end
106105

107106
it 'should redirect with same method' do
108-
response = subject.post('/')
107+
response = relative_location.post('/')
109108

110-
expect(response).to be_success
109+
expect(response).to be(:success?)
111110
expect(response.read).to be == "POST"
112111
end
113112
end

test/async/http/retry.rb

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,38 @@
33
# Released under the MIT License.
44
# Copyright, 2020-2023, by Samuel Williams.
55

6-
require_relative 'server_context'
7-
86
require 'async/http/client'
97
require 'async/http/endpoint'
108

11-
RSpec.describe 'consistent retry behaviour' do
12-
include_context Async::HTTP::Server
13-
let(:protocol) {Async::HTTP::Protocol::HTTP1}
9+
require 'sus/fixtures/async/http'
10+
11+
describe 'consistent retry behaviour' do
12+
include Sus::Fixtures::Async::HTTP::ServerContext
1413

1514
let(:delay) {0.1}
1615
let(:retries) {2}
1716

18-
let(:server) do
19-
Async::HTTP::Server.for(@bound_endpoint) do |request|
20-
Async::Task.current.sleep(delay)
17+
let(:app) do
18+
Protocol::HTTP::Middleware.for do |request|
19+
sleep(delay)
2120
Protocol::HTTP::Response[200, {}, []]
2221
end
2322
end
2423

2524
def make_request(body)
2625
# This causes the first request to fail with "SocketError" which is retried:
27-
Async::Task.current.with_timeout(delay / 2, SocketError) do
26+
Async::Task.current.with_timeout(delay / 2.0, SocketError) do
2827
return client.get('/', {}, body)
2928
end
3029
end
3130

32-
specify 'with nil body' do
33-
make_request(nil)
31+
it "retries with nil body" do
32+
response = make_request(nil)
33+
expect(response).to be(:success?)
3434
end
3535

36-
specify 'with empty array body' do
37-
make_request([])
36+
it "retries with empty body" do
37+
response = make_request([])
38+
expect(response).to be(:success?)
3839
end
3940
end

0 commit comments

Comments
 (0)