Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'net/http'
require 'stringio'
require_relative 'kiota_client_factory'
require_relative 'request_body'
require_relative 'middleware/response_handler_option'

module MicrosoftKiotaFaraday
Expand Down Expand Up @@ -180,8 +181,7 @@ def get_request_from_request_info(request_info)
end
end
end
request.body = request_info.content unless request_info.content.nil? || request_info.content.empty?
# TODO: the json serialization writer returns a string at the moment, change to body_stream when this is fixed
RequestBody.apply(request, request_info.content)
request_options = request_info.get_request_options
if !request_options.nil? && !request_options.empty?
request.options = Faraday::RequestOptions.new if request.options.nil?
Expand Down
45 changes: 45 additions & 0 deletions components/http/lib/microsoft_kiota_faraday/request_body.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

module MicrosoftKiotaFaraday
# Faraday sends an IO body as a Net::HTTP body_stream, which needs its length up front, so a
# stream is passed through and measured rather than read into memory.
module RequestBody
FRAMING_HEADERS = %w[Content-Length Transfer-Encoding].freeze

module_function

def apply(request, content)
# the request owns its framing, so whatever the caller sent is dropped and then set from the
# body that is actually written, which leaves every early return below safe
FRAMING_HEADERS.each { |header| request.headers.delete(header) }
return if content.nil?

unless content.respond_to?(:read)
request.body = content unless content.respond_to?(:empty?) && content.empty?
return
end

length = stream_length(content)
return if length&.zero?

if length.nil?
request.headers['Transfer-Encoding'] = 'chunked'
else
request.headers['Content-Length'] = length.to_s
end
request.body = content
end

# a stream that cannot report a size, such as a pipe or $stdin, is sent chunked instead
def stream_length(content)
return nil unless content.respond_to?(:size) && content.respond_to?(:pos)

size = content.size
# a stream can be positioned past its end, which leaves nothing to send rather than a
# negative number of bytes
size.nil? ? nil : [size - content.pos, 0].max
rescue StandardError
nil
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# frozen_string_literal: true

require 'stringio'

RSpec.describe MicrosoftKiotaFaraday::FaradayRequestAdapter do
subject(:adapter) { described_class.new(authentication_provider, nil, nil, client) }

let(:authentication_provider) { double('authentication_provider') }
let(:client) { double('client') }

let(:request_info) do
info = MicrosoftKiotaAbstractions::RequestInformation.new
info.http_method = :PUT
info.url_template = '{+baseurl}/files'
info.path_parameters = { 'baseurl' => 'https://example.com' }
info
end

before do
allow(client).to receive(:build_request).and_return(Faraday::Request.create(:put))
end

it 'sends a stream body without reading it into memory' do
stream = StringIO.new('rawbytes')
request_info.set_stream_content(stream, 'application/octet-stream')
request = adapter.get_request_from_request_info(request_info)
expect(request.body).to be(stream)
expect(request.headers['Content-Length']).to eq('8')
expect(stream.pos).to be_zero
end

it 'measures a stream that has already been partially read' do
stream = StringIO.new('rawbytes')
stream.read(3)
request_info.set_stream_content(stream, 'application/octet-stream')
expect(adapter.get_request_from_request_info(request_info).headers['Content-Length']).to eq('5')
end

it 'sends a stream of unknown length chunked' do
reader, writer = IO.pipe
writer.write('rawbytes')
writer.close
request_info.set_stream_content(reader, 'application/octet-stream')
request = adapter.get_request_from_request_info(request_info)
expect(request.headers['Transfer-Encoding']).to eq('chunked')
expect(request.headers['Content-Length']).to be_nil
end

it 'omits an empty stream body' do
request_info.set_stream_content(StringIO.new(''), 'application/octet-stream')
expect(adapter.get_request_from_request_info(request_info).body).to be_nil
end

it 'drops a caller supplied Content-Length when the body has to be chunked' do
reader, writer = IO.pipe
writer.write('rawbytes')
writer.close
request_info.headers.add('Content-Length', '99')
request_info.set_stream_content(reader, 'application/octet-stream')
request = adapter.get_request_from_request_info(request_info)
expect(request.headers['Transfer-Encoding']).to eq('chunked')
expect(request.headers['Content-Length']).to be_nil
end

it 'drops a caller supplied Transfer-Encoding when the length is known' do
request_info.headers.add('Transfer-Encoding', 'chunked')
request_info.set_stream_content(StringIO.new('rawbytes'), 'application/octet-stream')
request = adapter.get_request_from_request_info(request_info)
expect(request.headers['Content-Length']).to eq('8')
expect(request.headers['Transfer-Encoding']).to be_nil
end

it 'clears stale framing headers for an empty stream' do
request_info.headers.add('Content-Length', '99')
request_info.headers.add('Transfer-Encoding', 'chunked')
request_info.set_stream_content(StringIO.new(''), 'application/octet-stream')
request = adapter.get_request_from_request_info(request_info)
expect(request.body).to be_nil
expect(request.headers['Content-Length']).to be_nil
expect(request.headers['Transfer-Encoding']).to be_nil
end

it 'clears stale framing headers for an empty string body' do
request_info.headers.add('Content-Length', '99')
request_info.set_stream_content('', 'application/json')
request = adapter.get_request_from_request_info(request_info)
expect(request.body).to be_nil
expect(request.headers['Content-Length']).to be_nil
end

it 'clears stale framing headers when there is no body at all' do
request_info.headers.add('Content-Length', '99')
request = adapter.get_request_from_request_info(request_info)
expect(request.headers['Content-Length']).to be_nil
end

it 'treats a stream positioned past its end as empty' do
stream = StringIO.new('rawbytes')
stream.seek(100)
request_info.headers.add('Content-Length', '99')
request_info.set_stream_content(stream, 'application/octet-stream')
request = adapter.get_request_from_request_info(request_info)
expect(request.body).to be_nil
expect(request.headers['Content-Length']).to be_nil
end

it 'sends a body that does not answer to empty?' do
request_info.set_stream_content(42, 'application/json')
expect(adapter.get_request_from_request_info(request_info).body).to eq(42)
end

it 'still sends a string body' do
request_info.set_stream_content('{"a":1}', 'application/json')
expect(adapter.get_request_from_request_info(request_info).body).to eq('{"a":1}')
end

it 'still omits an empty string body' do
request_info.set_stream_content('', 'application/json')
expect(adapter.get_request_from_request_info(request_info).body).to be_nil
end
end
Loading