Skip to content

Commit 281fad9

Browse files
ebababiioquatix
authored andcommitted
Implement response info in streaming API. (#48)
1 parent ef85c56 commit 281fad9

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

lib/async/http/faraday/adapter.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,19 @@ def perform_request(env)
179179
response = env.stream_response do |&on_data|
180180
response = client.call(request)
181181

182+
save_response(env, response.status, nil, response.headers, finished: false)
183+
182184
response.each do |chunk|
183185
on_data.call(chunk)
184186
end
185187

186188
response
187189
end
188-
189-
save_response(env, response.status, nil, response.headers)
190190
else
191191
response = client.call(request)
192-
193-
save_response(env, response.status, encoded_body(response), response.headers)
194192
end
193+
194+
save_response(env, response.status, encoded_body(response), response.headers)
195195
end
196196
end
197197

test/async/http/faraday/adapter.rb

+9-5
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,20 @@ def get_response(url = bound_url, path = '/index', adapter_options: {})
198198
builder.adapter :async_http
199199
end
200200

201-
chunks = []
201+
streamed = []
202+
env = nil
202203

203204
response = client.get(bound_url) do |request|
204-
request.options.on_data = proc do |chunk|
205-
chunks << chunk
205+
request.options.on_data = proc do |chunk, size, block_env|
206+
streamed << [chunk, size]
207+
env ||= block_env
206208
end
207209
end
208210

209-
expect(response.body).to be_nil
210-
expect(chunks).to be == ["chunk0", "chunk1", "chunk2"]
211+
expect(response.body).to be(:empty?)
212+
expect(streamed).to be == [["chunk0", 6], ["chunk1", 12], ["chunk2", 18]]
213+
expect(env).to be_a(Faraday::Env)
214+
expect(env.status).to be == 200
211215
end
212216
end
213217
end

0 commit comments

Comments
 (0)