Skip to content

Implement Response Information of the new Streaming API #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/async/http/faraday/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,19 @@ def perform_request(env)
response = env.stream_response do |&on_data|
response = client.call(request)

save_response(env, response.status, nil, response.headers, finished: false)

response.each do |chunk|
on_data.call(chunk)
end

response
end

save_response(env, response.status, nil, response.headers)
else
response = client.call(request)

save_response(env, response.status, encoded_body(response), response.headers)
end

save_response(env, response.status, encoded_body(response), response.headers)
end
end

Expand Down
14 changes: 9 additions & 5 deletions test/async/http/faraday/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,20 @@ def get_response(url = bound_url, path = '/index', adapter_options: {})
builder.adapter :async_http
end

chunks = []
streamed = []
env = nil

response = client.get(bound_url) do |request|
request.options.on_data = proc do |chunk|
chunks << chunk
request.options.on_data = proc do |chunk, size, block_env|
streamed << [chunk, size]
env ||= block_env
end
end

expect(response.body).to be_nil
expect(chunks).to be == ["chunk0", "chunk1", "chunk2"]
expect(response.body).to be(:empty?)
expect(streamed).to be == [["chunk0", 6], ["chunk1", 12], ["chunk2", 18]]
expect(env).to be_a(Faraday::Env)
expect(env.status).to be == 200
end
end
end
Expand Down
Loading