Skip to content

Commit

Permalink
Migrate tests from rspec -> sus.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Sep 10, 2023
1 parent f9450cb commit 06aba96
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 180 deletions.
2 changes: 2 additions & 0 deletions gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
gem "covered"
gem "sus"
gem "sus-fixtures-async"
gem "sus-fixtures-async-http", "~> 0.5"
gem "sus-fixtures-openssl"

gem "bake"
gem "bake-test"
Expand Down
80 changes: 41 additions & 39 deletions test/async/http/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,34 @@
require 'async/http/protocol'
require 'async/http/body/hijack'

require_relative 'server_context'
require 'sus/fixtures/async/http'

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

describe '.proxied_endpoint' do
let(:protocol) {subject}

with '.proxied_endpoint' do
it "can construct valid endpoint" do
endpoint = Async::HTTP::Endpoint.parse("http://www.codeotaku.com")
proxied_endpoint = client.proxied_endpoint(endpoint)

expect(proxied_endpoint).to be_kind_of(Async::HTTP::Endpoint)
expect(proxied_endpoint).to be_a(Async::HTTP::Endpoint)
end
end

describe '.proxied_client' do
with '.proxied_client' do
it "can construct valid client" do
endpoint = Async::HTTP::Endpoint.parse("http://www.codeotaku.com")
proxied_client = client.proxied_client(endpoint)

expect(proxied_client).to be_kind_of(Async::HTTP::Client)
expect(proxied_client).to be_a(Async::HTTP::Client)
end
end

context 'CONNECT' do
let(:server) do
Async::HTTP::Server.for(@bound_endpoint) do |request|
with 'CONNECT' do
let(:app) do
Protocol::HTTP::Middleware.for do |request|
Async::HTTP::Body::Hijack.response(request, 200, {}) do |stream|
chunk = stream.read
stream.close_read
Expand All @@ -52,7 +54,7 @@

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

expect(response).to be_success
expect(response).to be(:success?)

input.write(data)
input.close
Expand All @@ -61,9 +63,9 @@
end
end

context 'echo server' do
let(:server) do
Async::HTTP::Server.for(@bound_endpoint) do |request|
with 'echo server' do
let(:app) do
Protocol::HTTP::Middleware.for do |request|
expect(request.path).to be == "localhost:1"

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

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

proxy.connect do |peer|
stream = Async::IO::Stream.new(peer)
Expand All @@ -93,12 +95,12 @@
end

proxy.close
expect(proxy.client.pool).to be_empty
expect(proxy.client.pool).to be(:empty?)
end

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

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

Expand All @@ -110,13 +112,13 @@
stream.close
proxy.close

expect(proxy.client.pool).to be_empty
expect(proxy.client.pool).to be(:empty?)
end
end

context 'proxied client' do
let(:server) do
Async::HTTP::Server.for(@bound_endpoint) do |request|
with 'proxied client' do
let(:app) do
Protocol::HTTP::Middleware.for do |request|
expect(request.method).to be == "CONNECT"

unless authorization_lambda.call(request)
Expand Down Expand Up @@ -174,16 +176,16 @@
proxy_client = client.proxied_client(endpoint)

response = proxy_client.get("/search")
expect(response).to_not be_failure
expect(response).not.to be(:failure?)

# The response would be a redirect:
expect(response).to be_redirection
expect(response).to be(:redirection?)
response.finish

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

pp client
end
Expand All @@ -194,18 +196,18 @@

response = proxy_client.get("/search")

expect(response).to_not be_failure
expect(response.read).to_not be_empty
expect(response).not.to be(:failure?)
expect(response.read).not.to be(:empty?)

proxy_client.close
end

context 'authorization header required' do
with 'authorization header required' do
let(:authorization_lambda) do
->(request) {request.headers['proxy-authorization'] == 'supersecretpassword' }
end

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

it 'succeeds' do
Expand All @@ -214,22 +216,22 @@

response = proxy_client.get('/search')

expect(response).to_not be_failure
expect(response.read).to_not be_empty
expect(response).not.to be(:failure?)
expect(response.read).not.to be(:empty?)

proxy_client.close
end
end

context 'request does not include headers' do
with 'request does not include headers' do
it 'does not succeed' do
endpoint = Async::HTTP::Endpoint.parse("https://www.google.com")
proxy_client = client.proxied_client(endpoint)

expect do
# 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.
response = proxy_client.get('/search')
end.to raise_error(Async::HTTP::Proxy::ConnectFailure)
end.to raise_exception(Async::HTTP::Proxy::ConnectFailure)

proxy_client.close
end
Expand All @@ -238,14 +240,14 @@
end
end

RSpec.describe Async::HTTP::Protocol::HTTP10 do
it_behaves_like Async::HTTP::Proxy
describe Async::HTTP::Protocol::HTTP10 do
it_behaves_like AProxy
end

RSpec.describe Async::HTTP::Protocol::HTTP11 do
it_behaves_like Async::HTTP::Proxy
describe Async::HTTP::Protocol::HTTP11 do
it_behaves_like AProxy
end

