2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
module HttpIOHelpers
|
2018-11-18 11:00:15 +05:30
|
|
|
def stub_remote_url_206(url, file_path)
|
|
|
|
WebMock.stub_request(:get, url)
|
|
|
|
.to_return { |request| remote_url_response(file_path, request, 206) }
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
def stub_remote_url_200(url, file_path)
|
|
|
|
WebMock.stub_request(:get, url)
|
|
|
|
.to_return { |request| remote_url_response(file_path, request, 200) }
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
def stub_remote_url_500(url)
|
|
|
|
WebMock.stub_request(:get, url)
|
2018-05-09 12:01:36 +05:30
|
|
|
.to_return(status: [500, "Internal Server Error"])
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
def remote_url_response(file_path, request, response_status)
|
2018-05-09 12:01:36 +05:30
|
|
|
range = request.headers['Range'].match(/bytes=(\d+)-(\d+)/)
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
body = File.read(file_path).force_encoding(Encoding::BINARY)
|
|
|
|
size = body.bytesize
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
{
|
2018-11-18 11:00:15 +05:30
|
|
|
status: response_status,
|
|
|
|
headers: remote_url_response_headers(response_status, range[1].to_i, range[2].to_i, size),
|
|
|
|
body: body[range[1].to_i..range[2].to_i]
|
2018-05-09 12:01:36 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
def remote_url_response_headers(response_status, from, to, size)
|
|
|
|
{ 'Content-Type' => 'text/plain' }.tap do |headers|
|
|
|
|
if response_status == 206
|
|
|
|
headers.merge('Content-Range' => "bytes #{from}-#{to}/#{size}")
|
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_smaller_buffer_size_than(file_size)
|
|
|
|
blocks = (file_size / 128)
|
|
|
|
new_size = (blocks / 2) * 128
|
2018-11-18 11:00:15 +05:30
|
|
|
stub_const("Gitlab::HttpIO::BUFFER_SIZE", new_size)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def set_larger_buffer_size_than(file_size)
|
|
|
|
blocks = (file_size / 128)
|
|
|
|
new_size = (blocks * 2) * 128
|
2018-11-18 11:00:15 +05:30
|
|
|
stub_const("Gitlab::HttpIO::BUFFER_SIZE", new_size)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|