Skip to content

Commit

Permalink
Merge fd622e8 into 7f444c3
Browse files Browse the repository at this point in the history
  • Loading branch information
iMacTia committed Aug 8, 2022
2 parents 7f444c3 + fd622e8 commit 6ca8009
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 8 deletions.
16 changes: 14 additions & 2 deletions .github/workflows/ci.yml
Expand Up @@ -40,7 +40,13 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: [ '2.6', '2.7', '3.0', '3.1', truffleruby-head ]
ruby: [ '2.6', '2.7', '3.0', '3.1' ]
experimental: [false]
include:
- ruby: head
experimental: true
- ruby: truffleruby-head
experimental: true

steps:
- uses: actions/checkout@v3
Expand All @@ -49,7 +55,13 @@ jobs:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true

- name: Test
- name: RSpec
continue-on-error: ${{ matrix.experimental }}
run: bundle exec rake

- name: Test External Adapters
if: ${{ matrix.ruby != '2.6' }}
continue-on-error: ${{ matrix.experimental }}
run: bundle exec bake test:external


1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -14,6 +14,7 @@ tmp
.bundle
Gemfile.lock
vendor/bundle
external

## PROJECT::SPECIFIC
.rbx
Expand Down
3 changes: 3 additions & 0 deletions .rubocop.yml
Expand Up @@ -14,6 +14,9 @@ Metrics/BlockLength:
- spec/**/*.rb
- examples/**/*.rb

Metrics/ParameterLists:
Max: 6

Layout/EmptyLinesAroundAttributeAccessor: # (0.83)
Enabled: true

Expand Down
3 changes: 2 additions & 1 deletion Gemfile
Expand Up @@ -7,6 +7,7 @@ source 'https://rubygems.org'
gem 'jruby-openssl', '~> 0.11.0', platforms: :jruby

group :development, :test do
gem 'bake-test-external'
gem 'coveralls_reborn', require: false
gem 'pry'
gem 'rack', '~> 2.2'
Expand All @@ -19,7 +20,7 @@ end

group :development, :lint do
gem 'rubocop'
gem 'rubocop-packaging', '~> 0.5'
gem 'rubocop-packaging', github: 'utkarsh2102/rubocop-packaging' # '~> 0.5'
gem 'rubocop-performance', '~> 1.0'
gem 'yard-junk'
end
Expand Down
3 changes: 3 additions & 0 deletions config/external.yaml
@@ -0,0 +1,3 @@
faraday-net_http:
url: https://github.com/lostisland/faraday-net_http.git
command: bundle exec rspec
4 changes: 2 additions & 2 deletions lib/faraday/adapter.rb
Expand Up @@ -59,7 +59,7 @@ def call(env)

private

def save_response(env, status, body, headers = nil, reason_phrase = nil)
def save_response(env, status, body, headers = nil, reason_phrase = nil, finished: true)
env.status = status
env.body = body
env.reason_phrase = reason_phrase&.to_s&.strip
Expand All @@ -68,7 +68,7 @@ def save_response(env, status, body, headers = nil, reason_phrase = nil)
yield(response_headers) if block_given?
end

env.response.finish(env) unless env.parallel?
env.response.finish(env) unless env.parallel? || !finished
env.response
end

Expand Down
18 changes: 18 additions & 0 deletions lib/faraday/options/env.rb
Expand Up @@ -157,6 +157,24 @@ def inspect
%(#<#{self.class}#{attrs.join(' ')}>)
end

def stream_response?
request.stream_response?
end

def stream_response(&block)
size = 0
yielded = false
block_result = block.call do |chunk| # rubocop:disable Performance/RedundantBlockCall
if chunk.bytesize.positive? || size.positive?
yielded = true
size += chunk.bytesize
request.on_data.call(chunk, size, self)
end
end
request.on_data.call(+'', 0, self) unless yielded
block_result
end

# @private
def custom_members
@custom_members ||= {}
Expand Down
20 changes: 17 additions & 3 deletions spec/support/shared_examples/request_method.rb
Expand Up @@ -153,25 +153,39 @@
let(:streamed) { [] }

context 'when response is empty' do
it do
it 'handles streaming' do
env = nil
conn.public_send(http_method, '/') do |req|
req.options.on_data = proc { |*args| streamed << args }
req.options.on_data = proc do |chunk, size, block_env|
streamed << [chunk, size]
env ||= block_env
end
end

expect(streamed).to eq([['', 0]])
# TODO: enable this after updating all existing adapters to the new streaming API
# expect(env).to be_a(Faraday::Env)
# expect(env.status).to eq(200)
end
end

context 'when response contains big data' do
before { request_stub.to_return(body: big_string) }

it 'handles streaming' do
env = nil
response = conn.public_send(http_method, '/') do |req|
req.options.on_data = proc { |*args| streamed << args }
req.options.on_data = proc do |chunk, size, block_env|
streamed << [chunk, size]
env ||= block_env
end
end

expect(response.body).to eq('')
check_streaming_response(streamed, chunk_size: 16 * 1024)
# TODO: enable this after updating all existing adapters to the new streaming API
# expect(env).to be_a(Faraday::Env)
# expect(env.status).to eq(200)
end
end
end
Expand Down

0 comments on commit 6ca8009

Please sign in to comment.