RSpec.describe Async::HTTP::Protocol::HTTP2 do
it_behaves_like Async::HTTP::Proxy
describe Async::HTTP::Protocol::HTTP2 do
it_behaves_like AProxy
end
63 changes: 31 additions & 32 deletions test/async/http/relative_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
# Copyright, 2018-2023, by Samuel Williams.
# Copyright, 2019-2020, by Brian Morearty.

require_relative 'server_context'

require 'async/http/relative_location'
require 'async/http/server'

RSpec.describe Async::HTTP::RelativeLocation do
include_context Async::HTTP::Server
let(:protocol) {Async::HTTP::Protocol::HTTP1}
require 'sus/fixtures/async/http'

describe Async::HTTP::RelativeLocation do
include Sus::Fixtures::Async::HTTP::ServerContext

subject {described_class.new(@client, 1)}
let(:relative_location) {subject.new(@client, 1)}

context 'server redirections' do
context '301' do
let(:server) do
Async::HTTP::Server.for(@bound_endpoint) do |request|
with 'server redirections' do
with '301' do
let(:app) do
Protocol::HTTP::Middleware.for do |request|
case request.path
when '/home'
Protocol::HTTP::Response[301, {'location' => '/'}, []]
Expand All @@ -31,30 +30,30 @@
end

it 'should redirect POST to GET' do
response = subject.post('/')
response = relative_location.post('/')

expect(response).to be_success
expect(response).to be(:success?)
expect(response.read).to be == "GET"
end

context 'limiting redirects' do
with 'limiting redirects' do
it 'should allow the maximum number of redirects' do
response = subject.get('/')
response = relative_location.get('/')
response.finish
expect(response).to be_success
expect(response).to be(:success?)
end

it 'should fail with maximum redirects' do
expect{
response = subject.get('/home')
}.to raise_error(Async::HTTP::TooManyRedirects, /maximum/)
response = relative_location.get('/home')
}.to raise_exception(Async::HTTP::TooManyRedirects, message: be =~ /maximum/)
end
end
end

context '302' do
let(:server) do
Async::HTTP::Server.for(@bound_endpoint) do |request|
with '302' do
let(:app) do
Protocol::HTTP::Middleware.for do |request|
case request.path
when '/'
Protocol::HTTP::Response[302, {'location' => '/index.html'}, []]
Expand All @@ -65,16 +64,16 @@
end

it 'should redirect POST to GET' do
response = subject.post('/')
response = relative_location.post('/')

expect(response).to be_success
expect(response).to be(:success?)
expect(response.read).to be == "GET"
end
end

context '307' do
let(:server) do
Async::HTTP::Server.for(@bound_endpoint) do |request|
with '307' do
let(:app) do
Protocol::HTTP::Middleware.for do |request|
case request.path
when '/'
Protocol::HTTP::Response[307, {'location' => '/index.html'}, []]
Expand All @@ -85,16 +84,16 @@
end

it 'should redirect with same method' do
response = subject.post('/')
response = relative_location.post('/')

expect(response).to be_success
expect(response).to be(:success?)
expect(response.read).to be == "POST"
end
end

context '308' do
let(:server) do
Async::HTTP::Server.for(@bound_endpoint) do |request|
with '308' do
let(:app) do
Protocol::HTTP::Middleware.for do |request|
case request.path
when '/'
Protocol::HTTP::Response[308, {'location' => '/index.html'}, []]
Expand All @@ -105,9 +104,9 @@
end

it 'should redirect with same method' do
response = subject.post('/')
response = relative_location.post('/')

expect(response).to be_success
expect(response).to be(:success?)
expect(response.read).to be == "POST"
end
end
Expand Down
27 changes: 14 additions & 13 deletions test/async/http/retry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,38 @@
# Released under the MIT License.
# Copyright, 2020-2023, by Samuel Williams.

require_relative 'server_context'

require 'async/http/client'
require 'async/http/endpoint'

RSpec.describe 'consistent retry behaviour' do
include_context Async::HTTP::Server
let(:protocol) {Async::HTTP::Protocol::HTTP1}
require 'sus/fixtures/async/http'

describe 'consistent retry behaviour' do
include Sus::Fixtures::Async::HTTP::ServerContext

let(:delay) {0.1}
let(:retries) {2}

let(:server) do
Async::HTTP::Server.for(@bound_endpoint) do |request|
Async::Task.current.sleep(delay)
let(:app) do
Protocol::HTTP::Middleware.for do |request|
sleep(delay)
Protocol::HTTP::Response[200, {}, []]
end
end

def make_request(body)
# This causes the first request to fail with "SocketError" which is retried:
Async::Task.current.with_timeout(delay / 2, SocketError) do
Async::Task.current.with_timeout(delay / 2.0, SocketError) do
return client.get('/', {}, body)
end
end

specify 'with nil body' do
make_request(nil)
it "retries with nil body" do
response = make_request(nil)
expect(response).to be(:success?)
end

specify 'with empty array body' do
make_request([])
it "retries with empty body" do
response = make_request([])
expect(response).to be(:success?)
end
end
Loading

0 comments on commit 06aba96

Please sign in to comment.