Skip to content

Commit 5073a1c

Browse files
binarycodeioquatix
authored andcommitted
fix chunked and no content responses
1 parent 520adbd commit 5073a1c

File tree

2 files changed

+48
-28
lines changed

2 files changed

+48
-28
lines changed

lib/async/http/faraday/adapter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ def call(env)
3232
client = HTTP::Client.new(*endpoints_for(env).to_a)
3333

3434
response = client.send(env[:method], env[:url].request_uri, env[:request_headers], env[:body] || [])
35-
36-
save_response(env, response.status, response.body.read, response.headers)
35+
36+
save_response(env, response.status, response.body && response.body.join, response.headers)
3737

3838
@app.call env
3939
end

spec/async/http/faraday/adapter_spec.rb

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,41 +29,61 @@
2929
Async::HTTP::Endpoint.parse('http://127.0.0.1:9294')
3030
}
3131

32-
it "client can get resource" do
33-
app = ->(request) do
34-
Protocol::HTTP::Response[200, {}, ["Hello World"]]
32+
def run_server(response)
33+
Async do |task|
34+
begin
35+
server_task = task.async do
36+
app = ->(_) { response }
37+
Async::HTTP::Server.new(app, endpoint).run
38+
end
39+
40+
yield
41+
ensure
42+
server_task.stop
43+
end
44+
end.wait
45+
end
46+
47+
def get_response(url, path)
48+
connection = Faraday.new(url: url) do |faraday|
49+
faraday.response :logger
50+
faraday.adapter :async_http
3551
end
3652

37-
server = Async::HTTP::Server.new(app, endpoint)
53+
connection.get(path)
54+
end
55+
56+
it "client can get resource" do
57+
run_server(Protocol::HTTP::Response[200, {}, ['Hello World']]) do
58+
response = get_response(endpoint.url, '/index')
3859

39-
Async do |task|
40-
server_task = task.async do
41-
server.run
42-
end
43-
44-
connection = Faraday.new(:url => endpoint.url) do |faraday|
45-
faraday.response :logger
46-
faraday.adapter :async_http
47-
end
48-
49-
response = connection.get("/index")
50-
51-
expect(response.body).to be == "Hello World"
52-
53-
server_task.stop
60+
expect(response.body).to eq 'Hello World'
5461
end
5562
end
5663

5764
it "can get remote resource" do
58-
Async do |task|
59-
connection = Faraday.new(:url => "http://www.google.com") do |faraday|
60-
faraday.response :logger
61-
faraday.adapter :async_http
62-
end
63-
64-
response = connection.get("/search?q=cats")
65+
Async do
66+
response = get_response('http://www.google.com', '/search?q=cats')
6567

6668
expect(response).to be_success
6769
end
6870
end
71+
72+
it 'properly handles chunked responses' do
73+
large_response_size = 65536
74+
75+
run_server(Protocol::HTTP::Response[200, {}, ['.' * large_response_size]]) do
76+
response = get_response(endpoint.url, '/index')
77+
78+
expect(response.body.size).to eq large_response_size
79+
end
80+
end
81+
82+
it 'properly handles no content responses' do
83+
run_server(Protocol::HTTP::Response[204]) do
84+
response = get_response(endpoint.url, '/index')
85+
86+
expect(response.body).to be_nil
87+
end
88+
end
6989
end

0 commit comments

Comments
 (0)