|
9 | 9 | require 'async/http/protocol'
|
10 | 10 | require 'async/http/body/hijack'
|
11 | 11 |
|
12 |
| -require_relative 'server_context' |
| 12 | +require 'sus/fixtures/async/http' |
13 | 13 |
|
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 |
16 | 16 |
|
17 |
| - describe '.proxied_endpoint' do |
| 17 | + let(:protocol) {subject} |
| 18 | + |
| 19 | + with '.proxied_endpoint' do |
18 | 20 | it "can construct valid endpoint" do
|
19 | 21 | endpoint = Async::HTTP::Endpoint.parse("http://www.codeotaku.com")
|
20 | 22 | proxied_endpoint = client.proxied_endpoint(endpoint)
|
21 | 23 |
|
22 |
| - expect(proxied_endpoint).to be_kind_of(Async::HTTP::Endpoint) |
| 24 | + expect(proxied_endpoint).to be_a(Async::HTTP::Endpoint) |
23 | 25 | end
|
24 | 26 | end
|
25 | 27 |
|
26 |
| - describe '.proxied_client' do |
| 28 | + with '.proxied_client' do |
27 | 29 | it "can construct valid client" do
|
28 | 30 | endpoint = Async::HTTP::Endpoint.parse("http://www.codeotaku.com")
|
29 | 31 | proxied_client = client.proxied_client(endpoint)
|
30 | 32 |
|
31 |
| - expect(proxied_client).to be_kind_of(Async::HTTP::Client) |
| 33 | + expect(proxied_client).to be_a(Async::HTTP::Client) |
32 | 34 | end
|
33 | 35 | end
|
34 | 36 |
|
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| |
38 | 40 | Async::HTTP::Body::Hijack.response(request, 200, {}) do |stream|
|
39 | 41 | chunk = stream.read
|
40 | 42 | stream.close_read
|
|
52 | 54 |
|
53 | 55 | response = client.connect("127.0.0.1:1234", [], input)
|
54 | 56 |
|
55 |
| - expect(response).to be_success |
| 57 | + expect(response).to be(:success?) |
56 | 58 |
|
57 | 59 | input.write(data)
|
58 | 60 | input.close
|
|
61 | 63 | end
|
62 | 64 | end
|
63 | 65 |
|
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| |
67 | 69 | expect(request.path).to be == "localhost:1"
|
68 | 70 |
|
69 | 71 | Async::HTTP::Body::Hijack.response(request, 200, {}) do |stream|
|
|
81 | 83 |
|
82 | 84 | it "can connect to remote system using block" do
|
83 | 85 | proxy = Async::HTTP::Proxy.tcp(client, "localhost", 1)
|
84 |
| - expect(proxy.client.pool).to be_empty |
| 86 | + expect(proxy.client.pool).to be(:empty?) |
85 | 87 |
|
86 | 88 | proxy.connect do |peer|
|
87 | 89 | stream = Async::IO::Stream.new(peer)
|
|
93 | 95 | end
|
94 | 96 |
|
95 | 97 | proxy.close
|
96 |
| - expect(proxy.client.pool).to be_empty |
| 98 | + expect(proxy.client.pool).to be(:empty?) |
97 | 99 | end
|
98 | 100 |
|
99 | 101 | it "can connect to remote system" do
|
100 | 102 | proxy = Async::HTTP::Proxy.tcp(client, "localhost", 1)
|
101 |
| - expect(proxy.client.pool).to be_empty |
| 103 | + expect(proxy.client.pool).to be(:empty?) |
102 | 104 |
|
103 | 105 | stream = Async::IO::Stream.new(proxy.connect)
|
104 | 106 |
|
|
110 | 112 | stream.close
|
111 | 113 | proxy.close
|
112 | 114 |
|
113 |
| - expect(proxy.client.pool).to be_empty |
| 115 | + expect(proxy.client.pool).to be(:empty?) |
114 | 116 | end
|
115 | 117 | end
|
116 | 118 |
|
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| |
120 | 122 | expect(request.method).to be == "CONNECT"
|
121 | 123 |
|
122 | 124 | unless authorization_lambda.call(request)
|
|
174 | 176 | proxy_client = client.proxied_client(endpoint)
|
175 | 177 |
|
176 | 178 | response = proxy_client.get("/search")
|
177 |
| - expect(response).to_not be_failure |
| 179 | + expect(response).not.to be(:failure?) |
178 | 180 |
|
179 | 181 | # The response would be a redirect:
|
180 |
| - expect(response).to be_redirection |
| 182 | + expect(response).to be(:redirection?) |
181 | 183 | response.finish
|
182 | 184 |
|
183 | 185 | # 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?) |
185 | 187 | proxy_client.close
|
186 |
| - expect(proxy_client.pool).to be_empty |
| 188 | + expect(proxy_client.pool).to be(:empty?) |
187 | 189 |
|
188 | 190 | pp client
|
189 | 191 | end
|
|
194 | 196 |
|
195 | 197 | response = proxy_client.get("/search")
|
196 | 198 |
|
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?) |
199 | 201 |
|
200 | 202 | proxy_client.close
|
201 | 203 | end
|
202 | 204 |
|
203 |
| - context 'authorization header required' do |
| 205 | + with 'authorization header required' do |
204 | 206 | let(:authorization_lambda) do
|
205 | 207 | ->(request) {request.headers['proxy-authorization'] == 'supersecretpassword' }
|
206 | 208 | end
|
207 | 209 |
|
208 |
| - context 'request includes headers' do |
| 210 | + with 'request includes headers' do |
209 | 211 | let(:headers) { [['Proxy-Authorization', 'supersecretpassword']] }
|
210 | 212 |
|
211 | 213 | it 'succeeds' do
|
|
214 | 216 |
|
215 | 217 | response = proxy_client.get('/search')
|
216 | 218 |
|
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?) |
219 | 221 |
|
220 | 222 | proxy_client.close
|
221 | 223 | end
|
222 | 224 | end
|
223 | 225 |
|
224 |
| - context 'request does not include headers' do |
| 226 | + with 'request does not include headers' do |
225 | 227 | it 'does not succeed' do
|
226 | 228 | endpoint = Async::HTTP::Endpoint.parse("https://www.google.com")
|
227 | 229 | proxy_client = client.proxied_client(endpoint)
|
228 | 230 |
|
229 | 231 | expect do
|
230 | 232 | # 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.
|
231 | 233 | response = proxy_client.get('/search')
|
232 |
| - end.to raise_error(Async::HTTP::Proxy::ConnectFailure) |
| 234 | + end.to raise_exception(Async::HTTP::Proxy::ConnectFailure) |
233 | 235 |
|
234 | 236 | proxy_client.close
|
235 | 237 | end
|
|
238 | 240 | end
|
239 | 241 | end
|
240 | 242 |
|
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 |
243 | 245 | end
|
244 | 246 |
|
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 |
247 | 249 | end
|
248 | 250 |
|
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 |
251 | 253 | end
|
0 commit comments